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 meg - July 10, 2013

With a multi-listing record, I created a list (checkbox multi-value) that's referencing another database table, with a different value and label. I'm happy with the results displaying the value in a comma delineated view, but not with the label. 

This is how it's currently displaying: 

PHP

<a href="#" class="<?php echo join(', ', $record['ingredients:labels']); ?>"><?php echo join(', ', $record['ingredients:values']); ?></a>

HTML

<a href="#" class="coconut, bananas, dates, vanilla, cashew">Thai Coconut, Banana, Dates, Vanilla, Cashew Milk</a>

Essentially, I need the results to view as such:

<a href="#" class="coconut">Thai Coconut</a>, <a href="#" class="bananas">Banana</a>, <a href="#" class="dates">Dates</a>, <a href="#" class="vanilla">Vanilla</a>, <a href="#" class="cashew">Cashew Milk</a>

What modifications do I need to make to the PHP so the HTML displays this way? Thanks so much!

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