Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 8th, 2012, 02:10 PM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default Loops in php -help

hey.

When I was in school we learned to program in java.
the loop formats where very simple to me.

for( initialization; termination; increment)
{
statements;
}

the format in which the for loop is in makes it very easy for me to understand when the loop begins and where it ends.

however I am having a very difficult time understanding the loops in php.
in chapter 2 page 63 there is a for-each loop. it was easy enough for me to understand. the loop was used with an array so I understood that when the array ended so did the loop. the problem for me though was understanding how to do a for-each loop without an array. if I lets say wanted to print 10 times on the screen “hello” - how would I go about doing it with a for-each loop? (I don't think that using a while loop is very efficient)

also. in chapter 3 page 93. there is a while loop in the code that prints the query of movies with their movie types. I don't understand the increment of this loop. how does PHP know to move to the next row if there is no statement telling it to like in the example in chapter 2 page 70 (the one that prints the number of movies the user selected) where there was a statement

$numlist = $numlist + 1;

telling the loop to move to the next on the list.


I not sure if I explained my difficulty easily enough to understand - but I’m sure that whoever learned java will know what I mean.

please help me.

thanks in advance
Reply With Quote
  #2 (permalink)  
Old January 9th, 2012, 04:46 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

You only use a foreach with an array, if you only wanted to output 'hello' 10 times then you would use a normal for loop like so;
Code:
for($i=0; $i <= 10; $i++)
{
echo 'hello' . "\n";
}
I am guessing here, with regard to the Chapter 3 Page 93 example, but if the while loop is grabbing data from the database with something like:
Code:
while($row = mysql_fetchrow($result))
then the loop counter will come from the database when the next row is obtained.
Reply With Quote
  #3 (permalink)  
Old January 9th, 2012, 07:24 AM
Registered User
Points: 32, Level: 1
Points: 32, Level: 1 Points: 32, Level: 1 Points: 32, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default

thanks for the reply.

In your answer you said that the while loop in chapter 3 page 93- changes the row because the end of the row of the database.

the problem is that I want to dictate when the while loop will switch a row in the database (it may sound silly but I'm thinking maybe I will want to have an if argument in the loop that will determine if I want to do a second loop on the same row)
Reply With Quote
  #4 (permalink)  
Old January 9th, 2012, 09:53 AM
Friend of Wrox
Points: 651, Level: 9
Points: 651, Level: 9 Points: 651, Level: 9 Points: 651, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2011
Location: North East UK
Posts: 125
Thanks: 0
Thanked 24 Times in 24 Posts
Default

Greetings,

You could do that as the next row will only be obtained from the db when the current iteration ends. So if you delay the end of the loop to process the current row again then that will work fine, however be careful you do not create an endless loop which will be down to the if condition used to determine if the current row should be processed again.

And I'm not going to ask why you'd want to waste time processing something you've just processed.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
Loops deontae45 VB.NET 2002/2003 Basics 2 September 28th, 2006 03:48 PM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
Loops Hudson40 Excel VBA 2 January 20th, 2005 07:40 AM
loops flyn Javascript How-To 1 November 21st, 2003 04:59 AM



All times are GMT -4. The time now is 12:21 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.