<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
  <channel>
    <title>'foreach in php' Thread RSS Feed</title>
    <link>http://www.programmersheaven.com/</link>
    <description>Contains the latest posts from the thread 'foreach in php' posted on the 'PHP' forum at Programmer's Heaven.</description>
    <language>en</language>
    <copyright>Copyright 2013 Programmers Heaven</copyright>
    <pubDate>Wed, 19 Jun 2013 00:03:07 -0700</pubDate>
    <lastBuildDate>Wed, 19 Jun 2013 00:03:07 -0700</lastBuildDate>
    <generator>Argotic Syndication Framework 2007.3.0.1, http://www.codeplex.com/Argotic</generator>
    <docs>http://www.rssboard.org/rss-specification</docs>
    <ttl>360</ttl>
    <image>
      <url>http://www.programmersheaven.com/images/ph.gif</url>
      <title>Programmers Heaven</title>
      <link>http://www.programmersheaven.com/</link>
      <width>88</width>
      <height>31</height>
    </image>
    <item>
      <title>foreach in php</title>
      <link>http://www.programmersheaven.com/mb/phpstuff/267793/267793/foreach-in-php/</link>
      <description>i want to use "foreach " loop statement in my php program.instead of taking array i want to use the resultant query(taken from the database) is this possible to do so.&lt;br /&gt;
like...&lt;br /&gt;
&lt;br /&gt;
foreach($result as $key=&amp;gt;$value){&lt;br /&gt;
//   statement to print the records.&lt;br /&gt;
&lt;br /&gt;
I am confused....i think when we fetch some records from database and stores it in any variable . it's (the variable is)an array in itself. I may be wrong.If i am ...then please correct me.&lt;br /&gt;
&lt;br /&gt;
Thanks in advance.&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/phpstuff/267793/267793/foreach-in-php/</guid>
      <pubDate>Sat, 24 Jul 2004 17:33:16 -0700</pubDate>
      <category>PHP</category>
    </item>
    <item>
      <title>Re: foreach in php</title>
      <link>http://www.programmersheaven.com/mb/phpstuff/267793/267933/re-foreach-in-php/#267933</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by skylar at  2004-7-26 8:53:8&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
: i want to use "foreach " loop statement in my php program.instead of taking array i want to use the resultant query(taken from the database) is this possible to do so.&lt;br /&gt;
: like...&lt;br /&gt;
: &lt;br /&gt;
: foreach($result as $key=&amp;gt;$value){&lt;br /&gt;
: //   statement to print the records.&lt;br /&gt;
: &lt;br /&gt;
: I am confused....i think when we fetch some records from database and stores it in any variable . it's (the variable is)an array in itself. I may be wrong.If i am ...then please correct me.&lt;br /&gt;
: &lt;br /&gt;
: Thanks in advance.&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
This is the way to retreive information from a database:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
$query = mysql_query("SELECT * FROM &amp;lt;mytablename&amp;gt;");&lt;br /&gt;
&lt;br /&gt;
while($sqldata = mysql_fetch_row($query)){&lt;br /&gt;
.. do stuff&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
mysql_fetch_row gives you a Multi-Dimensional array consisting of the rows then the columns. If you want to grab information from this multi-dimensional array out of order then you will need to create your own multi-dimensional array.&lt;br /&gt;
&lt;br /&gt;
$mydata = array();&lt;br /&gt;
for($rownum=0; $sqldata = mysql_fetch_row($query); $rownum++){&lt;br /&gt;
 $mydata[$rownum] = $sqldata;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
So then, if you want to grab the 2nd column of the 40th row you can do this:&lt;br /&gt;
&lt;br /&gt;
echo $mydata[1][39];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
But most of the time you don't want to store the data in an array. You want to go through each and every row.. analize the row then echo data out, whatever you want to do.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Reference: &lt;a href="http://us2.php.net/manual/en/function.mysql-fetch-row.php"&gt;http://us2.php.net/manual/en/function.mysql-fetch-row.php&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/phpstuff/267793/267933/re-foreach-in-php/#267933</guid>
      <pubDate>Mon, 26 Jul 2004 08:28:04 -0700</pubDate>
      <category>PHP</category>
    </item>
    <item>
      <title>Re: foreach in php</title>
      <link>http://www.programmersheaven.com/mb/phpstuff/267793/267958/re-foreach-in-php/#267958</link>
      <description>: &lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by skylar at  2004-7-26 8:53:8&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
: : i want to use "foreach " loop statement in my php program.instead of taking array i want to use the resultant query(taken from the database) is this possible to do so.&lt;br /&gt;
: : like...&lt;br /&gt;
: : &lt;br /&gt;
: : foreach($result as $key=&amp;gt;$value){&lt;br /&gt;
: : //   statement to print the records.&lt;br /&gt;
: : &lt;br /&gt;
: : I am confused....i think when we fetch some records from database and stores it in any variable . it's (the variable is)an array in itself. I may be wrong.If i am ...then please correct me.&lt;br /&gt;
: : &lt;br /&gt;
: : Thanks in advance.&lt;br /&gt;
: : &lt;br /&gt;
: : &lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: This is the way to retreive information from a database:&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: $query = mysql_query("SELECT * FROM &amp;lt;mytablename&amp;gt;");&lt;br /&gt;
: &lt;br /&gt;
: while($sqldata = mysql_fetch_row($query)){&lt;br /&gt;
: .. do stuff&lt;br /&gt;
: }&lt;br /&gt;
: &lt;br /&gt;
: mysql_fetch_row gives you a Multi-Dimensional array consisting of the rows then the columns. If you want to grab information from this multi-dimensional array out of order then you will need to create your own multi-dimensional array.&lt;br /&gt;
: &lt;br /&gt;
: $mydata = array();&lt;br /&gt;
: for($rownum=0; $sqldata = mysql_fetch_row($query); $rownum++){&lt;br /&gt;
:  $mydata[$rownum] = $sqldata;&lt;br /&gt;
: }&lt;br /&gt;
: &lt;br /&gt;
: So then, if you want to grab the 2nd column of the 40th row you can do this:&lt;br /&gt;
: &lt;br /&gt;
: echo $mydata[1][39];&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: But most of the time you don't want to store the data in an array. You want to go through each and every row.. analize the row then echo data out, whatever you want to do.&lt;br /&gt;
: &lt;br /&gt;
: &lt;br /&gt;
: Reference: &lt;a href="http://us2.php.net/manual/en/function.mysql-fetch-row.php"&gt;http://us2.php.net/manual/en/function.mysql-fetch-row.php&lt;/a&gt;&lt;br /&gt;
: &lt;br /&gt;
Hi skylar, thanks for your response.&lt;br /&gt;
Yeah you are right i can access the data by mysql_fetch_row() statement.Butin this case i have to use the field names of the tabel to display or perform some operation over it.I don't want to do this.&lt;br /&gt;
I read about foreach that...its the simplest way of fetching data and is very useful, but it works only with array.If i use this looping construct then it's not necessary to handle every field present in that array individually .It takes "key" and "value" argument and fetches the field and its corresponding value.&lt;br /&gt;
I wanted to knowthat ... does "foreach" handles just a double dimensional array or multiple dimensional array? and if it handles multidimensionla aray then can we use it to output the fetched fields (either we use mysql_fetch_row() or something else)?&lt;br /&gt;
As ithink when we do query like &lt;br /&gt;
$result=mysql_query(select * from employee);&lt;br /&gt;
whatever the result rtrieved, that stored in a multidimensional array $result (am i correct)? and if foreach can handle multidimensional arraythen it can  handle $result also.&lt;br /&gt;
I have not somuch idea about it .I am a newbie so please correct me if i am wrong.&lt;br /&gt;
Thanks alot.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/phpstuff/267793/267958/re-foreach-in-php/#267958</guid>
      <pubDate>Mon, 26 Jul 2004 10:22:31 -0700</pubDate>
      <category>PHP</category>
    </item>
    <item>
      <title>Re: foreach in php</title>
      <link>http://www.programmersheaven.com/mb/phpstuff/267793/269637/re-foreach-in-php/#269637</link>
      <description>&lt;strong&gt;&lt;span style="color: Red;"&gt;This message was edited by skylar at  2004-8-9 9:18:23&lt;/span&gt;&lt;/strong&gt;&lt;hr /&gt;&lt;br /&gt;
ummmm no..&lt;br /&gt;
&lt;br /&gt;
a foreach loop can ONLY HANDLE ONE ARRAY DIMENSION :)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
but listen.. if you have a multi-dimensional array.. the first array HOLDS another array.. so when you do a foreach of the first array, the foreach will spit out the 2nd array as the value, eg:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;

$firstarr = array(array(1, 2, 3), array(4, 5, 6));

foreach($firstarr as $secondarr){
 // $secondarr = array(1, 2, 3) .. then array(4, 5, 6)
 foreach($secondarr as $arrvalue){
  echo $arrvalue;
 }
}

output:
123456
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Oh yah.. mysql_query returns a RECORD SET.. record sets can only be accessed via mysql_fetch_row.. fetch_array.. fetch_assoc.. or fetch_object&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You need to use these functions to access the data from a record set created by mysql_query.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Use this as an example of how it should be done:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="sourcecode"&gt;

&amp;lt;exampletable&amp;gt; -&amp;gt; id, field1, field2, field3

$q = mysql_query("SELECT * FROM &amp;lt;exampletable&amp;gt;");
while($data = mysql_fetch_row($q)){
 echo "id = $data[0], field1 = $data[1], field2 = $data[2], field3 = $data[3]";
}

&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
hope that helps...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description>
      <guid isPermaLink="true">http://www.programmersheaven.com/mb/phpstuff/267793/269637/re-foreach-in-php/#269637</guid>
      <pubDate>Mon, 09 Aug 2004 09:09:52 -0700</pubDate>
      <category>PHP</category>
    </item>
  </channel>
</rss>