我们今天为大家带来的是一个PHP数组排序函数array_multisort对数据库表格进行排序的例子,以此来进一步了解PHP语言的概念,增加我们的编码经验。
#t#函数的原形是array_multisort($sortKeyArray,$ascOrDesc,$sortArray),PHP数组排序函数array_multisort中的第一个参数是为了保持数组键值的对应关系需要构建的排序列数组,第二个参数是预定义的常量,SORT_ASC - 按照上升顺序排序,SORT_DESC - 按照下降顺序排序,第三个参数就是所要被排序的数组。还有一个可缺省的参数是排序的数据类型,这里略过。看一下下面的例子就会明了。
<?php
class Storage
{
function getSellList()
{
global $db;
$db->query("set names utf8");
$db->query("select * from sold_record");
while ($row=$db->get_array())
{
$array[]=$row;
}
$db->free();
return $array;
}
}
$storage=new Storage();
$sellList=$storage->getSellList();
foreach ($sellList as $key => $row)
{
$cust[$key] = $row['customer_id'];
$prod[$key] = $row['product'];
$pty[$key] = $row['ptype'];
$sdt[$key]=$row['sell_date'];
}
$asdes="yes";
if($_GET[up]=="yes")
{
$asdes="no";
$sort=SORT_DESC;
}
elseif($_GET[up]=="no")
{
$asdes="yes";
$sort=SORT_ASC;
}
switch ($_GET[order])
{
case "cname":
array_multisort($cust,$sort,$sellList);
break;
case "product":
array_multisort($prod,$sort,$sellList);
break;
case "ptype":
array_multisort($pty,$sort,$sellList);
break;
case "date":
array_multisort($sdt,$sort,$sellList);
break;
}
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
上面这段代码就是PHP数组排序函数array_multisort对表格排序的具体代码编写。