通过递归调用函数从数据库检索数据后动态填充菜单
我正在为我的大学项目建立一个网站,并决定创建首页。在它上面,我尝试通过表示父类别并通过递归调用一个函数来动态填充类别和子类别来显示菜单,该函数通过从数据库获取数据来执行相同的操作。但是,结果仅显示主菜单或父菜单,不显示子类别。我不知所措。 php代码如下所示。
<?php
session_start();
include("connection.php");
$a=0;
if(isset($_SESSION["valid_user"]))
{
echo "WELCOME ";
echo $_SESSION["valid_user"];
$name = $_SESSION["valid_user"];
$sqllist = "select * from category order by category_name";
$rscatlist = mysql_query($sqllist);
$catid1 = "";
$opt = "";
while($rowcatlist = mysql_fetch_array($rscatlist))
{
$catid1 = $rowcatlist["category_id"];
$catname = $rowcatlist["category_name"];
$opt .= "<option value='$catid1'>$catname</option>";
}
$sql1 = "select * from register where email_id='$name'" ;
$rs1 = mysql_query($sql1);
$row = 0;
$row1 = "";
while($row1=mysql_fetch_array($rs1))
{
$row = $row1["admin"];
}
if($row==1)
{
echo "                             <a href='admin/usermanagement/index.php'>Management Services </a>   ";
}
echo "<p align='right'><a href='logout.php'><font size='3'>logout</font></a></p>"."<br>" ."<br/>";
}
else
{
echo "<p align='right'><a href='register.php'><font size='3'>Register</font></a> or <a href='login.php'><font size='3'>Login</font></a></p>"."<br/>"."<br/>";
}
$tbl = "";
$cat_id = 0;
$tbl = recursive($tbl,$cat_id);
function recursive($tbl,$cat_id)
{
$sql = "select * from category where parent_id='$cat_id'";
$rs = mysql_query($sql);
$tbl.="<ul>";
while($row=mysql_fetch_array($rs))
{
$tbl .= "<li><a href='product.php?category_id=$row'[category_id]'>$row[category_name]</a>";
$sql1 = "select * from category where parent_id=$row'[category_id]'";
$rs1 = mysql_query($sql1);
while($row1 = mysql_fetch_array($rs1))
{
recursive($tbl,$row["category_id"]);
}
$tbl.="</li>";
}
$tbl.="</ul>";
return $tbl;
}
?>
I am building a website for my college project and have decided to create the front page. On it i have tried to display the menu by representing the parent categories and by dynamically populating the categories with their subcategories retrieved by recursively calling a function which does the same by fetching data from the database. However, the result only shows the main or parent menus and does not show the sub categories. I am at a loss.
The php code is shown below.
<?php
session_start();
include("connection.php");
$a=0;
if(isset($_SESSION["valid_user"]))
{
echo "WELCOME ";
echo $_SESSION["valid_user"];
$name = $_SESSION["valid_user"];
$sqllist = "select * from category order by category_name";
$rscatlist = mysql_query($sqllist);
$catid1 = "";
$opt = "";
while($rowcatlist = mysql_fetch_array($rscatlist))
{
$catid1 = $rowcatlist["category_id"];
$catname = $rowcatlist["category_name"];
$opt .= "<option value='$catid1'>$catname</option>";
}
$sql1 = "select * from register where email_id='$name'" ;
$rs1 = mysql_query($sql1);
$row = 0;
$row1 = "";
while($row1=mysql_fetch_array($rs1))
{
$row = $row1["admin"];
}
if($row==1)
{
echo "                             <a href='admin/usermanagement/index.php'>Management Services </a>   ";
}
echo "<p align='right'><a href='logout.php'><font size='3'>logout</font></a></p>"."<br>" ."<br/>";
}
else
{
echo "<p align='right'><a href='register.php'><font size='3'>Register</font></a> or <a href='login.php'><font size='3'>Login</font></a></p>"."<br/>"."<br/>";
}
$tbl = "";
$cat_id = 0;
$tbl = recursive($tbl,$cat_id);
function recursive($tbl,$cat_id)
{
$sql = "select * from category where parent_id='$cat_id'";
$rs = mysql_query($sql);
$tbl.="<ul>";
while($row=mysql_fetch_array($rs))
{
$tbl .= "<li><a href='product.php?category_id=$row'[category_id]'>$row[category_name]</a>";
$sql1 = "select * from category where parent_id=$row'[category_id]'";
$rs1 = mysql_query($sql1);
while($row1 = mysql_fetch_array($rs1))
{
recursive($tbl,$row["category_id"]);
}
$tbl.="</li>";
}
$tbl.="</ul>";
return $tbl;
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在定义该函数之前调用 recursive() 函数似乎很奇怪。似乎很奇怪的是,您仅为 $cat_id 0 调用该函数。您是否需要循环遍历所有主级类别 ID 并将它们也传递到该函数中?
天啊,有没有人教过你评论你的代码;)
It seems odd that you're calling the recursive() function right before you're defining that function. It also seems odd that you're only calling that function for the $cat_id of 0. Wouldn't you need to loop through all your main-level category IDs and pass them into the function too?
And good Lord, has anyone ever taught you to comment your code ;)