ホームページ  >  記事  >  バックエンド開発  >  php+mysqlデータベース無制限分類コード(1/2)_PHPチュートリアル

php+mysqlデータベース無制限分類コード(1/2)_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-13 17:04:45763ブラウズ

php+mysqlデータベース無制限の分類コード この PHP 無制限分類コードは、データベースが MySQL であることを含め、比較的完全であり、データベース SQL テーブル構造

php チュートリアル + mysql チュートリアル データベース チュートリアル 無制限分類コード
も提供します。 この PHP 無制限分類コードは比較的完全で、データベースが MySQL であり、追加、削除、編集、移動の機能があり、データベース SQL テーブル構造も提供します。

//データベースに接続します
$link = mysql_connect('localhost','root','') または die(mysql_error());
mysql_select_db('class',$link) または die(mysql_error());
mysql_query("セット名 gbk");
//無制限の分類ライブラリ

クラスソートクラス{

var $data = array();
var $child = array(-1=>array());
var $layer = array(-1=>-1);
var $parent = array();
var $link;
var $table;
関数 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_array($results)){
$this->setnode($node['id'],$node['f_id'],$node['name']);
}
}
関数 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;
}
関数 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];}
関数 getparent ($id){
while ($this->parent[$id] != -1){
$id = $parent[$this->layer[$id]] = $this->parent[$id];
}
ksort($parent);
リセット($parent);
$parent を返します;
}
function getchild ($id){return $this->child[$id];}
関数 getchilds ($id = 0){
$child = 配列($id);
$this->getlist($child, $id);
$child を返します;
}
関数 addnode($name,$pid){
//echo "$this->table (`f_id`,`name`) 値に挿入 ('$pid','$name')";exit;
mysql_query("$this->テーブル (`f_id`,`name`) 値 ('$pid','$name') に挿入",$this->link);

}
関数 modnode($cid, $newname){
mysql_query("update $this->table set `name`='$newname' where `id` = $cid",$this->link);
}
関数 delnode($cid){
$allchilds = $this->getchilds($cid);
$sql ='';
if(空($allchilds)){
$sql = "`id` = $cid の $this->table から削除";
}その他{
$sql = '.$this->table.' where `id` in ('.implode(',',$allchilds).','.$cid.')';
}
mysql_query($sql,$this->link);
}
関数 movenode($cid, $topid){
mysql_query("update $this->table set `f_id`=$topid where `id` = $cid", $this->link);
}
}
//関数
関数 back(){
echo '';
終了します;
}
//選択を生成します
関数 makeselect($array,$formname){
グローバル $tree;
$select = '';
}
$tree = 新しいソートクラス($link,''p_newsclass`');
$op = !empty($_post['op']) $_post['op'] : $_get['op'];
if(!empty($op)){

if($op=='追加'){
$tree->addnode($_post['cname'],$_post['pid']);
戻る();
}

if($op=='mod'){
$tree->modnode($_post['cid'],$_post['cname']);
戻る();
}

if($op=='del'){
$tree->delnode($_get['cid']);
戻る();
}

if($op=='移動'){
$tree->movenode($_post['who'],$_post['to']);
戻る();
}
}
$category = $tree->getchilds();
?>

1 2

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/630842.html技術記事 php+mysql データベース無制限分類コード この php 無制限分類コードは、データベースが mysql で、追加、削除、編集、移動の機能があり、データベースも提供しています...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。