php+mysql如何實現(xiàn)的無限分類方法類定義-創(chuàng)新互聯(lián)

這篇文章主要介紹了php+mysql如何實現(xiàn)的無限分類方法類定義,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)建站主要從事做網(wǎng)站、成都做網(wǎng)站、網(wǎng)頁設計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務郊區(qū),10多年網(wǎng)站建設經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220

php+mysql實現(xiàn)的無限分類方法類定義與使用,具體如下:

創(chuàng)建數(shù)據(jù)庫以及表

 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");
//連接數(shù)據(jù)庫
$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, '頂極節(jié)點');
  $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);
 }
}
//函數(shù)
function back(){
 echo '<script language="JavaScript">window.location.href="test.php?" rel="external nofollow" +new Date().getTime();</script>';
 exit;
}
//聲成select
function makeSelect($array,$formName){
 global $tree;
 $select = '<select name="'.$formName.'">';
 foreach ($array as $id){
  $select.='<option value="'.$id.'">'.$tree->getLayer($id, '|-').$tree->getValue($id)."</option>";
 }
 return $select.'</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();
?>
 <style type="text/css">
  body{font-size:12px;}
  ul{list-style:none;}
  a{cursor:pointer;}
 </style>
 <script language="javascript">
  function $(e){return document.getElementById(e);}
  function mod(cid){
   $('cid').value=cid;
   $('op').value='mod';
   $('name').style.border='1px solid red';
  }
 </script>
 <form action="test.php" method="post">
  名稱:<input type="text" id="name" name="cname" /> 添加到:<?=makeSelect($category,'pid')?><br />
  <input type="hidden" id="op" name="op" value="add" />
  <input type="hidden" id="cid" name="cid" />
  <input type="submit" value="Submit" />
 </form>
 <h4>移動分類</h4>
 <form action="test.php" method="post">
  <?=makeSelect($category,'who')?>移動到:<?=makeSelect($category,'to')?>
  <input type="hidden" id="op" name="op" value="move" />
  <input type="submit" value="Submit" />
 </form>
 <ul>
<?php
foreach ($category as $id){
 echo '<li>'.$tree->getLayer($id, '|- ').$tree->getValue($id).' <a href="test.php?op=del&cid='.$id.'" rel="external nofollow" >Del</a> <a onclick="mod('.$id.')">Edit</a> </li>';
}
?>
</ul>

感謝你能夠認真閱讀完這篇文章,希望小編分享的“php+mysql如何實現(xiàn)的無限分類方法類定義”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián)網(wǎng)站建設公司,,關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關知識等著你來學習!

當前題目:php+mysql如何實現(xiàn)的無限分類方法類定義-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://muchs.cn/article22/pdgcc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、品牌網(wǎng)站建設微信小程序、軟件開發(fā)、網(wǎng)站內鏈、外貿建站

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設