Jump to content

Problem with multiple navigation queries


PHPiSean

Recommended Posts

Here's my code

 

<?php

//SECOND LINE
/*Needs to run 2 queries. One to recieve the main categories and one to recieve the 
* sub categories.
*/


$mainlinks = mysql_query("select * from ns_categories");

while ($catlinks = mysql_fetch_array($mainlinks)) {
    $catlinkname = $catlinks['name'];
    $catlinkid = $catlinks['id'];
    
    $sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid'");
    
    echo "<ul>";
    echo "<li><a href='products.php?mc=$catlinkid'>$catlinkname</a></li>";
    
    
    while ($catsublinks = mysql_fetch_array($sublinks)) {
        $sublinkname = $catsublinks['name'];
        $sublinkid = $catsublinks['id'];
        
        echo "<ul>";
        echo "<li><a href='products.php?sc=$sublinkid>$sublinkname</a></li>";
        echo "</ul>";
    
             
    }
        
}

echo "</ul>";





?>

 

It will return all of the main categories, but it will only return the second sub category in the table and not the first.

 

Any ideas?

Thanks

Are you sure it's not because you're ending the ul tag in the wrong place?

 

Seems like the code should be:

 

<?php
$mainlinks = mysql_query("select * from ns_categories");

while ($catlinks = mysql_fetch_array($mainlinks)) {
    $catlinkname = $catlinks['name'];
    $catlinkid = $catlinks['id'];
    
    $sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid'");
    
    echo "<ul>";
    echo "<li><a href='products.php?mc=$catlinkid'>$catlinkname</a></li>";
    
    
    while ($catsublinks = mysql_fetch_array($sublinks)) {
        $sublinkname = $catsublinks['name'];
        $sublinkid = $catsublinks['id'];
        
        echo "<ul>";
        echo "<li><a href='products.php?sc=$sublinkid>$sublinkname</a></li>";
        echo "</ul>";
    
             
    }
echo "</ul>";
}
?>

 

If that is not the issue, I would do a var_dump to check and see what mysql_fetch_array returns, by adding:

 

var_dump($catlinks);

under

while ($catlinks = mysql_fetch_array($mainlinks)) {

 

And also adding:

 

var_dump($catsublinks);

under

while ($catsublinks = mysql_fetch_array($sublinks)) {

 

And the array returned from both of them should have all the categories and sub categories that are inside your table, if not, there is an issue with the query, which would not seem right to me as long as the data is actually in there.

 

Let me know!

Hi

 

after I did the var dump, I found that the strings were actually there. The </ul> surprisingly is in the right place. The thing is, the query will only return the second row, which is odd. If the var_dump returns the values, but doesn't echo them. It must be something wrong with my while statement, but I'm stumped.

Sorry it took so long for  a response.

 

You're while loop seems fine, I'm sure it's a problem rendering the html..

 

Try this and tell me if it works:

$mainlinks = mysql_query("select * from ns_categories");

while ($catlinks = mysql_fetch_array($mainlinks)) 
{
    $catlinkname = $catlinks['name'];
    $catlinkid = $catlinks['id'];
    
    $sublinks = mysql_query("select * from ns_subcategories where mainid='$catlinkid' ");
    
    $html .= "<ul>";
    $html .= "<li><a href='products.php?mc=$catlinkid'>$catlinkname</a></li>";
    
    
    while ($catsublinks = mysql_fetch_array($sublinks)) {
        $sublinkname = $catsublinks['name'];
        $sublinkid = $catsublinks['id'];
        
        $html .= "<ul>";
        $html .= "<li><a href='products.php?sc=$sublinkid>$sublinkname</a></li>";
        $html .= "</ul>";
    
             
    }
        
}

$html .= "</ul>";

echo $html;

 

Edit: Sorry, did not realise the topic was solved, I based this off of the replies, not the SOLVED title.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.