Sorting Complicated Lists with PHP When A Database Isn’t Necessary
Websites commonly have content that's sorted in some fashion. For larger projects, utilizing a database may be optimal. But what about those smaller projects? When displaying a short list of advisory board members, for example, I typically go straight to HTML and my grand knowledge of the ABCs. That may work in the short run, but eventually mistakes happen. We can minimize the risk by leveraging PHP for sorting complicated lists.
Example Problem
The following HTML code shows how the advisory board is currently listed:
<h1>Advisory Board</h1>
<ul>
<li>Jake Bible, Smith & Wesson</li>
<li>Rachel Caprese, Mechanical Engineering Corp.</li>
<li>Calvin Hubbard, Consultant</li>
<li>Connell Kirkland, Moving Mountains Inc.</li>
<li>John Michael Smith, Smith and Sons</li>
</ul>
Since the content doesn't change often, maybe once or twice per year, it could be managed manually. However, the infrequent updates could lead to user error. It's easy to forget that a list is sorted when there are many other projects needing attention and it's been a few years since last looking at the advisory board page.
Sorting by Last Name
Instead, let's create a multidimensional associative array for the information.
<?php
$boardMembers = array(
array(
'firstName'=>'Jake',
'lastName'=>'Bible',
'organization'=>'Smith & Wesson'
),
array(
'firstName'=>'Rachel',
'lastName'=>'Caprese',
'organization'=>'Mechanical Engineering Corp.'
),
array(
'firstName'=>'Calvin',
'lastName'=>'Hubbard',
'organization'=>'Consultant'
),
array(
'firstName'=>'Connell',
'lastName'=>'Kirkland',
'organization'=>'Moving Mountains Inc.'
),
array(
'firstName'=>'John Michael',
'lastName'=>'Smith',
'organization'=>'Smith and Sons'
)
);
?>
The uasort() function can now be employed to sort by last name.
<?php
function cmp($a, $b) {
if($a['lastName'] == $b['lastName']) {
return 0;
}
return ($a['lastName'] < $b['lastName']) ? -1 : 1;
}
uasort($boardMembers, 'cmp');
?>
That's pretty much it, except for displaying the list.
<?php
print '<h1>Advisory Board</h1>';
print '<ul>';
foreach($boardMembers as $currMember) {
print '<li>' . $currMember['firstName'] . ' ' . $currMember['lastName'];
print ', ' . $currMember['organization'] . '</li>';
}
print '</ul>';
?>
Sorting by Last and First Name
Well, what happens if there is more than one Smith on the advisory board? The beauty of using uasort() and a multidimensional associative array is that we essentially have a mini database. A set of "fields" are available for sorting.
<?php
function cmp($a, $b) {
if($a['lastName'] == $b['lastName']) {
if($a['firstName'] == $b['firstName']) {
return 0;
}
return($a['firstName'] < $b['firstName']) ? -1 : 1;
}
return($a['lastName'] < $b['lastName']) ? -1 : 1;
}
?>
Final Code
<?php
//INITIALIZE THE LIST OF BOARD MEMBERS
$boardMembers = array(
array(
'firstName'=>'Jake',
'lastName'=>'Bible',
'organization'=>'Smith & Wesson'
),
array(
'firstName'=>'Rachel',
'lastName'=>'Caprese',
'organization'=>'Mechanical Engineering Corp.'
),
array(
'firstName'=>'Calvin',
'lastName'=>'Hubbard',
'organization'=>'Consultant'
),
array(
'firstName'=>'Connell',
'lastName'=>'Kirkland',
'organization'=>'Moving Mountains Inc.'
),
array(
'firstName'=>'John Michael',
'lastName'=>'Smith',
'organization'=>'Smith and Sons'
)
);
//COMPARISON FUNCTION USED FOR SORTING
function cmp($a, $b) {
if($a['lastName'] == $b['lastName']) {
if($a['firstName'] == $b['firstName']) {
return 0;
}
return($a['firstName'] < $b['firstName']) ? -1 : 1;
}
return($a['lastName'] < $b['lastName']) ? -1 : 1;
}
//SORT THE LIST
uasort($boardMembers, 'cmp');
//DISPLAY BOARD MEMBERS
print '<h1>Advisory Board</h1>';
print '<ul>';
foreach($boardMembers as $currMember) {
print '<li>' . $currMember['firstName'] . ' ' . $currMember['lastName'];
print ', ' . $currMember['organization'] . '</li>';
}
print '</ul>';
?>
Conclusion
With the new setup, there's no need to worry about the list being improperly sorted. Updates just need to be applied to the array and PHP will handle the rest.
0 Comments
There are currently no comments.
Leave a Comment