开发者

Dropdown select based on database entries

So, I would like a select to auto-populate the dropdown's "Yes" selection, or alternatively, the "No". The returned value from a database query will be used to evaluate which dropdown will be selected.

I have thought since it's a true/false auto-population to just write 2 conditionals, but I thought there would be a better (read: less messy code) way to write code that would auto-populate the right selection from the drop down, based on the result from the database.

The base of the code I'm trying to write would be checking the variable against all of the selects, then appending a string that would select that drop down for the user in the view.

My question would be is there a simpler way to do this, instead of writi开发者_如何学编程ng conditionals for each possible drop down value?

Code by request, written in CodeIgniter PHP:

 $this->db->select('row'); 
 $result = $this->db->get('table');

 // This just selects and returns the values. This code does work, I'm just looking for a better way to do this task that someone might know, because I'm going to have drop downs with hundreds of possibilities, of which, I want the predefined one to be selected.

 // Assume $result->row = "Yes"

 if ( $result = "Yes" ) {  
    #Code for echo'ing radio button with "Yes" selected
       }

 else {  
    #Code for echo'ing radio button with "No" selected
       }


Have you check Form Helper in CI? You can use form_radio and supply the value that you want to select in the third parameter:

form_radio('field_name', 'Yes', $result == 'Yes');
form_radio('field_name', 'No', $result == 'No');

First parameter is field name, Second parameter is value for the field, and third parameter is boolean (TRUE: radio selected, FALSE: not selected). Since this is a radio, the field name should same.


Something like this should work. I make many assumptions as to what your requirements and data are, though.

// The selected value
$selectedValue = 'foo';

// Database results, assuming a structure like so
$results = array(
  array(
    'id' => 1,
    'value' => 'foo'
  ),
  array(
    'id' => 2,
    'value' => 'bar'
  )
);

echo '<select name="selectField">';

foreach ($results as $result) {
  echo '<option value="'.$result['id'].'"';

  // Here we see if this result is the selected value.
  // If so, we spit out the HTML so the user's browser renders it as such.
  if ($result['value'] == $selectedValue) {
    echo ' selected="selected"';
  }

  echo '>'.$result['value'].'</option>';
}

echo '</select>';


Your question seems a bit confusing. You mention radio buttons in your code, but your question asks about dropdowns.

If you need to display a set of radio buttons or dropdown options based on a given value, you can define your selections as an array:

$data = array(
    'yes' => array('option1', 'option2', 'option2', 'etc'),
    'no' => array('option1', 'option2', 'option2', 'etc'),
    'somethingelse' => array('option1', 'option2', 'option2', 'etc')
);

And use it like this:

$query = $this->db->select('row')->get();

if ($query->num_rows() > 0) {
    $result = $query->row();

    if (array_key_exists(strtolower($result->row), $data)) {
        foreach ($data[$result->row] as $row) {
            // Use $row as you need
        }
    }
}

You could even wrap this into a function for re-usability.


Thanks Erisco, you just helped me out with something by your post.

My version though is more basic and pared down, without referencing any key => value pairs. It just sets up an array containing all the options from a previous dropdown menu, cycles through it [as it populates the dropdown menu as well], and sets the option to 'selected' if it matches the one that the user had previous selected and stored into the database:

<?php           
$naybpopulate = array('Bernal Heights', 'Castro', 'Chinatown', 'Cole Valley', 'Fishermans Wharf', 'Forest Hill', 'Haight-Ashbury', 'Hayes Valley', 'Inner Richmond', 'Inner Sunset', 'Japantown', 'Marina', 'Mission', 'Mission Bay', 'Nob Hill', 'Noe Valley', 'North Beach', 'Outer Richmond', 'Outer Sunset', 'Pacific Heights', 'Potrero Hill', 'Presidio', 'Russian Hill', 'SoMa', 'South Beach', 'Telegraph Hill', 'Tenderloin', 'Union Square', 'Western Addition', 'West Portal');

        echo '<select name="neighborhood" id="neighborhood">';

foreach ($naybpopulate as $nayb) {
echo '<option value="'.$nayb.'"';

// Here we see if this result is the selected value.
// If so, we spit out the HTML so the user's browser renders it as such.
if ($nayb == $neighborhood) {
echo ' selected="selected"';
}

echo '>'.$nayb.'</option>';
}

echo '</select>';
?>

Stack Overflow is the BEST. You guys rock :).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜