php與mysql的級連菜單,體現方法一(自家用)

來源:互聯網
上載者:User
不做什麼解釋了,早寫的了,已經忘的差不多了,使用只要更改相應的sql語句就可以
//js的指令碼完成張合

$GLOBALS["ID"] =1;
$layer=1;
$con=mysql_connect("127.0.0.1","root","");
if(!$con)
{
echo "級連菜單庫串連出錯!";
exit();
}
mysql_select_db("soohuo");
$sql="select * from t_class where parent_id=0";
$result=mysql_query($sql,$con);
$num=mysql_num_rows($result);//計算是否有上級
if($num>0)
{

show_child($con,$result,$layer,$ID);

}
function show($con,$pid)
{
$var_td="";
$var_tr="";
$sql="select * from t_class where parent_id=$pid";
$result1=mysql_query($sql,$con);
$j=0;
while($row=mysql_fetch_array($result1))
{

$sql="select * from t_class where parent_id=".$row["class_id"];
$result2=mysql_query($sql,$con);
$num2=mysql_num_rows($result2);
//$var_td="";
if($num2==0)
{
$var_td.="".$row["class_name"]."

";
$j++;
if($j%5==0)//每行的顯示數
$var_td.="
";
}
else
{
$var_tr=$var_tr."".$row["class_name"]."".show($con,$row["class_id"])."

";
//show_child($con,$result2,$layer);
}

}
$var="

".$var_td."
".$var_tr."

";
return $var;
}
function show_child($con,$result,$layer)
{
$numrows=mysql_num_rows($result); //計算有多少個一級項
if($numrows>0)
{
echo "



$pid=$row["class_id"];
$sql="select * from t_class where parent_id=$pid";
$result_sub=mysql_query($sql,$con);
$num_2=mysql_num_rows($result_sub);
if($num_2>0)
{
echo "

";
echo "


";
}
else
{
}
}
echo "

"; while($row=mysql_fetch_array($result)){ ";echo "
".$row["class_name"]."
";
echo show($con,$pid);
echo "

";

}
}
mysql_close($con);
?>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.