TalkPHP
 
 
Account Login
Latest Articles
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
» CSRF POST Token Protection
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 05-11-2008, 03:22 PM   #1 (permalink)
The Visitor
 
Join Date: Feb 2008
Posts: 4
Thanks: 2
pabin is on a distinguished road
Wink Need help on nested loop!

Could somebody overhere expalin how nested loop works because i am quite confuse on this issue?
pabin is offline  
Reply With Quote
Old 05-11-2008, 08:31 PM   #2 (permalink)
The Frequenter
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas
Posts: 430
Thanks: 13
delayedinsanity is on a distinguished road
Default

Nested:

1) A process or activity self-contained within a larger entity, that retains its identity and characteristics for reporting and analysis.

2) Nested is the seventh studio album by Bronx-born singer, songwriter, and pianist Laura Nyro, released in 1978 by Columbia Records.

What in particular about it are you having trouble with?
-m
delayedinsanity is offline  
Reply With Quote
Old 05-11-2008, 11:55 PM   #3 (permalink)
The Frequenter
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 482
Thanks: 15
Village Idiot is on a distinguished road
Default

A nested loop is a loop inside a loop. For instance, if you are reading posts on a forum sequentially, you would read:
Topic 1
Post 1
Post 2
Post 3
Topic 2
Post 1
Post 2
Post 3
So on the so forth.

If you want a purely programmatic example, run this code.
PHP Code:
<?php
for($i=0;$i<10;$i++)
{
    echo 
"Main Level $i - ";
    for(
$j=0;$j<10;$j++)
    {
        echo 
"Sub-Level $j - ";
    }
    echo 
"<br />";
}
?>
Here is what it will output:
http://justanotherportfolio.com/nest.php
__________________
There are two ways to write bug-free code, only the third one works.
Village Idiot is offline  
Reply With Quote
Old 05-12-2008, 12:00 AM   #4 (permalink)
The Addict
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 338
Thanks: 19
sketchMedia is on a distinguished road
Default

A nested loops is a loop that has a loop within it. First of all the outer loop's first iteration triggers the inner loop then the inner loop continues until completion, then the outer loop cycles again thus causing the inner again to trigger again.

put into a simple example:

PHP Code:
for($i=0;$i 10$i++)
{
    echo 
'Line '$i 1' ';
    for(
$j=0$j 5$j++)
    {
        echo 
$j;
    }
    echo 
'<br />';

outputs :
Code:
Line 1 01234
Line 2 01234
Line 3 01234
Line 4 01234
Line 5  01234
Line 6 01234
Line 7 01234
Line 8 01234
Line 9 01234
Line 10  01234
basically the first loop out puts : 'Line 1' then the nested loop starts and echos numbers 0-4, then the first loop echos 'Line 2' then triggers the nested loop again and the cycle continues until the first loop finishes.

hope i made that helps
__________________
There's nothing an agnostic can't do if he doesn't know whether he believes in anything or not
sketchMedia is offline  
Reply With Quote
Old 05-12-2008, 10:27 AM   #5 (permalink)
The Addict
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 338
Thanks: 19
sketchMedia is on a distinguished road
Default

damn village beat me! lol the code looks similar aswell, anyway both posts should help you understand
__________________
There's nothing an agnostic can't do if he doesn't know whether he believes in anything or not
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:41 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0