PHP 动态菜单与 SQL
我遇到以下问题,我有一个侧菜单,并且基于 $_GET['cat'] 它必须显示内容。
所以我调用了我的函数: function showCat($cat) , $cat = $_GET['cat'] 。
函数 showCat 是这样的:
function showCat($cat){
$cat= mysql_real_escape_string($cat);
connectDatabase();
$menuquery = mysql_query("SELECT c_id, c_naam FROM categorie WHERE c_parent = '0'");
while ($row = mysql_fetch_array($menuquery))
{
$c_id = $row["c_id"];
$c_naam = $row["c_naam"];
$c_parent = $row["c_parent"];
echo "<a href='producten.php?cat=$c_id'>$c_naam<br>";
if ($cat == "0")
{
}
else
{
$query2 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_parent = '$cat' "); // 5
while ($row2 = mysql_fetch_array($query2))
{
$c_id2 = $row2["c_id"]; // 11
$c_naam2 = $row2["c_naam"]; // test
if ($c_id == $cat)
{
echo "<a href='producten.php?cat=$c_id2'>- $c_naam2<br>";
}
else
{
echo "<a href='producten.php?cat=$c_id2'>- $c_naam2<br>";
$query3 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_id = '$c_id2'");
while ($row3 = mysql_fetch_array($query3))
{
$c_id3 = $row3["c_id"];
$c_naam3 = $row3["c_naam"];
$c_parent3 = $row3["c_parent"];
if ("a" == "b")
{
echo "<a href='producten.php?cat=$c_id3'>-- $c_naam3<br>";
}
}
}
}
}
}
echo "</h3>";
}
表“Categorie”看起来像这样: c_id c_naam c_parent
会发生以下情况:
如果我不设置 $cat 或将 $cat 设置为 1 或 3,则菜单如下所示:
- Item 1
- Item 2
- Item 3
如果我将 $cat 设置为 2(在 2 下有 4 和 5)菜单是这样的:
- 项目 1
- ~项目 4
- ~项目 5
- 项目 2
- ~项目 4
- ~项目 5
- 项目 3
- ~项目 4
- ~项目 5
如果我想要在项目 4 下添加其他项目,可以说6 和 7,我将 $cat 设置为 4,菜单变为:
- Item 1
- ~Item 6
- ~Item 7
- Item 2
- ~Item 6
- ~Item 7
- Item 3
- ~Item 6
- ~Item 7
我该怎么做才能使它工作可以变成:
- 项目 1
- 项目 2
- ~项目 4
- ~~项目 6
- ~~项目 7
- ~项目 5
- 项目 3
我希望我很清楚。
先感谢您!
如果不可能,有人可以给我一个例子,如何获取已经折叠的菜单吗?
I've got the following problem, i have a sidemenu and based on a $_GET['cat'] it must show content.
So i called my function : function showCat($cat) , $cat = $_GET['cat'] .
The function showCat is this :
function showCat($cat){
$cat= mysql_real_escape_string($cat);
connectDatabase();
$menuquery = mysql_query("SELECT c_id, c_naam FROM categorie WHERE c_parent = '0'");
while ($row = mysql_fetch_array($menuquery))
{
$c_id = $row["c_id"];
$c_naam = $row["c_naam"];
$c_parent = $row["c_parent"];
echo "<a href='producten.php?cat=$c_id'>$c_naam<br>";
if ($cat == "0")
{
}
else
{
$query2 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_parent = '$cat' "); // 5
while ($row2 = mysql_fetch_array($query2))
{
$c_id2 = $row2["c_id"]; // 11
$c_naam2 = $row2["c_naam"]; // test
if ($c_id == $cat)
{
echo "<a href='producten.php?cat=$c_id2'>- $c_naam2<br>";
}
else
{
echo "<a href='producten.php?cat=$c_id2'>- $c_naam2<br>";
$query3 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_id = '$c_id2'");
while ($row3 = mysql_fetch_array($query3))
{
$c_id3 = $row3["c_id"];
$c_naam3 = $row3["c_naam"];
$c_parent3 = $row3["c_parent"];
if ("a" == "b")
{
echo "<a href='producten.php?cat=$c_id3'>-- $c_naam3<br>";
}
}
}
}
}
}
echo "</h3>";
}
The table "Categorie" looks like this :
c_id
c_naam
c_parent
The following things occur :
If i don't set $cat or set $cat to 1 or 3 the menu is like this :
- Item 1
- Item 2
- Item 3
If i set $cat to 2 (under 2 there is 4 and 5) the menu is like this :
- Item 1
- ~Item 4
- ~Item 5
- Item 2
- ~Item 4
- ~Item 5
- Item 3
- ~Item 4
- ~Item 5
If i want under item 4 an other item, lets say 6 and 7, and i set $cat to 4, the menu becomes :
- Item 1
- ~Item 6
- ~Item 7
- Item 2
- ~Item 6
- ~Item 7
- Item 3
- ~Item 6
- ~Item 7
What can i do to make it work so it can become :
- Item 1
- Item 2
- ~Item 4
- ~~Item 6
- ~~Item 7
- ~Item 5
- Item 3
I hope i'm clear.
Thank you in advance!
If it's not possible can someone give me an example how to get an menu which already is collapesed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些应该改变的事情:
if ($cat == "0" || $cat != $c_id)
{
}
别的
{
...
如果循环中的当前类别不是给定的 $cat,则您不想显示子项目(或者我相信您所说的)。因此,您应该将该检查添加到 if 子句中。
$query2 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_parent = '$c_id' ");
不获取所选类别的子项,而是获取所选类别的子项在循环中。目前,当您选择 4 时,您将在第二个查询中检索 4 的子级,而不是第三个查询!
$query3 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_parent = '$c_id2'");
现在,您只是再次获得相同的类别,而不是它的子类别。
至于一般性评论,您可能希望以递归方式重写它,因此您不限于特定的嵌套深度。
Some things that should be changed:
if ($cat == "0" || $cat != $c_id)
{
}
else
{
...
If the current category in the loop is not the given $cat, you don't want to display the sub-items (or so i believe from what you say). So you should add that check to the if-clause.
$query2 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_parent = '$c_id' ");
Don't get the children of the selected category, get the children of the category that's selected in the loop. At the moment, when you select 4, you retrieve the children of 4 in the second query instead of the third!
$query3 = mysql_query("SELECT c_id, c_naam, c_parent FROM categorie WHERE c_parent = '$c_id2'");
Right now you're just getting the same category again instead of it's children.
As for a general remark, you might want to rewrite this in a recursive way, so you're not limited to a certain nesting depth.