php+mysql查詢實(shí)現(xiàn)無(wú)限下級(jí)分類樹(shù)輸出示例
本文實(shí)例講述了php+mysql查詢實(shí)現(xiàn)無(wú)限下級(jí)分類樹(shù)輸出。分享給大家供大家參考,具體如下:
這里介紹的php結(jié)合mysql查詢無(wú)限下級(jí)樹(shù)輸出,其實(shí)就是無(wú)限分類。給各位整理了幾個(gè)php無(wú)限分類的例子.
樹(shù)輸出:
function get_array($user_id,$top=0){
global $mysql,$_G;
$sql = "select user_id as name from `{spreads_users}` where p1.spreads_userid='{$user_id}'";
$rows= $mysql->db_fetch_arrays($sql);
if($top==1){
$arr[0]['name']=$user_id;
$arr[0]['children']=array();
}
$top=$top+1;
foreach ($rows as $key=>$value)
{
$r = get_array($value['name']); //調(diào)用函數(shù),傳入?yún)?shù),繼續(xù)查詢下級(jí)
$arr[0]['children'][$key]['name']= $value['username']; //組合數(shù)組
if(is_array($r)){
$arr[0]['children'][$key]['children']= $r[0]['children'];
}
$i++;
}
return $arr;
}
$list = get_array("1000",1); //調(diào)用函數(shù)1000是頂級(jí)ID
echo 'var data='.json_encode($list);
這個(gè)是輸出 Array 然后轉(zhuǎn)讓為 json
例子:
表結(jié)構(gòu):id字段為分類標(biāo)識(shí),name字段為分類名,father_id字段為所屬父分類的id,path字段為分類路徑,儲(chǔ)存該分類祖先的集合,isdir判斷是否是目錄,1為是,0為否.
顯示函數(shù):
//$count為分類等級(jí)
sort_list($str,$fatherid,$count)
{
$rs = $this->sql->re_datas("select * from sort where father_id = fatherid");
$num = $this->sql->sql_numrows();
$i=0;
$n = 1;
while(isset($rs[$i]))
{
$name = "";
for($n = 1 ; $n < $count ; $n )
{
$name.="│ ";
}
if($i 1==$num)
{
$name.="└─".$rs[$i][name];
}
else
{
$name.="├─".$rs[$i][name];
}
if($rs[$i][isdir])
{
$str.="<span style='color:#CCCCCC'>".$name."</span>";
}
else
{
$str.=$name";
}
$temp = $count 1;
$str = $this->sort_list($str,$rs[$i][id],$temp);
$i ;
}
return $str;
}
其中$this->sql對(duì)象為sql操作類對(duì)象,re_datas()函數(shù)返回查到的數(shù)組,sql_numrows()函數(shù)返回查詢到的數(shù)目.
調(diào)用方法:
$sort_list = sort_list($sort_list,0,1);
例子:
表:category
id int 主鍵,自增
name varchar 分類名稱
pid int 父類id,默認(rèn)0
頂級(jí)分類的 pid 默認(rèn)就是0了,當(dāng)我們想取出某個(gè)分類的子分類樹(shù)的時(shí)候,基本思路就是遞歸,當(dāng)然,出于效率問(wèn)題不建議每次遞歸都查詢數(shù)據(jù)庫(kù),通常的做法是先講所有分類取出來(lái),保存到PHP數(shù)組里,再進(jìn)行處理,最后還可以將結(jié)果緩存起來(lái)以提高下次請(qǐng)求的效率.
先來(lái)構(gòu)建一個(gè)原始數(shù)組,這個(gè)直接從數(shù)據(jù)庫(kù)中拉出來(lái)就行:
$categories = array(
array('id'=>1,'name'=>'電腦','pid'=>0),
array('id'=>2,'name'=>'手機(jī)','pid'=>0),
array('id'=>3,'name'=>'筆記本','pid'=>1),
array('id'=>4,'name'=>'臺(tái)式機(jī)','pid'=>1),
array('id'=>5,'name'=>'智能機(jī)','pid'=>2),
array('id'=>6,'name'=>'功能機(jī)','pid'=>2),
array('id'=>7,'name'=>'超級(jí)本','pid'=>3),
array('id'=>8,'name'=>'游戲本','pid'=>3),
);
目標(biāo)是將它轉(zhuǎn)化為下面這種結(jié)構(gòu):
電腦 —筆記本 ——-超級(jí)本 ——-游戲本 —臺(tái)式機(jī) 手機(jī) —智能機(jī) —功能機(jī)
用數(shù)組來(lái)表示的話,可以增加一個(gè) children 鍵來(lái)存儲(chǔ)它的子分類:
array(
//1對(duì)應(yīng)id,方便直接讀取
1 => array(
'id'=>1,
'name'=>'電腦',
'pid'=>0,
children=>array(
&array(
'id'=>3,
'name'=>'筆記本',
'pid'=>1,
'children'=>array(
//此處省略
)
),
&array(
'id'=>4,
'name'=>'臺(tái)式機(jī)',
'pid'=>1,
'children'=>array(
//此處省略
)
),
)
),
//其他分類省略
)
處理過(guò)程:
$tree = array();
//第一步,將分類id作為數(shù)組key,并創(chuàng)建children單元
foreach($categories as $category){
$tree[$category['id']] = $category;
$tree[$category['id']]['children'] = array();
}
//第二部,利用引用,將每個(gè)分類添加到父類children數(shù)組中,這樣一次遍歷即可形成樹(shù)形結(jié)構(gòu)。
foreach ($tree as $k=>$item) {
if ($item['pid'] != 0) {
$tree[$item['pid']]['children'][] = &$tree[$k];
}
}
print_r($tree);打印結(jié)果如下:
Array
(
[1] => Array
(
[id] => 1
[name] => 電腦
[pid] => 0
[children] => Array
(
[0] => Array
(
[id] => 3
[name] => 筆記本
[pid] => 1
[children] => Array
(
[0] => Array
(
[id] => 7
[name] => 超級(jí)本
[pid] => 3
[children] => Array
(
)
)
[1] => Array
(
[id] => 8
[name] => 游戲本
[pid] => 3
[children] => Array
(
)
)
)
)
[1] => Array
(
[id] => 4
[name] => 臺(tái)式機(jī)
[pid] => 1
[children] => Array
(
)
)
)
)
[2] => Array
(
[id] => 2
[name] => 手機(jī)
[pid] => 0
[children] => Array
(
[0] => Array
(
[id] => 5
[name] => 智能機(jī)
[pid] => 2
[children] => Array
(
)
)
[1] => Array
(
[id] => 6
[name] => 功能機(jī)
[pid] => 2
[children] => Array
(
)
)
)
)
[3] => Array
(
[id] => 3
[name] => 筆記本
[pid] => 1
[children] => Array
(
[0] => Array
(
[id] => 7
[name] => 超級(jí)本
[pid] => 3
[children] => Array
(
)
)
[1] => Array
(
[id] => 8
[name] => 游戲本
[pid] => 3
[children] => Array
(
)
)
)
)
[4] => Array
(
[id] => 4
[name] => 臺(tái)式機(jī)
[pid] => 1
[children] => Array
(
)
)
[5] => Array
(
[id] => 5
[name] => 智能機(jī)
[pid] => 2
[children] => Array
(
)
)
[6] => Array
(
[id] => 6
[name] => 功能機(jī)
[pid] => 2
[children] => Array
(
)
)
[7] => Array
(
[id] => 7
[name] => 超級(jí)本
[pid] => 3
[children] => Array
(
)
)
[8] => Array
(
[id] => 8
[name] => 游戲本
[pid] => 3
[children] => Array
(
)
)
)
優(yōu)點(diǎn):關(guān)系清楚,修改上下級(jí)關(guān)系簡(jiǎn)單.
缺點(diǎn):使用PHP處理,如果分類數(shù)量龐大,效率也會(huì)降低.
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+mysql數(shù)據(jù)庫(kù)操作入門教程》、《PHP基本語(yǔ)法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP+Mysql樹(shù)型結(jié)構(gòu)(無(wú)限分類)數(shù)據(jù)庫(kù)設(shè)計(jì)的2種方式實(shí)例
- php+mysql實(shí)現(xiàn)無(wú)限分類實(shí)例詳解
- 實(shí)現(xiàn)PHP+Mysql無(wú)限分類的方法匯總
- php+mysql數(shù)據(jù)庫(kù)實(shí)現(xiàn)無(wú)限分類的方法
- php+mysql實(shí)現(xiàn)無(wú)限級(jí)分類 | 樹(shù)型顯示分類關(guān)系
- php+mysql不用遞歸實(shí)現(xiàn)的無(wú)限級(jí)分類實(shí)例(非遞歸)
- php+mysql實(shí)現(xiàn)無(wú)限級(jí)分類
- PHP遞歸寫入MySQL實(shí)現(xiàn)無(wú)限級(jí)分類數(shù)據(jù)操作示例
- php+mysql實(shí)現(xiàn)的無(wú)限分類方法類定義與使用示例
相關(guān)文章
php連接與操作PostgreSQL數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了php連接與操作PostgreSQL數(shù)據(jù)庫(kù)的方法,以實(shí)例形式較為詳細(xì)的分析了php連接PostgreSQL數(shù)據(jù)庫(kù)以及進(jìn)行讀取與增加、修改、刪除等技巧,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
php字符串過(guò)濾strip_tags()函數(shù)用法實(shí)例分析
這篇文章主要介紹了php字符串過(guò)濾strip_tags()函數(shù)用法,結(jié)合實(shí)例形式分析了php字符串過(guò)濾函數(shù)strip_tags()功能、參數(shù)及相關(guān)使用技巧,需要的朋友可以參考下2019-06-06
PHP實(shí)時(shí)統(tǒng)計(jì)中文字?jǐn)?shù)和區(qū)別
今天小編就為大家分享一篇關(guān)于PHP統(tǒng)計(jì)實(shí)時(shí)統(tǒng)計(jì)漢字個(gè)數(shù)和區(qū)別,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
php中抓取網(wǎng)頁(yè)內(nèi)容的實(shí)例詳解
這篇文章主要介紹了php中抓取網(wǎng)頁(yè)內(nèi)容的實(shí)例詳解的相關(guān)資料,這里提供兩種實(shí)現(xiàn)方法,希望能幫助到大家,需要的朋友可以參考下2017-08-08
PHP在終端中實(shí)現(xiàn)進(jìn)度條來(lái)處理數(shù)據(jù)的示例詳解
在PHP中,有時(shí)候你需要在終端中處理大量數(shù)據(jù)或執(zhí)行長(zhǎng)時(shí)間運(yùn)行的任務(wù),同時(shí)希望能夠?qū)崟r(shí)跟蹤任務(wù)的進(jìn)度,在 PHP 中,有時(shí)候你需要在終端中處理大量數(shù)據(jù)或執(zhí)行長(zhǎng)時(shí)間運(yùn)行的任務(wù),同時(shí)希望能夠?qū)崟r(shí)跟蹤任務(wù)的進(jìn)度,需要的朋友可以參考下2023-10-10
Docker 安裝 PHP并與Nginx的部署實(shí)例講解
這篇文章主要介紹了Docker 安裝 PHP并與Nginx的部署實(shí)例講解,文中圖文操作步驟講解的很清楚,有感興趣的同學(xué)可以研究下2021-02-02
PHP使用ActiveMQ實(shí)現(xiàn)消息隊(duì)列的方法詳解
這篇文章主要介紹了PHP使用ActiveMQ實(shí)現(xiàn)消息隊(duì)列的方法,結(jié)合實(shí)例形式詳細(xì)分析了PHP使用ActiveMQ實(shí)現(xiàn)消息隊(duì)列具體步驟、相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-05-05

