無限分類數(shù)據(jù)庫表CREATE TABLE IF NOT EXISTS `royal_ask_group` (
創(chuàng)新互聯(lián)公司專注骨干網(wǎng)絡(luò)服務(wù)器租用10多年,服務(wù)更有保障!服務(wù)器租用,成都二樞服務(wù)器租用托管 成都服務(wù)器租用,成都服務(wù)器托管,骨干網(wǎng)絡(luò)帶寬,享受低延遲,高速訪問。靈活、實(shí)現(xiàn)低成本的共享或公網(wǎng)數(shù)據(jù)中心高速帶寬的專屬高性能服務(wù)器。
`gid` int(11) NOT NULL auto_increment,
`parent` int(11) NOT NULL default '0',
`topid` int(11) NOT NULL default '0',
`group_key` varchar(255) NOT NULL,
`group_title` varchar(64) NOT NULL,
`ordered` int(5) NOT NULL default '0',
`children` varchar(255) default NULL,
`flag` set('h','c','b','t') default NULL,
`content` text NOT NULL,
`image` varchar(255) default NULL,
PRIMARY KEY (`gid`),
KEY `parent` (`parent`),
KEY `topid` (`topid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;沒父id的parent=0;有父id的數(shù)組 $sql_data_array = array(
'parent' = (int)$_POST['parent'],
'topid' = (int)$_POST['topid'],
'group_key'= ($_POST['group_key']),
'group_title'= ($_POST['group_title']),
'flag'= (isset($_POST['flags']) ? join(',',$_POST['flags']) : ''),
'content'= ($_POST['content']),
'ordered' = (int)$_POST['ordered']);
可以在表A中增加一個(gè)字段,該字段關(guān)聯(lián)表B的SID,或者可以新建一張關(guān)聯(lián)表C (ID,NEWSID,SID) 起到關(guān)聯(lián)作用
select * from b2b_news as a,b2b_news_sort as b where a.sid= b.sid
是關(guān)聯(lián)查詢嗎 ,如果有其他條件的話,在后面加and吧
給個(gè)函數(shù)你,調(diào)用get_category()就是一個(gè)數(shù)組格式的結(jié)果
function get_category($parent_id=0){
$arr=array();
$sql = "select * from category where parent_id=$parent_id";//查詢子級(jí)數(shù)據(jù)
$result = array(a_object,b_object,,,)=sql_query($sql);//查詢結(jié)果一個(gè)數(shù)組或列表格式,自己完善。
if($result){
foreach($result as $re){//循環(huán)數(shù)組
if(get_category($re.id))//如果子級(jí)不為空
$re['child'] = get_category($re.id);
$arr[] = $re;
}
return $arr;
}
?php
/*========================================================
類名:catalog
功能:無限分級(jí)類
方法:
樹形顯示分類
catalog_show($id) //參數(shù)$id 遞歸調(diào)用
流程:找到父分類為0所有根分類- 一直遞歸取得所有分類并顯示
添加分類
catalog_add($uid,$name) //$uid 父id //$name 分類名
流程:依據(jù)$uid,在此id下添加一個(gè)新子id
刪除分類
catalog_del($uid)//參數(shù) $uid 數(shù)要?jiǎng)h除的分類
修改分類
catalog_set($id,$name) //參數(shù) $id 要修改的分類 //參數(shù) $name 新的分類名
變量:
$config //數(shù)據(jù)庫信息- host,user,pass,dbname
$catalog_dbname //分類數(shù)據(jù)庫名
數(shù)據(jù)庫:
catalog_id //分類的自然序號(hào)
catalog_uid //分類的父分類
catalog_name //分類名
catalog_path_number //親緣樹數(shù)字形式 0:1:2
catalog_path_char //親緣樹字符形式 分類1:分類1.1:分類1.1.1
參照文章
========================================================*/
class catalog{
var $config;
var $catalog_dbname;
var $links;
private function connect(){
$this-links = mysql_connect($this-config['host'],$this-config['user'],$this-config['pass']) or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
mysql_select_db($this-config['dbname'],$this-links);
mysql_query("SET NAMES gb2312");
}
function catalog_show($uid = 0){
$this-connect();
$sql = "Select * FROM ".$this-catalog_dbname. "
Where catalog_uid = ". $uid ."
orDER BY catalog_id ";
$result = mysql_query($sql,$this-links) or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
if(mysql_num_rows($result) 0){
while ($row = mysql_fetch_assoc($result)){
if($this-sun_catalog($row['catalog_id'])){//判斷有沒有子分類
$cata_img = "img id = 'img".$row['catalog_id']."' src='./img/last_node.jpg' ōnclick='click_catalog(".$row['catalog_id'].")'/";
}else{
$cata_img = "img src='./img/sp.jpg'/";
}
$path = explode(":",$row['catalog_path_number']);
if(count($path) 1){
for($i=1;$icount($path);$i++){
$path_img .= "img src='./img/sp.jpg'/";
}
}
echo $path_img.$cata_img;
echo "a class='menu' href = 'javascrīpt:send_id(".$row['catalog_id'].")'";
echo $row['catalog_name']."/abr";
$path_img = "";
if($this-sun_catalog($row['catalog_id'])){
$hidden_div = "style='display:none'";
echo "div id = 'div".$row['catalog_id']."' ".$hidden_div."";
$this-catalog_show($row['catalog_id']);
echo "/div";
}
}
}
}
private function sun_catalog($uid){//判斷是否有子分類
$sql = "Select * FROM ".$this-catalog_dbname. "
Where catalog_uid = ". $uid ."
orDER BY catalog_id ";
$result = mysql_query($sql,$this-links) or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
if(mysql_num_rows($result) 0){
return true;
}else{
return false;
}
}
function catalog_add($uid,$name){
//獲取父id的親緣樹
$this-connect();
$sql = "Select * FROM ".$this-catalog_dbname."
Where catalog_id = '".$uid."'";
$result = mysql_query($sql,$this-links)
or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
$row = mysql_fetch_assoc($result);
$fid_path_number = $row['catalog_path_number'];//id的數(shù)字親緣樹
$fid_path_char = $row['catalog_path_char'];//id的字符親緣樹
//插入數(shù)據(jù) 先插入行-再找到最新插入的id, 在依據(jù)這個(gè)id進(jìn)行修改
$sql = "Insert INTO ".$this-catalog_dbname."(catalog_uid,catalog_name)
VALUES(".$uid.",'".$name."')";
$result = mysql_query($sql,$this-links)
or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
$catalog_id = mysql_insert_id();//獲取自己的id
$catalog_path_number = $fid_path_number.":".$catalog_id;//得到自己的數(shù)字親緣數(shù)
$catalog_path_char = $fid_path_char.":".$name;//得到自己的字符親緣數(shù)
$sql = "Update '".$this-catalog_dbname."'
SET
catalog_path_number = '".$catalog_path_number."',
catalog_path_char = '".$catalog_path_char."'
Where
catalog_id = ".$catalog_id;
mysql_query($sql,$this-links)
or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
}
function catalog_del($id){
$this-connect();
$sql = "Delete FROM ".$this-catalog_dbname."
Where catalog_id = ".$id;
mysql_query($sql,$this-links)
or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
}
function catalog_set($id,$name){
$this-connect();
$sql = "Update ".$this-catalog_dbname."
SET
catalog_name = '".$name."'
Where
catalog_id = ".$id;
mysql_query($sql,$this-links)
or die("錯(cuò)誤: 第".__LINE__."行br".mysql_error());
}
}
?
/**
*?遞歸實(shí)現(xiàn)無限極分類
*?@param?$array?分類數(shù)據(jù)
*?@param?$pid?父ID
*?@param?$level?分類級(jí)別
*?@return?$list?分好類的數(shù)組?直接遍歷即可?$level可以用來遍歷縮進(jìn)
*/
function?getTree($array,?$pid?=0,?$level?=?0){
//聲明靜態(tài)數(shù)組,避免遞歸調(diào)用時(shí),多次聲明導(dǎo)致數(shù)組覆蓋
static?$list?=?[];????????foreach?($array?as?$key?=?$value){????????????//第一次遍歷,找到父節(jié)點(diǎn)為根節(jié)點(diǎn)的節(jié)點(diǎn)?也就是pid=0的節(jié)點(diǎn)
if?($value['pid']?==?$pid){????????????????//父節(jié)點(diǎn)為根節(jié)點(diǎn)的節(jié)點(diǎn),級(jí)別為0,也就是第一級(jí)
$value['level']?=?$level;????????????????//把數(shù)組放到list中
$list[]?=?$value;????????????????//把這個(gè)節(jié)點(diǎn)從數(shù)組中移除,減少后續(xù)遞歸消耗
unset($array[$key]);????????????????//開始遞歸,查找父ID為該節(jié)點(diǎn)ID的節(jié)點(diǎn),級(jí)別則為原級(jí)別+1
getTree($array,?$value['id'],?$level+1);
}
}
主要是利用函數(shù)的遞歸,然后在無下級(jí)的條件下退出遞歸!
這個(gè)是我自己寫的一個(gè)函數(shù),無限樹型的你參考下吧!
function getCategoryTree($TID, $iTable,$url,$urlPar="1=1") {
if ($TID == 0) {
$treeStr = $treeStr."ul id=\"tree\" class=\"lightTreeview treeview-black\"";
}
$sql = "select * from `$iTable` where TID1 = ".$TID." order by orderid";
$rs = mysql_query($sql);
while ($ary = mysql_fetch_array($rs)) {
$treeStr = $treeStr."li";
$rs2 = mysql_query("select count(*) from `$iTable` where TID1 = ".$ary["id"]);
$Vcount = @mysql_result($rs2, 0);
if ($Vcount != 0) {
$treeStr = $treeStr."div class=\"treeview-folder\"".$ary["title"]."/divul style=\"display:none\"";
$treeStr = $treeStr.getCategoryTree($ary["id"], $iTable,$url,$urlPar);
$treeStr = $treeStr."/ul";
} else {
$treeStr = $treeStr."div class=\"treeview-file\"a href=\"".$url."?T=".$ary["id"]."".$urlPar."\" target=\"pro\"".$ary["title"]."/a/div";
}
$treeStr = $treeStr."/li";
}@mysql_free_result($rs);
if ($TID == 0) {
$treeStr = $treeStr."/ul";
}
return $treeStr;
}