Need some help with nested loops.....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jm999
Forum Commoner
Posts: 28
Joined: Tue Aug 29, 2006 11:58 am

Need some help with nested loops.....

Post by jm999 »

pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here is the query that will get all of the necessary info:

[syntax="sql"]SELECT * FROM D_ARTICLES LEFT JOIN D_SUB_CAT ON D_ARTICLES.D_SUB_CAT_ID=D_SUB_CAT.D_SUB_CAT_ID LEFT JOIN D_CAT ON D_SUB_CAT.D_CAT_ID=D_CAT.D_CAT_ID WHERE D_ARTICLES.D_A_VISIBLE='1' AND D_ARTICLES.D_TYPE_ID='2' AND D_CAT.D_CAT_ID='2' ORDER BY D_CAT.D_CAT_ID, D_SUB_CAT.D_SUB_CAT_ID
What I need to do is display menus like:

TYPE NAME
-SUBCATEGORY NAME
-ARTICLE
-ARTICLE
-SUBCATEGORY 2 NAME
-ARTICLE
-ARTICLE

TYPE NAME 2
-SUBCATEGORY NAME
-ARTICLE
-ARTICLE

and so on. I'm pretty much a novice with PHP and don't really know how to approach sorting the results. I know I should use nested loops so if anyone can provide me with some sample code, suggestions, or even a tutorial then I would greatly appreciate it. Thanks!


pickle | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
visitor-Q
Forum Commoner
Posts: 72
Joined: Mon Feb 05, 2007 1:40 am

Post by visitor-Q »

this is just an example:

Code: Select all

<?php
        $sql = "SELECT * FROM D_ARTICLES
                LEFT JOIN D_SUB_CAT
                ON D_ARTICLES.D_SUB_CAT_ID=D_SUB_CAT.D_SUB_CAT_ID
                LEFT JOIN D_CAT ON D_SUB_CAT.D_CAT_ID=D_CAT.D_CAT_ID
                WHERE D_ARTICLES.D_A_VISIBLE='1'
                AND D_ARTICLES.D_TYPE_ID='2'
                AND D_CAT.D_CAT_ID='2'
                ORDER BY D_CAT.D_CAT_ID, D_SUB_CAT.D_SUB_CAT_ID";
        $query = mysql_query($sql);

        while($row = mysql_fetch_array($query)){
                        echo "<b>". $row['col1'] ."</b><br />n";
                        echo "-". $row['col2'] ."<br />\n";
                        echo "-". $row['col3'] ."<br />\n";
                        echo "-". $row['col4'] ."<br />\n";
        }
?>
jm999
Forum Commoner
Posts: 28
Joined: Tue Aug 29, 2006 11:58 am

Post by jm999 »

Well that would output the data like:

TYPE NAME
SUBCATEGORY NAME
ARTICLE
TYPE NAME
SUBCATEGORY NAME
ARTICLE 2

I need it to output

TYPE NAME
SUBCATEGORY NAME
ARTICLE 1
ARTICLE 2
ARTICLE 3
SUBCATEGORY 2 NAME
ARTICLE 4
ARTICLE 5
ARTICLE 6

TYPE NAME 2
SUBCATEGORY NAME
ARTICLE 1
ARTICLE 2

and so on.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you maybe use some example data so we can see what you actually want. I am having a whale of a time trying to figure out your data relationships.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I think the first two threads linked from Useful Posts should be of interest.
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post by Begby »

You can do this....

Code: Select all

while($row = mysql_fetch_array($query))
{ 
   $categories[$row['type']][$row['subcategory']][] = $row['article']
}


foreach( $categories as $category = > $subs )
{
  echo $category.'<br />' ;
  foreach( $subs as $sub => $articles)
  {
    echo $sub.'<br />' ;
    foreach( $articles as $article )
    {
       echo $artice.'<br />' ;
    }

  }
}
Post Reply