Solved

How could a combobox to be started with a defined option dinamically marked?

Posted on 2013-07-02
4
277 Views
Last Modified: 2013-07-02
Hi Experts!

How could a combobox to be started with a defined option dinamically marked?
(depending on the case, the combobox options are not fixed)

Accordingly with the example bellow if I have a value ($d_cr) previously defined to start presenting, how could be the combobox "forced" to present it instead of a blank value?


                 CR (L):
                 <select name="sb_cr">
                    <!--option selected="selected"-->     
                    
                    // Just try it...
                    <option selected="<?php echo $d_cr ?>">
                    
                    
                 </option>
                    
                 <?php

    //----------Inicio Combo 1 (CR) -----------------------------------------------------------------
    $sql = mysql_query("select sb_cr, cr_info, substr(descricao,1,25) as descricao from cr where tipo=1 order by descricao");
    //----------Final Combo 1-------------------------------------------------------------------------

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['sb_cr'];
        $data = $row['descricao'];
        echo '<option value="' . $id . '">' . $data . '</option>';
    }

?>
            </select>

Open in new window


Thanks in advance!
0
Comment
Question by:Eduardo Fuerte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39293045
The select / option tags looks like this:

<select name="my_data">
<option value="" selected>Choose One</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>

So maybe something like this?

<select name="sb_cr">
<option value="<?php echo $d_cr; ?>" selected><?php echo $d_cr; ?></option>
<!-- more as needed -->
</select>

W3Schools is pretty good for an HTML reference.
http://www.w3schools.com/tags/tag_select.asp
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 39293459
<?php

    //----------Inicio Combo 1 (CR) -----------------------------------------------------------------
    $sql = mysql_query("select sb_cr, cr_info, substr(descricao,1,25) as descricao from cr where tipo=1 order by descricao");
    //----------Final Combo 1-------------------------------------------------------------------------

    while ($row = mysql_fetch_array($sql)) {
        $id = $row['sb_cr'];
        $data = $row['descricao'];
        $selected = (($id == $d_cr) ? " selected" : "");
        echo '<option value="' . $id . '"' . $selected . '>' . $data . '</option>';
    }
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39293536
Assuming you want a SELECT OPTION to be pre-selected based on the value of a variable, something like this will achieve what you need:

<select>
<?php
while ($row = mysql_fetch_array($sql)) {
    printf("<option value='%s' %s>%s</option>", $row['sb_cr'], ($row['sb_cr'] == $d_cr) ? "selected": "", $row['descricao'] );
}
?>
</select>

Open in new window

It outputs a formatted string, and includes a ternary if statement to decide whether to output 'selected'
0
 

Author Closing Comment

by:Eduardo Fuerte
ID: 39293665
Works correctly.
0

Featured Post

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question