PHP Nested Loop Tutorial

35454 Users read it.
by Zo Zo (July 4, 2003)
A tutorial about nested loops in PHP and MySQL.

User Rating (65 votes)
11 reviews available | Read the reviews »
User Level:Beginner
Product:Dreamweaver MX
Database:MySQL
Server Model:PHP ImpAkt 1(UD4), PHP MySQL (DWMX), PHP PHAkt 1 (UD4)
Browser:All

I guess since you are looking for a nested loop "solution" you already familiar with a simple database structure used in this method.



Open DW, create a table.

Create a recordset named "menu" to show only PARENT menu items which have SID value "0":

Insert dynamic text for MENU name field.

Create another Recordset and name it "submenu".
Note the SID equals $row_menu['id'], we will modify this later in the code view.

Create a second table under the first one, insert the Dynamic text from SUBMENU "menuname" Recordset and apply REPEAT REGION to both tables.

Now go to CODE VIEW and place the Recordset "submenu" under the closing </table> tag of the parent menu item table.
Look into the query where we typed earlier $row_menu['id'], we need slightly to change it.
Replace: $query_submenu = "SELECT * FROM menu WHERE sid = $row_menu['id']";
With: $query_submenu = "SELECT * FROM menu WHERE sid =".$row_menu['id']."";

This procedure could me much "cleaner" but i won't complicate things.
Dont forget to apply <?php tags ?>

Now we will loop the second table.
Type in repeat manualy, Dreamweaver will "complain" if you try to do it in design view.

<?php do { ?>
YOUR SECOND TABLE
<?php while ($row_submenu = mysql_fetch_assoc($submenu)); ?>

Next we are going to....hey, thats it!!!

 

 

 

User Reviews

Total of 11 reviews
Awesome.....
Written by Adrian Lord on November 13, 2009

I just wanted to say a big THANKS for this tutorial. I have been looking for ages for a simple step by step guide on how to next tables within tables. I followed your guide (although I have separate tables for the 'menu' and 'submenu' parts) and it ...

SOLVED - THIS SOLUTION WORKS
Written by A. Hos on June 5, 2008

Just read Chris Pfrang's post (10 January 2006, 10:51). I followed the tutorial THEN implemented his two small fixes/adjustments. It worked like a charm! Thank you Chris and Zo Zo.

RE: Review of Jayson's code
Written by student101 on May 19, 2008

Works similar to my asp version.Thank you for taking the time. Cheers

RE: Review of Jayson's code
Written by student101 on May 19, 2008

Thank you! Cheers

Review of Jayson's code
Written by John Robertson on May 18, 2008

Hi again Jayson. I compared our code and see why yours was not working. mysql_select_db($database_cnparent, $cnparent);$query_menu = 'SELECT * FROM parent WHERE sid = 0 ORDER BY menuname ASC';$menu = mysql_query($query_menu, $cnparent) or ...

RE: no code only pictures?
Written by John Robertson on May 18, 2008

Greetings Jason Thank you for your persistance in wanting to find an answer to this article. For a long time (would you believe - now nearly TWO YEARS since I first asked the same question) I did not believe anyone could solve this (or want to - ...

RE: Has anyone been successful in making this tutorial work?
Written by student101 on May 18, 2008

<?php require_once('../../Connections/cnparent.php'); ?><?phpif (!function_exists('GetSQLValueString')) {function GetSQLValueString($theValue, $theType, $theDefinedValue = '', $theNotDefinedValue = '') { $theValue = get_magic_quotes_gpc() ...

no code only pictures?
Written by student101 on May 17, 2008

Pretty poor, due to no real code and only pictures, nobody can get this to work the way you have explained. Post your entire page, love to see what that looks like? Cheers

Follow us on twitter Subscribe to our RSS feed
Activate your free membership today | Login | Currency