Page 1 of 1

Secondary Loop Malfunction

Posted: Mon Jun 19, 2006 6:28 pm
by $var
Good Day Devnet!

I am using a database to populate a menu that uses CSS and JavaScript to create a slide-out sub menu.

It uses one loop to create the categories, and then a loop inside for the subcategories.
The second loop isn't working. It populates the first menu, but then doesn't finish and no other menus are loaded.
I have provided links to working and non working examples in the below code.. thanks a lot for looking.

Code: Select all

echo "<div id='dhtmlgoodies_slidedown_menu'><ul>" ;
	// Begin PHP Loop #1 - Works ------->  
	$sql = "SELECT COUNT(*) FROM var_section_names";
	$numrecord = mysql_query($sql);
	$numrecords = mysql_fetch_array($numrecord);
	$numrecords[0];
		
	$intRecsPerPage=12;
	if($_POST["intpage"]=="")
	{
		$intpage=1;
	}
	$sql = "SELECT * FROM var_section_names ORDER BY Section_N_Weight ASC";
	$result = mysql_query($sql) or die (mysql_error());
	for($x = (($intpage-1) * $intRecsPerPage); $x < (($intpage-1) * $intRecsPerPage) + $intRecsPerPage; $x++)
		{
			if($x >= $numrecords[0])
				{
					break;
				}
			$sectionresults = mysql_fetch_array($result);
			$sectionName = $sectionresults["Section_N_Name"];
			$sectionNID = $sectionresults["Section_N_ID"];

			echo "<li><a href='#'>$sectionName</a>";
			echo "<ul>";
			// Begin Static Loop - Works -------> http://www.thevariables.com/_/
			echo "<li><a href'#'>Roll Out 1</a></li><li><a href'#'>Roll Out 1</a></li>";
			// End Static Loop - Works ------->
			// Begin PHP Loop #2 - Doesn't Work -------> http://www.thevariables.com/_/indexBad.php
			 $sql2 = "SELECT COUNT(*) FROM var_sections WHERE Section_AID = '$sectionNID'";
					$numrecord = mysql_query($sql2);
					$numrecords = mysql_fetch_array($numrecord);
					$numrecords[0];
						
					$intRecsPerPage=12;
					if($_POST["intpage"]=="")
					{
						$intpage=1;
					}
					$sql2 = "SELECT * FROM var_sections WHERE Section_AID = '$sectionNID' ORDER BY Section_Weight ASC";
					$result = mysql_query($sql2) or die (mysql_error());
					for($x = (($intpage-1) * $intRecsPerPage); $x < (($intpage-1) * $intRecsPerPage) + $intRecsPerPage; $x++)
						{
							if($x >= $numrecords[0])
								{
									break;
								}
							$sectionsresults = mysql_fetch_array($result);
							$sectionsPT = $sectionsresults["Section_PT"];
							$sectionsID = $sectionsresults["Section_ID"];
							
							echo "<li><a href='#'>$sectionsPT</a></li>";
			
			}
			echo "</ul>"; 
			// End PHP Loop #2 - Doesn't Work------->					
			echo "</li>";
			}
			// End PHP Loop #1 - Works ------->
			echo "</ul>";

Posted: Mon Jun 19, 2006 6:31 pm
by Luke
I don't know if this is the problem, but I would use a different variable name for each loop (instead of x both times use a and b)

Posted: Mon Jun 19, 2006 6:40 pm
by $var
yah, i tried that too, i updated it to:

Code: Select all

$sql2 = "SELECT * FROM var_sections WHERE Section_AID = $sectionNID ORDER BY Section_Weight ASC";
			$result = mysql_query($sql2) or die (mysql_error());
thanks though

Posted: Thu Jun 22, 2006 9:38 pm
by $var
i've still bee unable to solve this riddle.

why would a loop that is closing not allow a parent loop to continue? anyone?

Posted: Thu Jun 22, 2006 10:07 pm
by tecktalkcm0391
Try:

Code: Select all

echo "<div id='dhtmlgoodies_slidedown_menu'><ul>" ; 
        // Begin PHP Loop #1 - Works ------->  
        $sql = "SELECT COUNT(*) FROM var_section_names"; 
        $numrecord = mysql_query($sql); 
        $numrecords = mysql_fetch_array($numrecord); 
        $numrecords[0]; 
                
        $intRecsPerPage=12; 
        if($_POST["intpage"]=="") 
        { 
                $intpage=1; 
        } 
        $sql = "SELECT * FROM var_section_names ORDER BY Section_N_Weight ASC"; 
        $result = mysql_query($sql) or die (mysql_error()); 
        for($x = (($intpage-1) * $intRecsPerPage); $x < (($intpage-1) * $intRecsPerPage) + $intRecsPerPage; $x++) 
                { 
                        if($x >= $numrecords[0]) 
                                { 
                                        break; 
                                } 
                        $sectionresults = mysql_fetch_array($result); 
                        $sectionName = $sectionresults["Section_N_Name"]; 
                        $sectionNID = $sectionresults["Section_N_ID"]; 

                        echo "<li><a href='#'>$sectionName</a>"; 
                        echo "<ul>"; 
                        // Begin Static Loop - Works -------> http://www.thevariables.com/_/ 
                        echo "<li><a href'#'>Roll Out 1</a></li><li><a href'#'>Roll Out 1</a></li>"; 
                        // End Static Loop - Works -------> 
                        // Begin PHP Loop #2 - Doesn't Work -------> http://www.thevariables.com/_/indexBad.php 
                         $sql2 = "SELECT COUNT(*) FROM var_sections WHERE Section_AID = '$sectionNID'"; 
                                        $numrecord = mysql_query($sql2); 
                                        $numrecords = mysql_fetch_array($numrecord); 
                                        $numrecords[0]; 
                                                
                                        $intRecsPerPage=12; 
                                        if($_POST["intpage"]=="") 
                                        { 
                                                $intpage=1; 
                                        } 
                                        $sql2 = "SELECT * FROM var_sections WHERE Section_AID = '{$sectionNID}' ORDER BY Section_Weight ASC"; 
                                        $result = mysql_query($sql2) or die (mysql_error()); 
                                        for($y = (($intpage-1) * $intRecsPerPage); $y < ((($intpage-1) * $intRecsPerPage) + $intRecsPerPage); $y++) 
                                                { 
                                                        if($y >= $numrecords[0]) 
                                                                { 
                                                                        break; 
                                                                } 
                                                        $sectionsresults = mysql_fetch_array($result); 
                                                        $sectionsPT = $sectionsresults["Section_PT"]; 
                                                        $sectionsID = $sectionsresults["Section_ID"]; 
                                                        
                                                        echo "<li><a href='#'>$sectionsPT</a></li>"; 
                        
                        } 
                        echo "</ul>"; 
                        // End PHP Loop #2 - Doesn't Work------->                                    
                        echo "</li>"; 
                        } 
                        // End PHP Loop #1 - Works -------> 
                        echo "</ul>";

Posted: Tue Jul 04, 2006 10:31 am
by $var
hmm... nope, this doesn't work either...
you'd think that something could loop inside of something without so much trouble.