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
Solved

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

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

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 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

861 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