搜尋
首頁php教程PHP源码php 多种无限分类实例

<script>ec(2);</script>

创建数据库教程以及表:

CREATE DATABASE `sortclass`DEFAULT CHARSET utf8;
CREATE TABLE IF NOT EXISTS `class` (
`cid` mediumint(8) unsigned NOT NULL auto_increment,
`pid` mediumint(8) unsigned NOT NULL,
`cname` varchar(50) NOT NULL,
PRIMARY KEY (`cid`),
KEY `pid` (`pid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

处理文件示例:

header("Content-type: text/html; charset=utf-8");
//连接数据库
$link = mysql教程_connect('localhost','root','eric') or die(mysql_error());
mysql_select_db('sortclass',$link);
//无限分类类库
class SortClass{

var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $link;
var $table;
function SortClass($link, $table){
$this->setNode(0, -1, '顶极节点');
$this->link = $link;
$this->table = $table;
$node = array();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setNode($node['cid'],$node['pid'],$node['cname']);
}
}
function setNode ($id, $parent, $value){
$parent = $parent?$parent:0;
$this->data[$id] = $value;
$this->child[$id] = array();
$this->child[$parent][] = $id;
$this->parent[$id] = $parent;
$this->layer[$id] = !isset($this->layer[$parent])? 0 : $this->layer[$parent] + 1;
}
function getList (&$tree, $root= 0){
foreach ($this->child[$root] as $key=>$id){
$tree[] = $id;
if ($this->child[$id]) $this->getList($tree, $id);
}
}
function getValue ($id){return $this->data[$id];}
function getLayer ($id, $space = false){
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
}
function getParent ($id){return $this->parent[$id];}
function getParents ($id){
while ($this->parent[$id] != -1){
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
reset($parent);
return $parent;
}
function getChild ($id){return $this->child[$id];}
function getChilds ($id = 0){
$child = array($id);
$this->getList($child, $id);
return $child;
}
function addNode($name,$pid){
mysql_query("insert into $this->table (`pid`,`cname`) values ('$pid','$name')",$this->link);
}
function modNode($cid, $newName){
mysql_query("update $this->table set `cname`='$newName' where `cid` = $cid",$this->link);
}
function delNode($cid){
$allChilds = $this->getChilds($cid);
$sql ='';
if(empty($allChilds)){
$sql = "delete from $this->table where `cid` = $cid";
}else{
$sql = 'delete from '.$this->table.' where `cid` in ('.implode(',',$allChilds).','.$cid.')';
}
mysql_query($sql,$this->link);
}
function moveNode($cid, $topid){
mysql_query("update $this->table set `pid`=$topid where `cid` = $cid", $this->link);
}
}
//函数
function back(){
echo '';
exit;
}
//声成select
function makeSelect($array,$formName){
global $tree;
$select = '';
}
$tree = new SortClass($link,'`class`');
$op = !empty($_POST['op']) ? $_POST['op'] : $_GET['op'];
if(!empty($op)){

if($op=='add'){
$tree->addNode($_POST['cname'],$_POST['pid']);
back();
}

if($op=='mod'){
$tree->modNode($_POST['cid'],$_POST['cname']);
back();
}

if($op=='del'){
$tree->delNode($_GET['cid']);
back();
}

if($op=='move'){
$tree->moveNode($_POST['who'],$_POST['to']);
back();
}
}
$category = $tree->getChilds();
?>



名称: 添加到:=makeSelect($category,'pid')?>





移动分类



=makeSelect($category,'who')?>移动到:=makeSelect($category,'to')?>




    foreach ($category as $id){
    echo '
  • '.$tree->getLayer($id, '|- ').$tree->getValue($id).' Del Edit
  • ';
    }
    ?>

简单的通过递归查询加目录path字段的无限分类
缺点:查询数据库次数太多,不方便其他操作,比如删除节点。添加节点,移动节点
2,左右值无限分类,预排序二叉树
缺点:操作繁琐,数据库冗余,且添加删除修改都要进行左右值更新

用递归无无限分页类的枋心算法

function cate_arr($arr,&$arr2=array(),$per_id=0,$lv=0){
  static  $i=0;  //从0开始

  if ((bool)$arr) {
  foreach ($arr as $value) {
   if ($value['per_id']==$per_id) {
    $value['lv']=$lv;
    $arr2[$i]=$value;
    $i++;
    $lv++;
    cate_arr($arr,$arr2,$value['cate_id'],$lv--);
   }
  }
 
  }

}

数据结构如

array(
0=>array(
'cate_id'=11,
'per_id'=0,
'cate_name'='a'
),
1=>array(
'cate_id'=12,
'per_id'=0,
'cate_name'='b'
),
2=>array(
'cate_id'=13,
'per_id'=11,
'cate_name'='a10'
),
....);

再来看一个简单的与mysqli直接连接的无限分类

header("Content-type:text/html;charset=utf-8");   $db=new mysqli("localhost","root","","news_php100") ; //实例化一个数据库连接。使用这个前一定要确保已经加载了mysqli类库,或者用mysql_connect这个方式连接。   if(mysqli_connect_errno()){  echo "链接失败:".mysqli_connect_error();  exit(); }   $db->query("set names utf8");  $result=$db->query("select name from class where f_id=0"); //查找f_id=0的分类,也就是查找每一个大类。  while($row=$result->fetch_assoc()){  echo $row['name'].""; //这样就把每个大类循环出来了。  }  //同样我们可以把新闻的子类循环出来。  $result=$db->query("select * from class where f_id=1"); //查找f_id=1的分类,也就是查找‘新闻’的子类。  while($row=$result->fetch_assoc()){  echo $row['name']."  "; //这样就把‘新闻’的子类循环出来了。注意:只是子类,不包括孙子类。  }  //写到这里,我们会发现一个问题,如果这个分类是10级分类,难道我们要写10个循环把它每个子类循环出来?如果是更多级分类呢,这样写显然是不现实的。  //那又有什么办法解决呢?我们可以写一个递归的函数,把f_id作为参数传入,不断循环每一个f_id的值,也就是说把每一个f_id值的子类循环出来。  //首先我们把各个分类的值保存在一个二维数组中,在下面的递归函数里有用。  $result=$db->query("select * from class");  while($row=$result->fetch_assoc()){  $arr[]=array($row[id],$row[f_id],$row[name]); //每一行保存一个分类的id,f_id,name的信息。  }  function fenlei($f_id=0){ //$f_id初始化为0,也就是从最大分类开始循环.  global $arr; //声明$arr为全局变量才可在函数里引用。  for($i=0;$i"; //$arr[$i][1]表示第$i+1个分类的name的值。  fenlei($arr[$i][0]); //$arr[$i][1]表示第$i+1个分类的id的值。进行递归,也就是把自己的id作为f_id参数把自己的子类再循环出来。  }  }  }  fenlei(); //使用这个函数. 

 

本分类方法的优势:

1,数据库结构简单,只有 cid parentid name 三个字段,无任何冗余字段
2,不使用递归查询,所有操作只需一条sql语句
3,所有数据在读取一次数据库后,在数组内进行分析处理,节省数据库服务器资源

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),