NYCPHP Meetup

NYPHP.org

[nycphp-talk] learning about foreach continued

Hans Zaunere zaunere at yahoo.com
Wed May 21 16:05:16 EDT 2003


--- "Joshua S. Freeman" <jfreeman at amnh.org> wrote:
> Thanks for everyone who's chimed in so far... I appreciate the various code
> snippets but I'm trying to see if I can do everything I need here using
> 'foreach()'...
> 
> so.. 
> 
> since this:
> 
> $query= mysql_query("select * FROM taxa_order");
> 
>   $result = mysql_fetch_assoc($query);
>         print ("<select name=\\"order\\">");
>         foreach ($result as $key=>$val)
>         print "<option value=\\"$key\\">$val</option>";
>         print ("</select>");
> 
> yields this:
> 
> <select name="order"><option value="order_id">1</option><option
> value="order_name">Hymenoptera</option></select>

You'll need to do this:

<?php

$result = mysql_query("SELECT * FROM taxa_order");

$rowcnt = mysql_num_rows($result);

?>

<select name="taxa_order">
  <?php for( $i = 0; $i < $rowcnt; ++$i ): ?>
   <option
value="<?=mysql_result($result,$i,0)?>"><?=mysql_result($result,$i,1)?></option>
  <?php endfor; ?>
</select>



Oh wait, you wanted to use foreach()  --  Don't :)

Unless you want to iterate over the result using for()/while(), then you
could iterate over the resulting array with a foreach - but that's just
silly.

What you'd like to see is a "mysql_fetch_resultset_into_array()" like Oracle
has.  Then you'd have an array that you could iterate over.

I think this would make for a good pCom  :)


H




More information about the talk mailing list