Jump to content

help with using list and arranging mysql results


tryingtolearn

Recommended Posts

Been using a function I found here a while back for listing categories and sub categories and it works perfect.

function listSubcats ($parent, $level=0){	
global $abc;
$sql = "SELECT id, title FROM Cat WHERE parent = $parent";	
$res = mysqli_query($abc, $sql);	
while (list($id, $title) = $res->fetch_row())	{		
$indent = str_repeat('-', $level);		
echo "<OPTION value='$id'>$indent $title</OPTION>\n";		
listSubcats ($id, $level+1);                       
// list its subcats	
}
}

 

But this is the first time I need change the way the results are displayed.

And that leads me to realizing I am a bit confused on how list really works.

 

The mysql table has the typical ID, CatName, parent

 

I need to have each Cat with all subcats related to it listed in its own div.

But whatever I try I cannot get the placements of the opening and closing divs in the right spots.

 

So can anyone tell if its even possible to do it with this function.

 

I did work it out using multiple queries but then read on alot of forums that queries inside while statements is not good.

 

Just looking for he best (Correct) way of geting the results laid out properly.

like this


<div class="one-third column">
Cat1
</div>
<div class="one-third column">
Cat 2
</div>
<div class="one-third column">
Cat 3
- Subcat 1
- Subcat 2
-- Sub Subcat 1
</div>
<div class="one-third column">
Cat 4
</div>

 

Hope that makes sense... Thanks for any guidance.

Link to comment
Share on other sites

try

function listSubcats ($parent, $level=0){    
    global $abc;
    $sql = "SELECT id, title FROM Cat WHERE parent = $parent";    
    $res = mysqli_query($abc, $sql);
    while (list($id, $title) = $res->fetch_row())    {        
        if ($level==0) echo "<div class='one-third column'>\n";   
        $indent = str_repeat('-', $level);        
        echo "$indent$title<br />\n";        
        listSubcats ($id, $level+1);                       
        // list its subcats    
        if ($level==0) echo "</div>\n";   
    }
}

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.