ホームページ  >  記事  >  バックエンド開発  >  追加、削除、移動、変更が可能な無制限のニュース分類コードを完成させる_PHP チュートリアル

追加、削除、移動、変更が可能な無制限のニュース分類コードを完成させる_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-13 16:58:57977ブラウズ


//连接続データベース库教程
$link = mysql教程_connect('localhost','root','密码') または die(mysql_error());
mysql_select_db('ソートクラス',$link);
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 = 配列();
$results = mysql_query('select * from '.$this->table.'',$this->link);
while($node = mysql_fetch_assoc($results)){
$this->setnode($node['cid'],$node['pid'],$node['cname']);
}
}
関数 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);
}
}
関数 getvalue ($id){return $this->data[$id];}
関数 getlayer ($id, $space = false){
return $space?str_repeat($space, $this->layer[$id]):$this->layer[$id];
}
関数 getparent ($id){return $this->parent[$id];}
関数 getparents ($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){
mysql_query("$this->テーブル (`pid`,`cname`) 値 ('$pid','$name') に挿入",$this->link);
}
関数 modnode($cid, $newname){
mysql_query("update $this->table set `cname`='$newname' where `cid` = $cid",$this->link);
}
関数 delnode($cid){
$allchilds = $this->getchilds($cid);
$sql ='';
if(空($allchilds)){
$sql = "`cid` = $cid の $this->table から削除";
}その他{
$sql = '.$this->テーブルから削除します。'ここで `cid` の ('.implode(',',$allchilds).','.$cid.')';
}
mysql_query($sql,$this->link);
}
関数 movenode($cid, $topid){
mysql_query("update $this->table set `pid`=$topid where `cid` = $cid", $this->link);
}
}
//関数数
関数 back(){
echo '';
出口;
}
// 生成選択
関数 makeselect($array,$formname){
グローバル $tree;
$select = '';
}
$tree = 新しいソートクラス($link,'`クラス`');
$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['誰'],$_post['to']);
戻る();
}
}
$category = $tree->getchilds();
?>