Forums

DB Multi Record Loader: How do I alphabetize list?

sjkelley 15 Feb, 2013
I am using Chronoforms 4 and the DB Multi Record Loader to display a table of records. Everything is working great except I can't figure out how to alphabetize the table based on the "Company Name" field — in my code below that is $detail['q1_company_name']

Here is the Custom Code I'm using. I've also attached a screenshot of what the front-end output looks like.

<h1>Edit GOSheet Records</h1><table>
<tr>
<td width="250"><b>Company</b></td>
<td width="200"><b>Product Type</b></td>
<td width="200"><b>Plan Type</b></td>
<td width="225"><b>Movement Type</b></td>
<td width="200"><b>Date Created</b></td>
<td width="200"><b>Last Modified</b></td>
<td width="100"><b>User ID</b></td>
</tr>
<?php
foreach($form->data['UserDetails'] as $detail):
?>
<tr>
<td width="500"><a href="index.php?option=com_chronoforms&chronoform=trexadd&token=<?php echo $detail['cf_uid']; ?>"><?php echo $detail['q1_company_name']; ?></a>     </td>

<td width="200"><?php echo $detail['q1_product_type']; ?></td>
<td width="200"><?php echo $detail['q1_plan_type']; ?></td>
<td width="225"><?php echo $detail['q1_movement_type']; ?></td>
<td width="200"><?php echo $detail['cf_created']; ?></td>
<td width="200"><?php echo $detail['cf_modified']; ?></td>
<td width="100"><?php echo $detail['cf_user_id']; ?></td>
</tr>
<?php
endforeach;
?>
</table>
GreyHead 15 Feb, 2013
Hi Sjkelley,

Try the Order Fields box on the Data Displayer tab of the DB Multi-Record Loader - I think that will do it. If not it's possible to sort using PHP.

Bob
sjkelley 15 Feb, 2013
Bob,

Thanks for the quick response. I appreciate it.

In fact I had attempted to use the Order Fields approach. I added "q1_company_name" (the name of the field I want to the records to be alphabetized by — see attached screengrab. But it didn't do anything.

I also had fiddled around with doing it via PHP in the Custom Code field but for the life of me I couldn't figure out the proper way to use the asort function in this context.

Can you provide an example of the proper usage in this situation?
sjkelley 05 Mar, 2013
Thanks for the tip. However I took a look at that link and I'll be honest: I still can't seem to figure out how to use the usort function properly in this circumstance.

Again, all I'm trying to do is have all of my records sorted alphabetically by the field called "q1_company_name" (see my original post above for details).

What am I missing?
GreyHead 06 Mar, 2013
Hi sjkelley,

Not tested but (assuming that you have PHP 5.3+ I think that you'd need this in a Custom Code action:
<?php
usort($form->data['UserDetails'], function($a, $b) {
    return $a['q1_company_name'] - $b['q1_company_name'];
});
?>

Bob
sjkelley 06 Mar, 2013
Thanks for the insight. I think I'm 90% there. I added the code you suggested so that the Custom Code I'm using is as follows:

<h1>Edit GOSheet Records</h1>
<table>
<tr>
<td width="250"><b>Company</b></td>
<td width="200"><b>Product Type</b></td>
<td width="200"><b>Plan Type</b></td>
<td width="225"><b>Movement Type</b></td>
<td width="200"><b>Date Created</b></td>
<td width="200"><b>Last Modified</b></td>
<td width="100"><b>User ID</b></td>
</tr>

<?php
usort($form->data['UserDetails'], function($a, $b) {
    return $a['q1_company_name'] - $b['q1_company_name'];
});
?>

<?php
foreach($form->data['UserDetails'] as $detail):
?>

<tr>
<td width="500"><a href="index.php?option=com_chronoforms&chronoform=trexadd&token=<?php echo $detail['cf_uid']; ?>">
<?php echo $detail['q1_company_name']; ?></a>     </td>

<td width="200"><?php echo $detail['q1_product_type']; ?></td>
<td width="200"><?php echo $detail['q1_plan_type']; ?></td>
<td width="225"><?php echo $detail['q1_movement_type']; ?></td>
<td width="200"><?php echo $detail['cf_created']; ?></td>
<td width="200"><?php echo $detail['cf_modified']; ?></td>
<td width="100"><?php echo $detail['cf_user_id']; ?></td>
</tr>
<?php
endforeach;
?>
</table>


The function did change the ordering of the table, so it's sorting them in some fashion. However they're still not alphabetized by the "q1_company_name" field.

Do I have the usort code in the wrong position?
GreyHead 07 Mar, 2013
Hi sjkelley,

Hmm my fault for rushing :-( The function is good to sort numbers but not strings: try this version instead for a straight string comparison:
<?php
usort($form->data['UserDetails'], function($a, $b) {
    return strcmp($a['q1_company_name'], $b['q1_company_name']);
});
?>

or this to do a 'natural' sort:
<?php
usort($form->data['UserDetails'], function($a, $b) {
    return strnatcmp(a['q1_company_name'], $b['q1_company_name']);
});
?>

or this to do a case-insensitive 'natural' sort:
<?php
usort($form->data['UserDetails'], function($a, $b) {
    return strnatcasecmp(a['q1_company_name'], $b['q1_company_name']);
});
?>

Bob
sjkelley 07 Mar, 2013
That worked great! Thanks so much!

To be honest, I'm at best a tinkerer when it comes to PHP so this working example will really help me understand how to get the most out of Chronoforms moving forward.
This topic is locked and no more replies can be posted.