On Tue, 2003-06-03 at 02:12, Bix wrote:
> I'm fine with using while loops, use them for most of my listing work, but I
> wanted to try using a foreach loop instead but it seemed not to work as
> expected. For ym table generation, I need the $key to do some maths on in
> order to get my table looking right. All the guts are done, but for some
> reason, when usng a foreach loop, foreach (mysql_fetch_array($result) as
> $key => $value) $value is not an array of the fields. Whereas with a while
> loop, it works fine. Is this a problem with foreach?
> 
> If it is, i'll stick to while, but with a counter to generate a key.

Nope...it's working perfectly. mysql_fetch_array() returns an array.
foreach() operates on an array. What you have:

  foreach (mysql_fetch_array($result) as $key => $article) {
     //. . .
  }

...is the same as saying this:

  $arr = mysql_fetch_array($result);
  foreach ($arr as $key => $article) {
     //. . .
  }

So it is iterating over the correct array (the returned row array) 
instead of the array over which you want it to be iterating (the
array of rows).

Using the counter is probably your best bet. mysql_fetch_array() doesn't
return any information on which row it's currently returning.



Hope this clears it up a bit,

Torben


> "Justin French" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > This is correct:
> >
> > while($myrow = mysql_fetch_array($result))
> >     {
> >     // ...
> >     }
> >
> > The iteration of the while loop represents one returned row from the mysql
> > result, with $myrow being an array of values returned.
> >
> > Rather than a SELECT * query, let's look at a something where we know the
> > column names:
> >
> > <?
> > $sql = "
> >     SELECT first, surname, age
> >     FROM employee
> >     WHERE age >= 18
> >     LIMIT 50
> >     ";
> > $result = mysql_query($sql);
> > while($myrow = mysql_fetch_array($result))
> >     {
> >     echo "Name: {$myrow['first']} {$myrow['surname']}.  Age:
> > {$myrow['age']}<br />";
> >     }
> > ?>
> >
> >
> > Now, given the above code, what else do you need to do?
> >
> >
> > Justin
> >
> >
> >
> >
> > on 03/06/03 8:25 AM, Bix ([EMAIL PROTECTED]) wrote:
> >
> > > Hi all,
> > >
> > > I am trying to build up a table using values from a db as follows...
> > >
> > > mysql_select_db($db, $sql_connection);
> > > $result = mysql_query("SELECT * FROM $table WHERE $query LIMIT
> > > $limit",$sql_connection);
> > > $output = "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"
> > > width=\"370\">\n";
> > > foreach(mysql_fetch_array($result) as $key => $article){
> > > //stuff
> > > }
> > >
> > > now if I use a while loop ie:
> > >
> > > while ($array = mysql_fetch_array($result)){
> > > //stuff
> > > }
> > >
> > > all the matched rows are processed, however with the foreach model,
> $article
> > > isnt an array, it is the first row all merged together.
> > >
> > > Any ideas?
> > >
> > > ;o)
> > >
> > >
> >
> 


-- 
 Torben Wilson <[EMAIL PROTECTED]>                        +1.604.709.0506
 http://www.thebuttlesschaps.com          http://www.inflatableeye.com
 http://www.hybrid17.com                  http://www.themainonmain.com
 -----==== Boycott Starbucks!  http://www.haidabuckscafe.com ====-----




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to