分类表数据表参考:
CREATE TABLE `mf_sort` (
`sortid` smallint(3) unsigned NOT NULL auto_increment,
`main` tinyint(2) unsigned NOT NULL default '0',
`parentid` smallint(3) unsigned NOT NULL default '0',
`layer` smallint(3) unsigned NOT NULL default '0',
`orders` tinyint(2) unsigned NOT NULL default '0',
`sort` varchar(100) NOT NULL default '',
PRIMARY KEY (`sortid`),
KEY `main` (`main`,`parentid`,`layer`,`orders`)
) ;
#sortid 类别编号
#main 根分类
#parentid 父ID
#layer 分类等级
#orders 排列顺序
#输出的时候 order by `main` asc, `orders` asc
两个主要的函数
function get_main_layer_orders($parentid)
{
global $x_db;
$sql = "select `main`, `layer`, `orders` ";
$sql .= "from `mf_sort` ";
$sql .= "where `sortid`='$parentid'";
$x_db->exec($sql);
$data = $x_db->get_data();
$layer = $data[0]['layer'] 1;
$main = $data[0]['main'];
$orders = $data[0]['orders'];
$sql = "select `sortid` from `mf_sort` ";
$sql .= "where `parentid`='$parentid'";
$x_db->exec($sql);
$n = $x_db->n;
if ($n>0)
{
$lastid = $parentid;
get_lastid($lastid);
$sql = "select `orders` from `mf_sort` ";
$sql .= "where `sortid`='$lastid'";
$x_db->exec($sql);
$data = $x_db->get_data();
$orders = $data[0][0];
$sql = "update `mf_sort` ";
$sql .= "set `orders`=`orders` 1 ";
$sql .= "where `orders`>$orders and `main`='$main'";
$x_db->exec($sql);
$orders = $orders 1;
return array($main, $layer, $orders);
}
else
{
$sql = "update `mf_sort` ";
$sql .= "set `orders`=`orders` 1 ";
$sql .= "where `orders`>$orders and `main`='$main'";
$x_db->exec($sql);
return array($main, $layer, $orders 1);
}
}
//取得最后一个有效sortid
function get_lastid(&$parentid)
{
global $x_db;
$pre = $parentid;
$sql = "select max(`sortid`) as `id` ";
$sql .= "from `mf_sort` ";
$sql .= "where `parentid` = '$parentid'";
$x_db->exec($sql);
$data = $x_db->get_data();
$id = $data[0]['id'];
if (empty($id))
{
$parentid = $pre;
}