1

I have a form and date fields in it: "date from" and "date to" (to make a specify a period of availability. There may be several available periods, so I made a button by which you can add more "date from" and "date to" fields.

I use custom meta fields to store the values of the entered days and here is the code:

$s = 1;
foreach($_POST['tgt_available_from'] AS $sID => $sValue){
   $ads_data['available_dates_from'][$s] = $sValue;
   $s++;
}   
$s = 1; 
foreach($_POST['tgt_available_to'] AS $sID => $sValue){
   $ads_data['available_dates_to'][$s] = $sValue;
   $s++;
}

    $s = 1;
 foreach( $ads_data['available_dates_to'] AS $sID => $sValue){
            update_post_meta($postid, 'available_dates_from_'.$s, $ads_data['available_dates_from'][$s]);
            update_post_meta($postid, 'available_dates_to_'.$s, $ads_data['available_dates_to'][$s]);
            $s++;
    }

Once post is saved, I get the following meta keys / meta values array:

[available_dates_from_1] => Array
    (
        [0] => 01/19/2012
    )

[available_dates_to_1] => Array
    (
        [0] => 01/29/2012
    )

[available_dates_from_2] => Array
    (
        [0] => 02/19/2012
    )

[available_dates_to_2] => Array
    (
        [0] => 02/28/2012
    )

[available_dates_from_3] => Array
    (
        [0] => 03/19/2012
    )

[available_dates_to_3] => Array
    (
        [0] => 03/29/2012
    )

So the problem that I have is that I don't know how could I now show all the avalable dates (from and to) in theme template. 'available_dates_from_1' and 'available_dates_from_2' are not related so I can't make a foreach loop. I don't know and the total number of entered available days, so i can't use a while loop.

Maybe there could be any smarter solution?

1

This completely untested, but something like this should work. It assumes that the dates come in before (start and end) - this should have been verified when the data was processed.

$i=1;
$key_start = 'available_dates_from_';
$key_end = 'available_dates_to_';

echo '<ul>';
while($start = get_post_meta($post_id, $key_start.$i, true)){
    $end = get_post_meta($post_id, $key_end.$i, true);
    echo '<li> From '.$start. ' until '.$end.'</li>';
    $++;
}
echo '</ul>';

This goes through each of the meta keys for that post 'available_dates_from_i' from i=1 until the key doesn't exist.

The drawback is that will continue until it first fails to find the appropriate key. So if you delete a pair of dates say, all those after it will not be picked up by this loop, unless you renumber them.

By after I refer to the index appended to the key, not chronologically.

If querying by a date range is not important then you may find it easier to store the ranges as arrays. If it is, it would probably make more sense to store this in your own custom table.

0

You can save arrays as meta too, so if you save it this way, I think it's easier to iterate thru:

$s = 1;
foreach($_POST['tgt_available_from'] AS $sID => $sValue){
   $ads_data[$s]['available_dates_from'] = $sValue;
   $s++;
}   
$s = 1; 
foreach($_POST['tgt_available_to'] AS $sID => $sValue){
   $ads_data[$s]['available_dates_to'] = $sValue;
   $s++;
}

 update_post_meta($postid, 'available_dates', $ads_data);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.