Page 1 of 1

Need some help with nested loops.....

Posted: Wed Mar 07, 2007 4:28 pm
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]

Posted: Wed Mar 07, 2007 5:15 pm
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";
        }
?>

Posted: Wed Mar 07, 2007 5:42 pm
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.

Posted: Wed Mar 07, 2007 7:08 pm
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.

Posted: Thu Mar 08, 2007 8:25 am
by feyd
I think the first two threads linked from Useful Posts should be of interest.

Posted: Thu Mar 08, 2007 10:39 am
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 />' ;
    }

  }
}