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 开发者_StackOverflow中文版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?
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.
精彩评论