note 26281 modified in function.usort by thiago
| From: | thiago@php.net | Date: | Tue, 22 Jul 2008 19:18:50 +0000 |
| Subject: | note 26281 modified in function.usort by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142571@lists.php.net to get a copy of this message | ||
If you've used ADO before, you may have used the Sort property on a recordset. It's very
powerful - you can add a nice ORDER BY clause after getting the recordset from the database.
It's especially helpful if you want to show a list on a web page and make the column headings
links that cause the list to resort on that column.
I wanted to do the same thing with mysql recordsets, but it looks like you have to have the ORDER BY
in your query. I re-worked the example from johan_land at yahoo dot com above for sorting
multidimensional arrays. When I get a mysql recordset, I create an array with all of the records
like this:
<?php
$aaRows[] = array();
if (mysql_num_rows($rsRows) > 0)
while ($aRow = mysql_fetch_array($rsRows, MYSQL_ASSOC))
$aaRows[] = $aRow;
?>
At this point $aaRows is an array of arrays - it's a numeric-indexed array containing records
from the recordset, which themselves are associative arrays. The following code takes the array of
records as the first parameter, and an array of fields to sort - each field is an associative array,
where 'name' is the db field name, and 'dir' is the direction to sort. If dir
is 'DESC' (case-insensitive), it will sort descending. Any other value (including not
setting one) will cause it to sort ascending.
<?php
function sortRows(&$a_aaRows, $a_aaSortCriteria)
{
GLOBAL $g_aaSortArray;
function compare($a_aRow1, $a_aRow2, $a_lField = 0)
{
GLOBAL $g_aaSortArray;
$lCompareVal = 0;
if ($a_lField < count($g_aaSortArray))
{
$sSortFieldName = $g_aaSortArray[$a_lField]['name'];
$sSortFieldDir = $g_aaSortArray[$a_lField]['dir'];
$vValue1 = eval('return $a_aRow1[' . $sSortFieldName . '];');
$vValue2 = eval('return $a_aRow2[' . $sSortFieldName . '];');
if ($vValue1 == $vValue2)
$lCompareVal = compare($a_aRow1, $a_aRow2, $a_lField + 1);
else
{
$lCompareVal = $vValue1 > $vValue2 ? 1 : -1;
if (strtolower(substr($sSortFieldDir, 0, 4)) == 'desc')
$lCompareVal = -$lCompareVal;
}
}
return $lCompareVal;
}
$g_aaSortArray = $a_aaSortCriteria;
usort($a_aaRows, 'compare');
}
?>
When I call it it looks something like this:
<?php
sortRows($aaRows, array(array('name' => 'STATE', 'dir' =>
'ASC'), array('name' => 'CITY', 'dir' =>
'DESC')));
?>
--was--
If you've used ADO before, you may have used the Sort property on a recordset. It's very
powerful - you can add a nice ORDER BY clause after getting the recordset from the database.
It's especially helpful if you want to show a list on a web page and make the column headings
links that cause the list to resort on that column.
I wanted to do the same thing with mysql recordsets, but it looks like you have to have the ORDER BY
in your query. I re-worked the example from johan_land at yahoo dot com above for sorting
multidimensional arrays. When I get a mysql recordset, I create an array with all of the records
like this:
$aaRows[] = array();
if (mysql_num_rows($rsRows) > 0)
while ($aRow = mysql_fetch_array($rsRows, MYSQL_ASSOC))
$aaRows[] = $aRow;
At this point $aaRows is an array of arrays - it's a numeric-indexed array containing records
from the recordset, which themselves are associative arrays. The following code takes the array of
records as the first parameter, and an array of fields to sort - each field is an associative array,
where 'name' is the db field name, and 'dir' is the direction to sort. If dir
is 'DESC' (case-insensitive), it will sort descending. Any other value (including not
setting one) will cause it to sort ascending.
function sortRows(&$a_aaRows, $a_aaSortCriteria)
{
GLOBAL $g_aaSortArray;
function compare($a_aRow1, $a_aRow2, $a_lField = 0)
{
GLOBAL $g_aaSortArray;
$lCompareVal = 0;
if ($a_lField < count($g_aaSortArray))
{
$sSortFieldName = $g_aaSortArray[$a_lField]['name'];
$sSortFieldDir = $g_aaSortArray[$a_lField]['dir'];
$vValue1 = eval('return $a_aRow1[' . $sSortFieldName . '];');
$vValue2 = eval('return $a_aRow2[' . $sSortFieldName . '];');
if ($vValue1 == $vValue2)
$lCompareVal = compare($a_aRow1, $a_aRow2, $a_lField + 1);
else
{
$lCompareVal = $vValue1 > $vValue2 ? 1 : -1;
if (strtolower(substr($sSortFieldDir, 0, 4)) == 'desc')
$lCompareVal = -$lCompareVal;
}
}
return $lCompareVal;
}
$g_aaSortArray = $a_aaSortCriteria;
usort($a_aaRows, 'compare');
}
When I call it it looks something like this:
sortRows($aaRows, array(array('name' => 'STATE', 'dir' =>
'ASC'), array('name' => 'CITY', 'dir' =>
'DESC')));
http://php.net/manual/en/function.usort.php