PHP 实现无限极栏目分类
首先,创建一个DB
CREATE TABLE IF NOT EXISTS `class` ( `id` mediumint(6) NOT NULL AUTO_INCREMENT, `title` varchar(30) NOT NULL, `pid` mediumint(6) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8
然后插入数据
INSERT INTO `class` VALUES (1, '品牌女装', 0); INSERT INTO `class` VALUES (2, '精品男装', 0); INSERT INTO `class` VALUES (3, '连衣裙', 1); INSERT INTO `class` VALUES (4, '旗袍', 1); INSERT INTO `class` VALUES (5, '燕尾服', 2);
数据的结构能够自己去设计
这里采用递归的方法去实现无限极栏目分类
<?php $con = mysql_connect("localhost","root","123456"); mysql_query("set names 'utf8'"); mysql_select_db("test"); function get_str($id = 0) { global $str; $sql = "select id,title from class where pid= $id"; $result = mysql_query($sql); if($result && mysql_affected_rows()){ $str .= '<ul>'; while ($row = mysql_fetch_array($result)) { $str .= "<li>" . $row['id'] . "--" . $row['title'] . "</li>"; get_str($row['id']); } $str .= '</ul>'; } return $str; } echo get_str(0); ?>
返回一个数组
<?php $con = mysql_connect("localhost","root","123456"); mysql_query("set names 'utf8'"); mysql_select_db("test"); function get_array($id=0){ $sql = "select id,title from class where pid= $id"; $result = mysql_query($sql); $arr = array(); if($result && mysql_affected_rows()){ while($rows=mysql_fetch_assoc($result)){ $rows['list'] = get_array($rows['id']); $arr[] = $rows; } return $arr; } } $list = get_array(0); var_dump($list);?>
第三种分类;将同一个字段的一样的值归为一类,
function Staff_Pay_nen($type,$emp_cd){ $con=dbcon9(); $sql = " "; $sql .= " SELECT "; $sql .= " pay_nen "; if($type == "haken"){ //派遣 $sql .= " FROM "; $sql .= " tbl_staff_gensen "; $sql .= " WHERE "; $sql .= " staff_cd = '".$emp_cd."' AND "; $sql .= " kokyaku_cd= '000000' AND "; $sql .= " sagyo_type='0' AND "; }elseif($type == "syoukai"){ //日々紹介 $sql .= " ,kokyaku_cd "; $sql .= " FROM "; $sql .= " tbl_staff_gensen "; $sql .= " WHERE "; $sql .= " staff_cd = '".$emp_cd."' AND "; $sql .= " sagyo_type ='1' AND "; $sql .= " kokyaku_cd <> '000000' AND "; $sql .= " pay_nen is not null AND "; } if(1 <= date("n",time()) && date("n",time()) <=6){ //処理日前年と処理日当年 $sql .= " pay_nen in (to_char(now(),'yyyy'),to_char(now() + '-1 year','yyyy')) "; }else if(7 <= date("n",time()) && date("n",time()) <=12){ //処理日当年のみ $sql .= " pay_nen in (to_char(now(),'yyyy')) "; } $sql .= " ORDER BY "; $sql .= " pay_nen desc "; $sql .= " ,kokyaku_cd ASC "; $result=pg_query($sql); $i = 0; $test_day = ""; $pay_nen = array(); if($type == "haken"){ //派遣 while($row=pg_fetch_assoc($result)){ $pay_nen[$i] = $row['pay_nen']; $i++; } }elseif($type == "syoukai"){ //日々紹介 while($row=pg_fetch_assoc($result)){ if($test_day == $row['pay_nen']){ $pay_nen[$row['pay_nen']] .= ",".$row['kokyaku_cd']; }else{ $pay_nen[$row['pay_nen']] = $row['kokyaku_cd']; } $test_day = $row['pay_nen']; } } return $pay_nen; }
这样的显示如下
品牌女装 连衣裙 旗袍 精品男装 燕尾服