Solved

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

Posted on 2013-07-02
4
275 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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