Question

I've been searching on the site (and through Google) but I guess I might be misphrasing my problem.

Here's my situation. I have this dynamically populated dropdown (which is working) that can, in some occasions, receive a value to display a specific option.

    echo "<td><select id='bids_id' class='ui-widget-content ui-corner-all normalselect' style='width: 150px'>";
    $sql = "SELECT
                `bids`.`id`,
                `bids`.`name`
            FROM
                `bids`
                Inner Join `bids_primes` ON `bids`.`id` = `bids_primes`.`bid_id`
            WHERE
                `bids_primes`.`tid` = '". $db->real_escape_string($_SESSION['Current_TID']) ."'
            ";
    $querybids = $db->query($sql);
while($row = $querybids->fetch_object()) {
    echo "<option value='{$row->id}' ". ($row->id == $timeinfo->relatedbid ? "selected=selected" : "") .">{$row->name}</option>";
}

Now the only thing I'm trying to do, with no luck, is to add a "hardcoded" choice with the values of '0' and a text of 'Other'. This choice should also be bound by the same mechanic (where it can be specified to be pre-selected).

[UPDATE] So to help out with the context of my situation, here is the code that preceeds this part. This is a PHP file that is called from within another PHP file (same principal as a div). The PHP in question is an entry form that is used both for new data entry and modification of existing data (and this interacts with the database).

The script knows if it's adding or modifying based on the "entry_id" it receives as paramater, a bit like a function would.

    if(isset($_REQUEST['entry']) && ($_REQUEST['entry'] > 0)) {
    $sql = "SELECT
                `cap_plan`.`entryid`,
                `cap_plan`.`startdate`,
                `bids`.`name`,
                `cap_plan`.`duration`,
                `cap_plan`.`description`,
                `cap_plan`.`relatedbid`
            FROM
                `cap_plan`
            LEFT JOIN
                `bids` ON `cap_plan`.`relatedbid` = `bids`.`id`
            WHERE
                `cap_plan`.`entryid` = '". $db->real_escape_string($_REQUEST['entry']) ."'
            ";
    $timeinfo = $db->query($sql)->fetch_object();
} 
    else {
        $timeinfo = new StdClass();
        $timeinfo->entryid = 0;
        $timeinfo->employee = $_SESSION['name'];
        $timeinfo->startdate = date("Y-m-d");
        $timeinfo->duration = "";
        $timeinfo->description = "";
        $timeinfo->relatedbid = "0";
    }

Here's a screenshot to help illustrate it all: http://i.imgur.com/EYcXfO9.png

Was it helpful?

Solution

If I understand your question correctly, all you wish to do is to insert a hard-coded option at the top of the list, BEFORE the dynamically generated choices.

Just do this:

echo "<td><select id='bids_id' class='ui-widget-content ui-corner-all normalselect' style='width: 150px'>";
echo "<option value = '0'>Choose One</option>";
$sql = "SELECT etc etc etc
etc.

Note that you do not need to specify the selected attribute on the first, default, option tag (the "Choose One" option), because it will automatically be the selected option IF nothing else is selected. I believe that is what you would want.


What I would do, though, is to assign all values to a variable and then echo out the variable, so that everything appears all-at-once. Like this:

$opt = "<td><select id='bids_id' class='ui-widget-content ui-corner-all normalselect' style='width: 150px'>";
$opt .= "<option value = '0'>Choose One</option>";

$sql = "SELECT etc etc etc";
$querybids = $db->query($sql);

while($row = $querybids->fetch_object()) {
    $opt .= "<option value='{$row->id}' ". ($row->id == $timeinfo->relatedbid ? "selected=selected" : "") .">{$row->name}</option>";
}

echo $opt;

OTHER TIPS

I'm going to put this here:

  1. Just because the value is "0" doesn't mean it'll be the first one in the list; it'll be first because it's first in the HTML.

  2. In a <select> form element with all its options, there should only be one option that's selected. If that's true, and you're testing for all options OTHER than "other", then none of your <options> will get the selected attribute if the option is, in fact, "other". So, your first option ("other") will be selected because it's first.

Does that makes sense?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top