Sorting by name in options table

7 posts by 3 authors in: Forums > CMS Builder
Last Post: May 19, 2012   (RSS)

By nmsinc - March 16, 2012 - edited: March 16, 2012

I need to sort a list of names from an option table below and having trouble - anyone with ideas

<?php if ($CURRENT_USER['isAdmin']): ?>
Insurance Company:
<select name="member_company" onchange = "populateAdjusterList(this, 'insurance');">
<option value="">All</option>
<?php foreach (mysql_select("member_companies", "member_type = 'Insurance'") as $insurance): ?>
<option value = "<?php echo $insurance['num'];?>"><?php echo $insurance['member_company_name'];?></option>
<?php endforeach ?>
</select>


Thanks - nmsinc
nmsinc

Re: [nmsinc] Sorting by name in options table

By (Deleted User) - March 16, 2012

Hi nmsinc,

One method would be to use getRecords to load the records and set 'orderBy' to "member_company_name ASC":
<?php
// Get records
list($memberCompanies, $memberCompaniesMetaData) = getRecords(array(
'tableName' => 'member_companies',
'orderBy' => 'member_company_name ASC',
));
?>


<?php
// loop over records and output options
foreach ( $memberCompanies as $insurance):
?>
<option value = "<?php echo $insurance['num'];?>"><?php echo $insurance['member_company_name'];?></option>
<?php endforeach ?>


This will output the options in alphabetical order.

Hope this helps,

Tom

Re: [Tom P] Sorting by name in options table

By nmsinc - March 16, 2012

Hit Tom,

Thanks for the idea, however, I'm using this with a custom ajax script, with the mysql_select command to populate the drop-down selection so the sort on record load serves no value here! Any other suggestions?

Thanks - nmsinc
nmsinc

Re: [nmsinc] Sorting by name in options table

By (Deleted User) - March 16, 2012

Hi nmsinc,

Try this instead:

<?php if ($CURRENT_USER['isAdmin']): ?>
Insurance Company:
<select name="member_company" onchange = "populateAdjusterList(this, 'insurance');">
<option value="">All</option>
<?php $companiesList = mysql_select("member_companies", "member_type = 'Insurance'"); ?>
<?php $numToName = array(); ?>
<?php foreach ( $companiesList as $company): ?>
<?php $numToName[$company['num']] = $company['member_company_name']; ?>
<?php endforeach; ?>
<?php asort($companiesList); ?>
<?php foreach ( $companiesList as $insurance): ?>
<option value = "<?php echo $insurance['num'];?>"><?php echo $insurance['member_company_name'];?></option>
<?php endforeach ?>
</select>
<?php endif; ?>


It just creates a temporary array of num to company name, sorts is alphabetically using asort (maintaining the key=>value assocation) and then runs another foreach to output the results.

Is this a workable solution? If not, it might be worthwhile to see what can be done with the ajax to improve things.

Let me know,

Tom

Re: [nmsinc] Sorting by name in options table

By robin - May 18, 2012

Hey nmsinc,

Here is another trick you can try, we can sort it right at the source.

You can actually pass more than just the WHERE clause to mysql_select. Something like this might be what you need:

mysql_select("member_companies", "member_type = 'Insurance' ORDER BY member_company_name")

Hope that helps,
Robin
Robin
Programmer
interactivetools.com

Re: [robin] Sorting by name in options table

By nmsinc - May 19, 2012

Thanks Robin, worked perfect!

nmsinc
nmsinc