TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 08-30-2009, 05:36 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default While loop is over my head

Hi,

The WHILE and FOREACH loops below are elementary, yet how they work keeps evading my mental grasp. Here is a UDF:

PHP Code:
$c_pettype "horse" ;
 
 function 
getpets($c_pettype)
 {
 
  
$q "SELECT *
       FROM t_petdata
         WHERE pettype = '
$c_pettype'" ;
          
 
$result mysql_query($q) or die ('bad query') ;

 
$j ;

 [
color="Red"]while($row mysql_fetch_assoc($result))[/color]
 { 
     [
color="Red"]foreach($row as $key => $value)[/color]
     {
         
$array[$j][$key] = $value ;
     }
                
     return 
$array ;
 }
 

So, I get the arrays, and the general concept of looping, etc., but the concepts of "WHILE($row = ...)" and "FOREACH($row as...) are not clear to me. I mean, where does "$row" come from?

Is there a way to help me understand this on a simple level?

Thanks!

Dave

Last edited by codefreek : 09-01-2009 at 05:32 PM. Reason: php tags added, please read note, Thank you!
Dave is offline  
Reply With Quote
Old 08-30-2009, 06:13 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

As you know, while loops will execute as long as a condition is true. The first one says while you can assign $row from mysql_fetch_assoc it can continue. When an array reaches its end you will not be able to assign it, making the while loop end. Mysql_fetch_assoc returns an array, so you can apply foreach to $row.

The syntax of foreach is
foreach($arr as $key => $value)

So the following code represents the values.
PHP Code:
$arr["key1"]  = 7//$key=key1 $value = 7
$arr["key2"] = 15//$key=key2 $value = 15
$arr["ABC"] = 23//$key=ABC $value = 23 
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Dave (08-31-2009)
Old 08-31-2009, 03:01 AM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thanks, VI...

That helps...I'm still trying to get the full hang of this concept.

while($row = mysql_fetch_assoc($result))
{...

...means

1. The variable $row will be assigned the value(s) of the array of the first row of the results of the db query (assuming the query was successful).

2. Do everything within the WHILE loop. Drop the pointer to the second row in the db query results.

3. If there is another row in the db query results, then $row will take the second row's array value and repeat the WHILE loop.

4. And on and on, until the pointer is passed the last row of the db query resultset, at which point $row will not contain a value, and the WHILE loop exits.

Does that sound about right for the WHILE loop statement?

Thanks again,

Dave
Dave is offline  
Reply With Quote
Old 08-31-2009, 08:17 AM   #4 (permalink)
The Wanderer
 
Join Date: May 2009
Posts: 6
Thanks: 0
RishikeshJha is on a distinguished road
Default

Quote:
Originally Posted by Dave View Post
Hi,

The WHILE and FOREACH loops below are elementary, yet how they work keeps evading my mental grasp. Here is a UDF:

PHP Code:
$c_pettype "horse" ;
 
 function 
getpets($c_pettype)
 {
 
  
$q "SELECT *
       FROM t_petdata
         WHERE pettype = '
$c_pettype'" ;
          
 
$result mysql_query($q) or die ('bad query') ;

 
$j ;

 [
color="Red"]while($row mysql_fetch_assoc($result))[/color]
 { 
     [
color="Red"]foreach($row as $key => $value)[/color]
     {
         
$array[$j][$key] = $value ;
     }
                
     return 
$array ;
 }
 

So, I get the arrays, and the general concept of looping, etc., but the concepts of "WHILE($row = ...)" and "FOREACH($row as...) are not clear to me. I mean, where does "$row" come from?

Is there a way to help me understand this on a simple level?

Thanks!

Dave
$row contains the value that is assigned mysql_fetch_assoc($result).

Last edited by codefreek : 09-01-2009 at 05:33 PM.
RishikeshJha is offline  
Reply With Quote
The Following User Says Thank You to RishikeshJha For This Useful Post:
Dave (09-02-2009)
Old 08-31-2009, 01:55 PM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Dave View Post
Thanks, VI...

That helps...I'm still trying to get the full hang of this concept.

while($row = mysql_fetch_assoc($result))
{...

...means

1. The variable $row will be assigned the value(s) of the array of the first row of the results of the db query (assuming the query was successful).

2. Do everything within the WHILE loop. Drop the pointer to the second row in the db query results.

3. If there is another row in the db query results, then $row will take the second row's array value and repeat the WHILE loop.

4. And on and on, until the pointer is passed the last row of the db query resultset, at which point $row will not contain a value, and the WHILE loop exits.

Does that sound about right for the WHILE loop statement?

Thanks again,

Dave
1 and 2. Mysql_fetch_assoc returns a single row returned from the query as an array. It can keep track of itself and keep submitting the next row every time its called.

3. Yes, but not in your script since it returns before the loop can repeat.

4. Almost, I believe that the results are passed by value. But other that that, you are correct.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Dave (09-02-2009)
Old 09-01-2009, 05:31 PM   #6 (permalink)
Super Moderator
Inquisitive 
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 791
Thanks: 241
codefreek is on a distinguished road
Default

ModNote: Please read this, and follow the right use of code tags,
Thank you,
Prettifying Pasted Code on TalkPHP
-CF
codefreek is offline  
Reply With Quote
The Following User Says Thank You to codefreek For This Useful Post:
Dave (09-02-2009)
Old 09-02-2009, 02:31 AM   #7 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thanks, VI and RishikeshJha --

After still spending a lot of time on this, I think I'm finally beginning to "get it" with these concepts.

It is always the same: frustrating at first, then later you look back and wonder what the fuss was all about...

I'm sure I'll have more questions a little later.

Dave
Dave is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested while loop needed for this? gtifllm General 1 08-17-2009 07:50 PM
Horizontal nested loop problem Dracula General 3 06-05-2008 03:12 PM
Another quetion - Loop inside a loop Jmz General 1 05-30-2008 09:07 PM
Need help on nested loop! pabin Absolute Beginners 4 05-12-2008 10:27 AM
Best Way To Loop In A Table? StevenF Absolute Beginners 16 03-14-2008 04:26 PM


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design