NYCPHP Meetup

NYPHP.org

[nycphp-talk] learning about foreach continued

max goldberg max at idsociety.com
Wed May 21 16:13:59 EDT 2003


You probably want something like this:

$result = mysql_fetch_assoc($query);

echo '<select name="order">';
foreach ($result as $row) {

$option_value = $row['order_value'];
$option_label = $row['order_label'];

// replace order_value with the name of the column with the value in it
// replace order_label with the name of the column with the label in it

echo "<option value=\\"$option_value\\">$option_label</option>";
echo "</select>";

If you would like me to explain further how this works just ask.

-Max


Joshua S. Freeman wrote:

> Hi Max,
>
> I can see you're trying to help me and I appreciate it.
>
> So...  I can't seem to do anything with either mysql_fetch_both or
> mysql_fetch_num... mysql_fetch_array brings up TOO much information...
>
> I don't have enough background yet to put the information you've given me
> here to good use.
>
> J.
>
> On 5/21/03 3:47 PM, "max goldberg" <max at idsociety.com> wrote:
>
>
>>From www.php.net/mysql
>>
>>The function mysql_fetch_array() uses a constant for the different types
>>of result arrays. The following constants are defined:
>>
>>Table 3. MySQL fetch constants
>>
>>constant description
>>MYSQL_ASSOC Columns are returned into the array having the fieldname as
>>the array index.
>>MYSQL_BOTH Columns are returned into the array having both a numerical
>>index and the fieldname as the array index.
>>MYSQL_NUM Columns are returned into the array having a numerical index
>>to the fields. This index starts with 0, the first field in the result.
>>
>>More at http://www.php.net/manual/en/function.mysql-fetch-array.php
>>
>>-max
>>
>>
>>Joshua S. Freeman 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>
>>>
>>>how do I get it to stop from creating two <option></option> rows (there's
>>>only one row in the table!) and how do I get it to stop from
including the
>>>names of the columns and just including the value for each column in
>>
>>the one
>>
>>>row?
>>>
>>>J.
>>>
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>>
>>
>>
>>
>
>
>
> --- Unsubscribe at http://nyphp.org/list/ ---
>
>




More information about the talk mailing list