Heim > php教程 > PHP源码 > Hauptteil

php 多种无限分类实例

WBOY
Freigeben: 2016-06-08 17:27:22
Original
1077 Leute haben es durchsucht
<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,所有数据在读取一次数据库后,在数组内进行分析处理,节省数据库服务器资源

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!