Basically I have a table in MYSQL
Name ID Parent
Bob 321 root
Sue 32 root
Boo 393 32
Geek 72 393
Sam 312 393
Tovoc 341 321
Reese 322 393
Sheep 811 341
I want to create a menu that outputs a menu structure with submenus based on the parent ID
So it should output
Bob
Tovoc
Sheep
Sue
Boo
Sam
Geek
Reese
The child entries are linked with the parent ID and if they have a parent they become a child. Ultimately it would build a multidimentual array. Clues?
Heres what I got so far. It works, but I can't figure out how to increase or reset the margin style properly to get a nice structure view. I have the margin-left increase by 10px in every child elements found, then reset back to 1 if the parent is user
isparent(user,1);
function isparent($Parent,$if=1){
$stdr = 'SELECT * FROM CraveBins WHERE UserID = '.$_SESSION[CraveUserID].' AND Parent=\''.$Parent.'\' ORDER BY Name ASC';
$resultMainMenu = mysql_query($stdr) or die('421 '.$stdr.mysql_error());
if(mysql_num_rows($resultMainMenu)){
while($row = mysql_fetch_array($resultMainMenu)){
$if++;
echo '<div class="binDir" onclick="setBin(this)" id="BinListUser" style="margin-left:'.(5+($if * 10)).'px" driID="'.$row[ID].'">'.$row['Name'].'</div>'; // echo main menu
$n = isparent($row['ID'],$if);
}//end while
}else{
$if=1;
} // if rows
}//end function
I don't understand your $if++
. I thought this was your level of indentation. If so: remove the $if++
, modify the inner isparent($row['ID'],$if+1)
and you should be fine ... I guess.
Alternatively, move your $if++
outside the while loop (before ;o)
Also: you don't need mysql_num_rows(...)
, I believe. Just start fetching, and it'll return nothing if it's finished. Also, don't use mysql_*, use mysqli or pdo.
If your $if
was meant to count the children, just call it $n
, don't mix depth and count.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.