Jump to content

Recommended Posts

So I am making a navigation system for a website and I want all the links to be in a table on a database so that the links can easily be added to or subtracted from when found necessary. 

 

The way the navigation works is that it is a horizontal navbar with drop down menus filled with sub categories

 

I have two tables set up

 

Table 1 is called categories  and includes a unique ID and a Link Name. for example

 

CID = 1

NAME = Houses

 

CID = 2

NAME = Apartments

 

 

I then have a second table called subcategories with 3 columns.  A unique ID a Link name and a third column which saves the CID of its parent category

 

for example:

 

SCID = 1

CATREF = 2

NAME = Big Apartments

 

SCID = 2

CATREF = 1

NAME = Small Houses

 

 

My Problem is that Im trying to set up my navigation and I think the issue is that I am trying to put a loop inside a loop and thats what is causing the error.  I don't know much about code - but I am a quick learner and can navigate my way around enough to understand what is going on. 

 

this is the code I have so far:

 

<?php do { ?>
  <li class="firstmenu"><a href="<?php echo $row_TopCategories['CATID']; ?>" id="grab<?php echo $row_TopCategories['CATID']; ?>" class="firstmenuHL">
  	<?php echo $row_TopCategories['CNAME']; ?></a>
      <!-- DROP DOWN MENU STARTS HERE -->  
        <ul onmouseover="document.getElementById('grab<?php echo $row_TopCategories['CATID']; ?>').className='firstmenuDARK';" 
        	onmouseout="document.getElementById('grab<?php echo $row_TopCategories['CATID']; ?>').className='firstmenuHL';">
        
          <! SUB CATEGORIES START HERE -->
          <li><a href='#'><?php echo $row_SubCategories['SCNAME']; ?></a></li>
          <!-- SUB CATEGORIES END HERE -->
         </ul>
      <!-- DROP DOWN MENU ENDS HERE -->



        </li>
  <?php } while ($row_TopCategories = mysql_fetch_assoc($TopCategories)); ?></ul>

 

 

I thought I just had to do something like while( CID = CATREF) do{  ((i understand thats not probably coded - just conveying the idea))

 

 

So please help me figure this out - if you need more of my code please let me know!

Link to comment
https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/
Share on other sites

your code above has

 

do {

    ....

} while ($row_TopCategories = mysql_fetch_assoc($TopCategories));

 

At the start of that loop you reference $row_TopCategories, but you don't assign anythig to it until the while() at the end of the loop.

 

Also you mention loop within loop. Where's the other loop?

in that exact code I haven't put the second loop yet - I want the loop to appear where it says <! SUB CATEGORIES START HERE --> and end where it says it ends - the closest I have gotten to getting it to work is this:

 

  <?php if ($row_TopCategories['CATREF'] == $row_SubCategories['CATID']) do { ?>
          <li><a href='#'><?php echo $row_SubCategories['SCNAME']; ?></a></li>
           <?php } while ($row_SubCategories = mysql_fetch_assoc($SubCategories)); ?>

 

But what this ends up doing is putting all of the submenu items into the first submenu and not dividing them up into their corresponding parent links-

 

 

 

I've gotten closer- I now have the first submenu only populated with links that belong in it - but now all the submenus after it remain empty.

 

this is the code I have right now:

 

<?php do { ?>
  <li class="firstmenu"><a href="<?php echo $row_TopCategories['CATID']; ?>" id="grab<?php echo $row_TopCategories['CATID']; ?>" class="firstmenuHL">
  	<?php echo $row_TopCategories['CNAME']; ?><?php echo $row_TopCategories['CATID']; ?></a>
        
     
        <ul onmouseover="document.getElementById('grab<?php echo $row_TopCategories['CATID']; ?>').className='firstmenuDARK';" 
        	onmouseout="document.getElementById('grab<?php echo $row_TopCategories['CATID']; ?>').className='firstmenuHL';">
        
        <?php do { ?>
        	<?php if ($row_TopCategories['CATID'] == $row_SubCategories['CATREF']) { 
		echo "<li><a href='#'>".$row_SubCategories['SCNAME'].$row_SubCategories['CATREF'].$row_TopCategories['CATID']."</a></li>"; } ?>
           <?php } while ($row_SubCategories = mysql_fetch_assoc($SubCategories)); ?>
           
         </ul>
       


        </li>
  <?php } while ($row_TopCategories = mysql_fetch_assoc($TopCategories)); ?>

 

This is the code just for that main section - can anyone figure out why it only populates the first list?

are you wanting something like this

<?php

$sql = "SELECT c.catid, c.cname , sc.scatid, sc.scname 
        FROM categories c
        INNER JOIN subcategories sc ON c.catid = sc.catref
        ORDER BY c.cid, sc.scid";
$res = mysql_query ($sql);

$prevcat = '';

echo '<ul>';
while (list($cid, $cat, $scid, $subcat) = mysql_fetch_row($res))
{
    if ($prevcat != $cid)
    {
        if ($prevcat != '')
        {
            echo '</ul>';
        }
        echo "<li class="firstmenu"><a href='$cid' id='grab$cid' > $cat </a></li>";
        echo "<ul>";
        $prevcat = $cid;
    }
    echo "<li><a href='$scid'> $subcat</a></li>";  
}
echo '</ul>';
echo '</ul>';

?>

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.