Modifying lists (checkbox multi-value) to display value and label differently

5 posts by 2 authors in: Forums > CMS Builder
Last Post: July 10, 2013   (RSS)

By gregThomas - July 10, 2013

Hi Meg,

What about doing something like this:

<?php

  $ingredients      = array_combine($record['ingredients:values'], $record['ingredients:labels']);
  $ingredientsCount = count($ingredients);
  $i                = 1; 

?>
<?php foreach($ingredients as $value => $label): ?>
  <a href="#" class="<?php echo $value; ?>"><?php echo $label; ?></a> <?php if($i != $ingredientsCount){ echo ','; } ?>
  <?php $i++; ?>
<?php endforeach; ?>

So I've combined the ingredients into one array of values and labels using array_combine (http://php.net/manual/en/function.array-combine.php), then set a count of the array to to ingredientsCount. 

Then a foreach loop goes through each item creating the link using the value and label from the array. Finally I've added an if statement to the loop that will display a comma unless it's the final item in the loop.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By meg - July 10, 2013

Sounds good, but I plugged it in and got these errors.

At the top: Notice: Undefined variable: record in /home/content/88/11310188/html/menu-and-pricing.php on line 92 Notice: Undefined variable: record in /home/content/88/11310188/html/menu-and-pricing.php on line 92 Warning: array_combine() expects parameter 1 to be array, null given in /home/content/88/11310188/html/menu-and-pricing.php on line 92

Where the ingredients are suppose to be listed: 

Warning: Invalid argument supplied for foreach() in /home/content/88/11310188/html/menu-and-pricing.php on line 169 

Any thoughts?

By gregThomas - July 10, 2013

Hi Meg,

You are getting the first error because where you have added the array_combine code the $record variable hasn't been created yet, then the second foreach error is being created as the ingredients array hasn't been created. Try adding the entire block of code where you want the links to display, as the $record variable should definitely have been created at that point. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By meg - July 10, 2013

Works like a charm - thank you!!