Sort list bay label

2 posts by 2 authors in: Forums > CMS Builder
Last Post: July 24, 2012   (RSS)

By theclicklab - July 24, 2012

Hi there, I have a dropdown list that defaults to sort by value. How do I change this to sort by label?

Example code:
<?php

$schema = loadSchema('product_database');
$fieldSchema = $schema['category'];
$fieldOptions = getListOptionsFromSchema($fieldSchema);

foreach ($fieldOptions as $valueAndLabel) {
list($value,$label) = $valueAndLabel;
$encodedValue = htmlspecialchars($value);
$encodedLabel = htmlspecialchars($label);
print "<option value=\"$encodedValue\">$encodedLabel</option>\n";
}
?>


Thanks

Re: [theclicklab] Sort list bay label

By Jason - July 24, 2012

Hi,

The function getListOptions() returns an array where the index is the option value, and the value is the option label. Once you have this, you can use the PHP function asort() to sort the array and output.

for example:

<?php
$listOptions = getListOptions('product_database', 'category');
asort($listOptions);
?>

<?php foreach ($listOptions as $value => $label): ?>
<option value = "<?php echo htmlspecialchars($value);?>"> <?php echo htmlspecialchars($label);?> </option>
<?php endforeach ?>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/