Re: Select value for driopdown box
| From: | Jim Lucas | Date: | Wed, 22 Jan 2003 18:50:26 +0000 |
| Subject: | Re: Select value for driopdown box | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-132659@lists.php.net to get a copy of this message | ||
function DrawOptions($options, $active='')
{
$str = '';
foreach($options AS $key=>$value)
{
$sel = ($active==$key?" SELECTED":"");
$str .= "<option
value='".$key."'".$sel.">".$value."</option>\n";
}
return($str);
}
Try this
<select name='something'>
<?=DrawOptions($array, $active)?>
</select>
array needs to be a key-value pair that has the value that you are looking
for as the key and if it matches it will make that key - value assoc the
selected one.
that should work for you
----- Original Message -----
From: "Ben C." <benc@cox.net>
To: <php-general@lists.php.net>
Sent: Wednesday, January 22, 2003 10:14 AM
Subject: [PHP] Select value for driopdown box
> I am using the query below to edit a record. I want to edit the field
which has a list of states in a dropdown box. I want to have the state
that is in the selected field shown as the selected state. How would I do
this?
>
>
> Query
> -------------------------
> <?
> $sql = "SELECT *
> FROM $table_name
> WHERE buyerid = \"$buyerid\"
> ";
>
> $result = @mysql_query($sql,$connection) or die(mysql_error());
>
>
> while ($row = mysql_fetch_array($result)) {
> $buyerid = $row['buyerid'];
> $state = $row['state'];
>
> $option_block .= "<option value=\"$state\">$state</option>";
> }
>
> $display_block = "
>
> <select name=\"state\" class=Pgtext>
> $option_block
> </select>
> ?>
>
> <tr>
> <td width="258" class="Pgtext" height="21">State</td>
> <td width="259" height="21"><? echo $display_block;
> ?></td>
> </tr>
>
> --------------------------------
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>