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.
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>
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
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
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?
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?
Hi sjkelly,
I think that the usort() answer from Petah in this StackOverFlow question is what you need.
Bob
I think that the usort() answer from Petah in this StackOverFlow question is what you need.
Bob
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?
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?
Hi sjkelley,
Not tested but (assuming that you have PHP 5.3+ I think that you'd need this in a Custom Code action:
Bob
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
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:
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?
<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?
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:
or this to do a 'natural' sort:
or this to do a case-insensitive 'natural' sort:
Bob
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
This topic is locked and no more replies can be posted.