Solved

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

Posted on 2013-07-02
4
268 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
4 Comments
 
LVL 108

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 34

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 42

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now