alexisfromboston Posted October 26, 2007 Share Posted October 26, 2007 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 More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 do you have a call to $row_TopCategories = mysql_fetch_assoc($TopCategories) before the do{} loop starts? Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378822 Share on other sites More sharing options...
alexisfromboston Posted October 26, 2007 Author Share Posted October 26, 2007 not quite sure what you are asking./.. Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378835 Share on other sites More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 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? Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378840 Share on other sites More sharing options...
alexisfromboston Posted October 26, 2007 Author Share Posted October 26, 2007 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- Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378851 Share on other sites More sharing options...
alexisfromboston Posted October 26, 2007 Author Share Posted October 26, 2007 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? Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378885 Share on other sites More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 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>'; ?> Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378929 Share on other sites More sharing options...
alexisfromboston Posted October 26, 2007 Author Share Posted October 26, 2007 i have to be honest and say this is a little too confusing for me to figure out. I wish I could - because its probably exactly what I need Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378933 Share on other sites More sharing options...
Barand Posted October 26, 2007 Share Posted October 26, 2007 I gaven't done the mouseovers etc but just wanted to know if the general output was right if you run it. Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378936 Share on other sites More sharing options...
alexisfromboston Posted October 26, 2007 Author Share Posted October 26, 2007 here we go - this is what I want: <a href="http://www.juggalicious.com/TESTING/example1.php"> EXAMPLE 1</a> this is as close as I've gotten: <a href="http://www.juggalicious.com/TESTING/example2.php"> EXAMPLE 2</a> Link to comment https://forums.phpfreaks.com/topic/74918-loop-in-a-loop-help-me-for-chocolate/#findComment-378938 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.