Jump to content

[SOLVED] While loop to build multilevel navigation


rondog

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.