I’m at a loss. I’ve got a custom meta box with the ‘multicheck’ code that Jan created. It’s working great. My issue now is getting the data to return properly.
Scenario: there are 5 possible values, each with the same meta_key. What I am trying to do is display specific content if a value is stored with one of the 5 possible values. What is happening is that the content is repeating.
Current site: http://dev.andrewnorcross.com/yoga/locations/tampa/
Current code:
<?php
global $post;
$offerings = get_post_meta($post->ID, "evol_offerings_select", false);
if ($offerings[0]=="") { ?>
<!-- If there are no custom fields, show nothing -->
<?php } else { ?>
<div class="offerings">
<h3>offerings:</h3>
<?php
foreach ($offerings as $offering) {
if ($offering["hot"]==true) {
echo "<div class='single_offering'>";
echo "<h3>Hot 90</h3>";
echo "<p class='class_info'>temp: 105° time: 90 min</p>";
echo '</div>';
}
if ($offering["flow"]==true) {
echo "<div class='single_offering'>";
echo "<h3>Flow 75</h3>";
echo "<p class='class_info'>temp: 80° time: 75 min</p>";
echo '</div>';
}
if ($offering["warm"]==true) {
echo "<div class='single_offering'>";
echo "<h3>Warm 60</h3>";
echo "<p class='class_info'>temp: 90° time: 60 min</p>";
echo '</div>';
}
if ($offering["chill"]==true) {
echo "<div class='single_offering'>";
echo "<h3>Chill 30</h3>";
echo "<p class='class_info'>temp: 75-80° time: 30 min</p>";
echo '</div>';
}
if ($offering["kids"]==true) {
echo "<div class='single_offering'>";
echo "<h3>Kids Class</h3>";
echo "<p class='class_info'>temp: comfy time: 60 min</p>";
echo '</div>';
}
} ?>
Your foreach is the problem. You’re iterating once for each offering, yet you’re evaluating ALL offering values in the array each time. So your PHP is dutifully printing all available offerings * the number of offerings. I’d say go without the foreach() and see what happens.
EDIT 1
Also please note, you’re casting strings to arrays. You’re trying to look up values in an array based on an invalid key. $offerings[“hot”] – $offerings[“kids”] don’t exist, as far as PHP is concerned. $offerings[0] – $offerings[4] DO exist.
EDIT 2
Okay, you want the following in place of your foreach():
etc.
EDIT 3
Or, if I think about it further, you could stick with the foreach() in the following manner:
EDIT 4
If you were dealing with a larger data set, I would probably go with the switch() version, as the in_array() calls scan the whole array each time, which could get expensive, while the foreach() will simply iterate through the array and move the array pointer with each iteration. Your call, but I’m thinking that would be the way to go in order to future-proof and performance-proof. (The performance hit on a 5 value array for in_array() is likely minuscule, but these things add up.)
PHP will evaluate
as true for a key so long as the value for that key is not empty, so I suspect get_post_meta() is returning something (“null” or even ” “) for those values.
try using
for strict equality and it should work.
Can you share the code that you are using to register those boxes? Sounds like each time you are saving, each value is overwriting each other. You will need to save the values into an array.
Try this:
What I’ve done here is:
*Actually, that’s what I’ve gleaned from your suggested requirements – that a given program has a set of offered activities, and that these activities need to be enumerated… or something. Maybe you could be more clear.