note 68864 added to ref.mysql
| From: | davesteinbatyahoodotcom at osu1 dot php dot net | Date: | Sun, 13 Aug 2006 22:32:11 +0000 |
| Subject: | note 68864 added to ref.mysql | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115878@lists.php.net to get a copy of this message | ||
I made this function to reduce DB calls. You can store Mysql results in a session var and sort the
results on any column. Might work nice in an AJAX app.
<?
function mysql_sort($results, $sort_field, $dir="ASC") {
$temp_array = array();
$i=0;
foreach ($results as $res) {
$temp_array[$i] = $res[$sort_field];
$i++;
}
if ($dir=="ASC") {
asort($temp_array);
} else {
arsort($temp_array);
}
$new_results = array();
$i=0;
foreach($temp_array as $k => $v) {
$new_results[$i] = $results[$k];
$i++;
}
ksort($new_results);
return $new_results;
}
//use
if (count($_SESSION["res"])==0) {
$_SESSION["res"] = [GET DATABASE RESULTS HOWEVER YOU MAY]
}
$_SESSION["res"] = mysql_sort($_SESSION["res"], $_REQUEST["sort"],
$_REQUEST["dir"]);
?>
<table>
<tr>
<td><a href="page.php?sort=f_name&dir=<? echo
($_REQUEST["dir"]=="ASC") ? "DESC":"ASC";
?>">First</a></td>
<td><a href="page.php?sort=l_name&dir=<? echo
($_REQUEST["dir"]=="ASC") ? "DESC":"ASC";
?>">Last</a></td>
</tr>
<? foreach ($_SESSION["res"] as $r) {?>
<tr>
<td><?=$r["f_name"]?></td>
<td><?=$r["l_name"]?></td>
</tr>
<? } ?>
</table>
----
Server IP: 216.194.113.175
Probable Submitter: 71.106.100.122
----
X-Spam-Status: No, hits=4.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_30_40,HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/ref.mysql.php
Edit -- https://master.php.net/note/edit/68864
Del: integrated -- https://master.php.net/note/delete/68864/integrated
Del: useless -- https://master.php.net/note/delete/68864/useless
Del: bad code -- https://master.php.net/note/delete/68864/bad+code
Del: spam -- https://master.php.net/note/delete/68864/spam
Del: non-english -- https://master.php.net/note/delete/68864/non-english
Del: in docs -- https://master.php.net/note/delete/68864/in+docs
Del: other reasons-- https://master.php.net/note/delete/68864
Reject -- https://master.php.net/note/reject/68864
Search -- https://master.php.net/manage/user-notes.php