Re: Select value for driopdown box
| From: | Greg Beaver | Date: | Sun, 26 Jan 2003 02:27:56 +0000 |
| Subject: | Re: Select value for driopdown box | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-133149@lists.php.net to get a copy of this message | ||
This line:
$selected = $state == $sel_state ? "selected=\"selected\"" : "";
should probably be:
$selected = (($state == $sel_state) ? "selected=\"selected\"" : "");
I haven't tested this correction, but have had to use the parentheses in my
code many times to fix problems with using ? :
Greg
--
phpDocumentor
http://www.phpdoc.org
"Ben C." <benc@cox.net> wrote in message
news:AIEKJHFHCPDJIKLELPKEKECJEIAA.benc@cox.net...
> I tried to use the below but it does not work. Please help!
>
> -----Original Message-----
> From: Michael Sweeney [mailto:Michael.Sweeney@verisity.com]
> Sent: Wednesday, January 22, 2003 10:29 AM
> To: Ben C.
> Cc: php-general@lists.php.net
> Subject: Re: [PHP] Select value for driopdown box
>
>
> Assuming that you know which state is selected before you build the
> select list:
>
> $sel_state = $_REQUEST['state'];
>
> ...
>
> while($row = mysql_fetch_array($result)) {
> $buyerid = $row['buyerid'];
> $state = $row['state'];
> $selected = $state == $sel_state ? "selected=\"selected\"" :
> "";
>
> $option_block .= "<option value=\"$state\" $selected>".
> "$state</option>\n";
> }
>
> That will put the string "selected=\"selected\"" (xhtml compliant) into
> the option statement for the state that was selected.
>
> ..michael..
>
> On Wed, 2003-01-22 at 10:14, Ben C. wrote:
> > 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>
> >
> > --------------------------------
> --
> Michael Sweeney <sweeney@verisity.com>
> Verisity Design, Inc
>