Forums

Custom Field Array (12 posts)

  1. Michael
    Member
    Posted 1 year ago #

    Hi,

    I am pretty new to php and I am sure that there is a something glaring that I am missing, but I am hoping someone can help me.

    I have to custom fields that each contain multiple values and I am trying to get them to display in sequence. I am using the code:

    <?php
    
    $m_venue = get_post_meta($post->ID, "monday_venue_name", false);
    $m_event = get_post_meta($post->ID, "monday_event_description", false);
    
        foreach ( $m_venue as $m_venue) {
    
                if ($m_venue){
    
                    echo '<div>'.$m_venue.'</div>';
    
                    echo '<div>'.$m_event.'</div>';
    
                }        
    
            }
    
    ?>

    I can get the $m_venue to display correctly, but $m_event simply comes out as "Array".

    I believe I need to break up the array into its individual vales, but I am having trouble figuring out how to do that.

    If anyone has some suggestions about how I might do this, or where I might turn for more information, I would greatly appreciate it.

    Thank you very much!

  2. vtxyzzy
    Member
    Posted 1 year ago #

    You will need some way to connect each $m_venue to its own $m_event. I do not see anything in the code shown that indicates a link between the two.

    If these are the only two data items you have, it might be simpler to put them both in a single custom field with a separator between them. Then explode them out to display them separately.

    Otherwise, you will need to put some key field in each of your values that can be extracted and used to look up the corresponding values.

  3. Michael
    Member
    Posted 1 year ago #

    Hey vtxyzzy!

    Thank you so much for your response!

    I see your point and it makes perfect sense!

    I must admit that I am not entirely sure how I might assign a link value to each of the fields automatically, but I will investigate!

    I was also exploring other options for getting the result I want and I came up with this code:
    '<?php

    $m_venue = get_post_meta($post->ID, "monday_venue_name", false);
    $m_event = get_post_meta($post->ID, "monday_event_description", false);

    for ( $i = 0; $i < count($m_venue); $i++) {

    echo '<div>'.$m_venue[$i].'</div>';

    echo '<div>'.$m_event[$i].'</div>';

    }

    ?>'

    Which does in fact return the value of each corresponding field, however the two are not linked. In other words, the $m_venue and $m_event values I was to have "grouped" together are not grouping.

    I believe this is almost exactly the point you made as well.

    I was thinking of using reset() to make sure that each array will start at 0, but I have not been able to make that work.

    Thank you again, and if you have any other suggestions I would love to hear them!

    Michael

  4. vtxyzzy
    Member
    Posted 1 year ago #

    AFAIK, there is no way to automatically assign a link key to each value. That is something you will have to do as you enter each custom field.

    Using reset() will not work because the arrays are not guaranteed to be in corresponding order to begin with.

    You will either need to put all values that go together in one custom field, say comma-separated, or else add in a key to each pair.

    Since each set will be unique to a post, this should be fairly easy. For example, you could just use a zero-padded number like this:

    Custom Field               Value
    ------------------------   ---------------------------
    monday_venue_name          001,the first venue
    monday_event_description   001,the first description
    monday_venue_name          002,the second venue
    monday_event_description   002,the second description

    Then, index the event array and get the values by key in the display loop:

    foreach ($m_event as $event) {
       list($key, $value) = explode(',',$event);
       $indexed_events[$key] = $value;
    }
    foreach ( $m_venue as $each_venue) {
       list($key,$venue) = explode(',',$each_venue);
       echo '<div>'.$venue.'</div>';
       echo '<div>';
       if ($description = $indexed_event[$key]) {
          echo $description;
       } else {
          echo '&nbsp';
       }
       echo '</div>';
    }
  5. Michael
    Member
    Posted 1 year ago #

    Thank you so much!

    I was toying with the idea of combining the two arrays into one array but that does not work either.

    I was hoping to keep the entry fields separate on the user end rather than comma separated, so I will have to design a key for each pair.

    Thank you so much for all of your help!

    Michael

  6. vtxyzzy
    Member
    Posted 1 year ago #

    You beat to it. Please refer to my previous reply.

  7. Michael
    Member
    Posted 1 year ago #

    Wow!

    Thank you!

    This is really great!

    I am sorry if my "newbie" is showing, but I wondered if you might be able to point me to a place where I might be able to learn about adding the zero-padded number to the field set?

    I am using the Custom Field Template plugin to generate the fields on a custom content type.

    I really do appreciate all of your help!

    Thank you so much!

  8. vtxyzzy
    Member
    Posted 1 year ago #

    I have never used the Custom Field Template plugin, so I don't know how it works.

    Do you type in the values for the custom fields? If so, just type in the value of the key as well.

  9. Michael
    Member
    Posted 1 year ago #

    Oh, the plugin just creates a UI for adding custom fields, well that is not ALL it does, but I cant claim to fully understand its other features.

    Hahahah!

    I have to imagine that there is a way to "count" and assign a unique ID to each instance of each custom field, and have it relatively "transparent" on the content type edit screen.

    I just have not yet wrapped my head around it yet.

    Thank you so much for all your help.

  10. vtxyzzy
    Member
    Posted 1 year ago #

    I can't imagine that it would be very simple to do what you are wanting. What happens if you only fill in one value of a pair? What happens if you go back tomorrow and edit one of a pair?

    If the plugin adds the key, it will have to show up in the value box. The user could then edit it and break the link.

    This could get nasty.

  11. Michael
    Member
    Posted 1 year ago #

    Yes, I am beginning to think you are very right about that!

    What I really want to do is create a weekly calendar that is connected to a custom post time so that the calendar of events is not updated in "real" time, but rather each week when that post is published.

    I guess I should probably look in another direction. Perhaps an already existing plugin that will allow for this type of thing.

    Its less of a calendar and more of a list of weekly events.

    In theory it does not sound too hard, but I am clearly doing something off-base!

    Hahahaha!

    Thank you so much for your help!

    Michael

  12. vtxyzzy
    Member
    Posted 1 year ago #

    I may be able to help if you are using a free theme that I can download to my test site. I will need a lot more detail, though.

    • What do you want to see on the screen?
    • Who enters the data?
    • How often is it entered?
    • How will you get to the screen? Does it need to show a post, or can it be a static page?

    Email me at mac =at= mcdspot =dot= com with as much detail as you can.

Topic Closed

This topic has been closed to new replies.

About this Topic