課程
/后端開發(fā)
/PHP
/PHP無限級(jí)分類技術(shù)
求下載地址
2016-12-25
源自:PHP無限級(jí)分類技術(shù) 4-1
正在回答
<?php
function getList($pid=0,&$res=array(),$spac=0){
$spac=$spac+4;
$link=mysqli_connect('localhost','root','root') or die('沒有鏈接到服務(wù)器');
? ? mysqli_select_db($link,'deepcate') or die("沒有鏈接到表");
$sql="select * from cate where pid={$pid}";
$rows=mysqli_query($link, $sql);
if($rows){
? ? ?while($row=mysqli_fetch_assoc($rows)){
? ? ? $row['catename']=str_repeat(' ',$spac).'|--'.$row['catename'];?
? ? ? $res[]=$row;
getList($row['id'],$res,$spac);
? ? }
return $res;
}
function showList($pid=0,$select){
$result=getList($pid);
echo "<select name='cate'>";
foreach ($result as $key => $value) {
$selecstr='';
if($value['id']==$select){
$selectstr='selected';
?echo "<option ?{$selectstr} >{$value['catename']}</option>";
function getCatePath($id,&$res=array(),$spac=0){
$spac=1;
$sql="select * from cate where id={$id}";
$rows= mysqli_query($link, $sql);
while($row=mysqli_fetch_assoc($rows)){
$row['catename']=$row['catename'].str_repeat('>',$spac);
$res[]=$row;
getCatePath($row['pid'],$res,$spac);
krsort($res);
function showCatePath($id){
$res=getCatePath($id);
foreach ($res as $key => $value) {
echo "<a href='cate.php?cid={$value['id']}'>{$value['catename']}</a>";
showCatePath(10);
?>
純自寫<--!>
舉報(bào)
經(jīng)典的遞歸實(shí)現(xiàn)和全路徑實(shí)現(xiàn)兩種方式實(shí)現(xiàn)無限分類
7 回答建議老師分享一下源碼和筆記,包括添加和刪除
1 回答為什么代碼在 class類下就實(shí)現(xiàn)不了遞歸呢?
2 回答老師什么時(shí)間上傳代碼
1 回答老師記得將代碼傳上來啊
1 回答好像有問題
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2016-12-28
<?php
function getList($pid=0,&$res=array(),$spac=0){
$spac=$spac+4;
$link=mysqli_connect('localhost','root','root') or die('沒有鏈接到服務(wù)器');
? ? mysqli_select_db($link,'deepcate') or die("沒有鏈接到表");
$sql="select * from cate where pid={$pid}";
$rows=mysqli_query($link, $sql);
if($rows){
? ? ?while($row=mysqli_fetch_assoc($rows)){
? ? ? $row['catename']=str_repeat(' ',$spac).'|--'.$row['catename'];?
? ? ? $res[]=$row;
getList($row['id'],$res,$spac);
? ? }
return $res;
}
}
function showList($pid=0,$select){
$result=getList($pid);
echo "<select name='cate'>";
foreach ($result as $key => $value) {
$selecstr='';
if($value['id']==$select){
$selectstr='selected';
}
?echo "<option ?{$selectstr} >{$value['catename']}</option>";
}
}
function getCatePath($id,&$res=array(),$spac=0){
$spac=1;
$link=mysqli_connect('localhost','root','root') or die('沒有鏈接到服務(wù)器');
? ? mysqli_select_db($link,'deepcate') or die("沒有鏈接到表");
$sql="select * from cate where id={$id}";
$rows= mysqli_query($link, $sql);
if($rows){
while($row=mysqli_fetch_assoc($rows)){
$row['catename']=$row['catename'].str_repeat('>',$spac);
$res[]=$row;
getCatePath($row['pid'],$res,$spac);
}
krsort($res);
return $res;
}
}
function showCatePath($id){
$res=getCatePath($id);
foreach ($res as $key => $value) {
echo "<a href='cate.php?cid={$value['id']}'>{$value['catename']}</a>";
}
}
showCatePath(10);
?>
純自寫<--!>