create table onepiece( id int auto_increment, pid int not null, name varchar(225) not null, primary key(id) );
insert onepiece values (1,0,'海军'), (2,0,'海贼'), (3,0,'革命军'), (4,1,'青雉'), (5,1,'赤犬'), (6,1,'黄猿'), (7,2,'四皇'), (8,2,'七武海'), (9,2,'草帽海贼团'), (10,9,'索隆'), (11,7,'香克斯'), (12,8,'多弗朗明哥'), (13,8,'克洛克达尔');
<?php
class Unlimited{
protected $mysqli;
public function __construct($config){
$this->mysqli=new mysqli($config['host'],$config['user'],$config['pwd']);
$this->mysqli->select_db($config['db']);
$this->mysqli->set_charset('utf8');
if ($this->mysqli->connect_errno) {
echo $this->mysqli->connect_error;
}
}
private function getList($pid=0,&$result=array(),$spac=0){
$spac=$spac+2;
$sql="select * from onepiece where pid={$pid}";
$rs=$this->mysqli->query($sql);
while($row=$rs->fetch_assoc()) {
$row['name']=str_repeat('  ',$spac).$row['name'];
$result[]=$row;
$this->getList($row['id'],$result,$spac);
}
return $result;
}
/**
* 展现下拉列表式分类
* @return [type]
*/
public function displayList(){
$rs=$this->getList();
$str="<select name='cate'>";
foreach ($rs as $key => $val) {
$str.="<option >{$val['name']}</option>";
}
$str.="</select>";
return $str;
}
private function getLink($cid,&$result=array()){
$sql="select * from onepiece where id={$cid}";
$rs=$this->mysqli->query($sql);
if($row=$rs->fetch_assoc()){
$result[]=$row;
$this->getLink($row['pid'],$result);
}
return array_reverse($result);
}
/**
* 展现导航Link
* @param [type] $cid [description]
* @return [type] [description]
*/
public function displayLink($cid){
$rs=$this->getLink($cid);
$str='';
foreach ($rs as $val) {
$str.="<a href=''>{$val['name']}</a>>";
}
return $str;
}
/**
* 增加分类
* @param [type] $pid 父类id
* @param [type] $name 本类名
*/
public function addNodes($pid,$name){
$sql="insert into onepiece values('',{$pid},'".$name."')";
if($this->mysqli->query($sql)){
return true;
}
}
/**
* 删除分类
* @param [type] $id 本类id
* @return [type]
*/
public function deleteNodes($id){
$sql="select * from onepiece where pid ={$id}";
$rs=$this->mysqli->query($sql);
if($row=$rs->fetch_assoc()){
$mes="还有子元素,请勿删除";
}else{
$sql="delete from onepiece where id={$id}";
if($this->mysqli->query($sql)){
$mes="删除成功";
}
}
return $mes;
}
}
CREATE TABLE IF NOT EXISTS `category` ( `categoryId` smallint(5) unsigned NOT NULL AUTO_INCREMENT, `parentId` smallint(5) unsigned NOT NULL DEFAULT '0', `categoryName` varchar(50) NOT NULL, PRIMARY KEY (`categoryId`) ) ;
INSERT INTO `category` (`categoryId`, `parentId`, `categoryName`) VALUES (1, 0, 'php'), (2, 0, 'java'), (3, 0, 'c/c++'), (4, 1, 'php基础'), (5, 1, 'php开源资料'), (6, 1, 'php框架'), (7, 2, 'java Se'), (8, 2, 'java EE'), (9, 2, 'java Me'), (10, 3, 'c/c++基础编程'), (11, 3, 'c/c++系统开发'), (12, 3, 'c嵌入式编程'), (13, 3, 'c++应用开发'), (14, 13, 'c++桌面应用开发'), (15, 13, 'c++游戏开发');
<?php
/*
php无限极分类
*/
//获取某分类的直接子分类
function getSons($categorys,$catId=0){
$sons=array();
foreach($categorys as $item){
if($item['parentId']==$catId)
$sons[]=$item;
}
return $sons;
}
//获取某个分类的所有子分类
function getSubs($categorys,$catId=0,$level=1){
$subs=array();
foreach($categorys as $item){
if($item['parentId']==$catId){
$item['level']=$level;
$subs[]=$item;
$subs=array_merge($subs,getSubs($categorys,$item['categoryId'],$level+1));
}
}
return $subs;
}
//获取某个分类的所有父分类
//方法一,递归
function getParents($categorys,$catId){
$tree=array();
foreach($categorys as $item){
if($item['categoryId']==$catId){
if($item['parentId']>0)
$tree=array_merge($tree,getParents($categorys,$item['parentId']));
$tree[]=$item;
break;
}
}
return $tree;
}
//方法二,迭代
function getParents2($categorys,$catId){
$tree=array();
while($catId != 0){
foreach($categorys as $item){
if($item['categoryId']==$catId){
$tree[]=$item;
$catId=$item['parentId'];
break;
}
}
}
return $tree;
}
//测试 部分
$pdo=new PDO('mysql:host=localhost;dbname=test','root','8888');
$stmt=$pdo->query("select * from category order by categoryId");
$categorys=$stmt->fetchAll(PDO::FETCH_ASSOC);
$result=getSons($categorys,1);
foreach($result as $item)
echo $item['categoryName'].'<br>';
echo '<hr>';
$result=getSubs($categorys,0);
foreach($result as $item)
echo str_repeat(' ',$item['level']).$item['categoryName'].'<br>';
echo '<hr>';
$result=getParents($categorys,7);
foreach($result as $item)
echo $item['categoryName'].' >> ';
echo '<hr>';
$result=getParents2($categorys,15);
foreach($result as $item)
echo $item['categoryName'].' >> ';
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有