NYCPHP Meetup

[nycphp-talk] learning about foreach

gadhra bfeqx44002 at sneakemail.com
Wed May 21 15:14:43 EDT 2003


Hi Josh,

See my other post on your mysql problem.

I would modify your query to be this:

$sql = "select order_id, order_name from order";

then, when you get the results straightened out, you can do something 
like this:

$result = mysql_query($sql);
print '<select name="order">';
while ($query = mysql_fetch_assoc($result) ) {
    print "<option 
value=\\"$query[order_id]\\">$query[order_name]</option>\
";
}
print '</select>';

Something that might be a good thing to do is to run the query above, 
and do the following:
print_r(mysql_fetch_row($result));
print_r(mysql_fetch_assoc($result));
print_r(mysql_fetch_array($result));

This can give you a lot of insight into how PHP stores the MySQL data, 
and how you might iterate through it.

+gadhra+


Joshua S. Freeman jfreeman-at-amnh.org |NY PHP| wrote:

>Hi Hans, et al.
>
>Thanks for the response and advice Hans...
>
>I don't know if this is salient to your response but ideally, what I want
>the combination of the query and the foreach to do is to go into the 'order'
>table, grab the single record that's in there (it has two columns, an
>'order_id' column and an 'order_name' column) and then, in my form, build:
>
><select name="order">
>          <option value="1">Hymenoptera</option>
>        </select>
>
>
>
>so... the "1" in the 'value="1" is from the 'order_id' column and the
>'Hymenoptera' is from the 'order_name' column.
>
>I'm not sure if this was clear before..  I know it wasn't clear from my code
>snippet.
>
>Thanks..
>
>J.
>
>
>
>On 5/21/03 2:58 PM, "Hans Zaunere" <zaunere at yahoo.com> wrote:
>
>  
>
>>--- "Joshua S. Freeman" <jfreeman at amnh.org> wrote:
>>    
>>
>>>I've made a conceptual breakthrough (duh!) with regards to this project I'm
>>>working on to really get to a meaningful skill level with MySQL/PHP
>>>development.  But.. for some reason, I'm really stuck on this one point:
>>>
>>>I have a database called 'wasps'.  The database has 19 tables so far most
>>>of
>>>which are 'lookup' tables (is that the right term?)...
>>>
>>>The main table in the database is called 'specimens'.  Each 'specimen' has
>>>a
>>>taxonomy (order, family, genus, subgenus, species, subspecies).
>>>
>>>For each part of the taxonomy I have a separate table.
>>>
>>>Ideally, when someone loads the survey form to begin building a new record
>>>in the 'specimens' table, I want to query those taxonomy tables (order,
>>>family, genus, etc...) so that I can build
>>><select><option></option></select> menus based on the rows inside each
>>>taxonomic table...
>>>
>>>I want to do this using 'foreach'...
>>>
>>>I've tried a few different things but I can't get it to work:
>>>
>>>mysql_connect($host, $user, $password)
>>>    or die("Could not connect to database");
>>>mysql_select_db($database)
>>>    or die ("Could not select $database database");
>>>
>>>print "The current database is $database";
>>>
>>>$query= mysql_query("select * FROM order");
>>>      
>>>
>>I think you want to return to $result here, since it's used below.
>>    
>>
>>>$result = $query;
>>>        print ("<select name=\\"order\\">");
>>>        foreach ($result as $key=>$val)
>>>      
>>>
>>$result is not an array - it's a resource handle pointing to a chunk of
>>memory that contains MySQL's data.  You'd probably want something like:
>>
>>for( $rows = array(),$i = 0,$rowcnt = mysql_num_rows($result); $i < $rowcnt;
>>++$i ) {
>> $rows[] = mysql_fetch_assoc($result);
>>}
>>
>>
>>foreach( $rows as $rownum => $row ) {
>> foreach( $row as $colname => $colvalue ) {
>>    echo "<br><br>$rownum => $colname => $colvalue";
>> }
>>}
>>
>>
>>.. for example.
>>
>>
>>H
>>
>>
>>    
>>
>>>        print "<option value=\\"1\\>$val</option>";
>>>        print ("</select>");
>>>        }
>>>
>>>?>
>>>
>>>but this does not work.. i know I'm close.. and I just spent 2 hours at
>>>barnes & nobles at a table in the cafe with a bunch of PHP books (couldn't
>>>find that WROX book someone suggested earlier)  trying to make this work on
>>>just the order table because that table only has one record in it...  but I
>>>could not make it work.. if I can figure out how to make this, I'll be able
>>>to make considerable progress on this thing..
>>>
>>>Someone please help me over this hump!
>>>
>>>Thanks!
>>>
>>>J.
>>>
>>>
>>>
>>>
>>>
>>>
>>>      
>>>
>>
>>
>>
>>
>>
>>    
>>
>
>
>
>--- Unsubscribe at http://nyphp.org/list/ ---
>
>
>
>
>
>  
>

-- 
+gadhra+(/s)
*********************
XXXXXXXXXXXXXXXXX
And seize the metropolis;
It's you it's built on





More information about the talk mailing list