rondog Posted January 10, 2008 Share Posted January 10, 2008 I have 3 tables layed out like this: Client --------------- Id Name Shoot --------------- Id Name Client_Id Tape --------------- Id Name Shoot_Id I am using a while loop to build some navigation that looks like this: <ul id="tree1"> <li><a href="#">ROTC</a> <ul> <li><a href="#">Shoot 1</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 2</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 3</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> </ul> </li> <li><a href="#">Ranger</a> <ul> <li><a href="#">Shoot 1</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 2</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 3</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> </ul> </li> <li><a href="#">Airborn</a> <ul> <li><a href="#">Shoot 1</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 2</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 3</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> </ul> </li> <li><a href="#">ASB</a> <ul> <li><a href="#">Shoot 1</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 2</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> <li><a href="#">Shoot 3</a> <ul> <li><a href="#">Tape 1</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 2</a></li> <li><a href="#">Tape 4</a></li> </ul> </li> </ul> </li> </ul> I have figured out how to make at least the top level, but am lost when trying to get into the sub areas..Here is what I came up with. The html written code is commented out and it not doing anything. <ul id="tree1"> <?php $clients = mysql_query("SELECT * FROM client") or die(mysql_error()); $shoots = mysql_query("SELECT * FROM shoot") or die(mysql_error()); $tapes = mysql_query("SELECT * FROM tape") or die(mysql_error()); while ($row = mysql_fetch_array($clients)) { echo("<li><a href=\"#\">$row[name]</a></li>"); } ?> </ul> I am pretty sure this is going to be a while loop inside of a while loop, but I dont know how to do it so some guidance would be appreciated..thanks! -Ronnie Quote Link to comment https://forums.phpfreaks.com/topic/85328-solved-while-loop-to-build-multilevel-navigation/ Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Share Posted January 10, 2008 If i understand correctly you will need multiple while loops but also specific sql searches for each client. Therefore when you want the shoot menu for client 1 you are going to need to search the shoot table for client id. Then for the tape information search for shoot id. You will also need some form of if statement. Saying if there is a shoot for client then <ul> <li><a href="#">Sub Item 1.1</a></li> <li><a href="#">Sub Item 1.2</a></li> </ul> else echo </li> same idea for the tape . Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/85328-solved-while-loop-to-build-multilevel-navigation/#findComment-435414 Share on other sites More sharing options...
rondog Posted January 10, 2008 Author Share Posted January 10, 2008 hey thanks for the prompt reply..I redid my post that replicates exactly what my menu should look like..I don't know if that changes what your reply was. Im going to read it right now. Quote Link to comment https://forums.phpfreaks.com/topic/85328-solved-while-loop-to-build-multilevel-navigation/#findComment-435415 Share on other sites More sharing options...
rondog Posted January 10, 2008 Author Share Posted January 10, 2008 I kind of understand what you are saying but still dont know what to do exactly..would I be calling a query for each shoot? Quote Link to comment https://forums.phpfreaks.com/topic/85328-solved-while-loop-to-build-multilevel-navigation/#findComment-435416 Share on other sites More sharing options...
adam291086 Posted January 10, 2008 Share Posted January 10, 2008 this may work but i am not 100% sure at all <ul id="tree1"> <?php $clients = mysql_query("SELECT * FROM client") or die(mysql_error()); $tapes = mysql_query("SELECT * FROM tape") or die(mysql_error()); while ($topmenu = mysql_fetch_array($clients)) { echo"<li><a href=\"#\">$topmenu['name']</a>"; $shoots = mysql_query("SELECT * FROM shoot WHERE Client_Id = $topmenu['Id'] ") or die(mysql_error()); $mysql_count = mysql_num_rows($shoots); if($mysql_count >0) { while ($shootmenu = mysql_fetch_array($shoots)) { echo "<ul>"; echo"<li><a href=\"#\">$shootmenu['name']</a></li>"; echo "</ul>"; } else { echo "</li>"; } } ?> </ul> Quote Link to comment https://forums.phpfreaks.com/topic/85328-solved-while-loop-to-build-multilevel-navigation/#findComment-435431 Share on other sites More sharing options...
rondog Posted January 10, 2008 Author Share Posted January 10, 2008 hell ya man that got me off to a good start..I had to move the <ul> and </ul> out of the while loop and just sit them right outside. Here is the output code I ended up using..and you can see the menu here: http://dopserv1.com/hosted/army/rotc <?php $clients = mysql_query("SELECT * FROM client") or die(mysql_error()); while ($topmenu = mysql_fetch_array($clients)) { echo "<li><a href=\"#\">$topmenu[name]</a>\n"; $shoots = mysql_query("SELECT * FROM shoot WHERE client_id = $topmenu[id]") or die(mysql_error()); $shoot_count = mysql_num_rows($shoots); if($shoot_count > 0) { echo "<ul>\n"; while ($shootmenu = mysql_fetch_array($shoots)) { echo "<li><a href=\"#\">$shootmenu[name]</a>\n"; $tapes = mysql_query("SELECT * FROM tape WHERE shoot_id = $shootmenu[id]") or die(mysql_error()); $tape_count = mysql_num_rows($tapes); if($tape_count > 0) { echo "<ul>\n"; while($tapemenu = mysql_fetch_array($tapes)) { echo "<li><a href=\"#\">$tapemenu[name]</a>\n"; } echo "</ul>\n"; } else { echo "</li>\n"; } } echo "</ul>\n"; } else { echo "</li>\n"; } } ?> is their some sort of php option that formats white space? It echoes in the code all one line so I threw in some line breaks, but im gonna do some searching for some auto formating..if you know of anything throw me some options..thanks adam for the help I really appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/85328-solved-while-loop-to-build-multilevel-navigation/#findComment-435818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.