views:

520

answers:

4

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 writing 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
       }
A: 

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>';
erisco
That's really not how you do it in CodeIgniter.
Phil Sturgeon
Yet there is nothing wrong with this answer as it could be easily applied to Code Igniter. There is nothing Code Igniter does to make this method wrong. In fact, Code Igniter encourages something worse and I would not recommend it. Put your down votes elsewhere.
erisco
A: 

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.

Daniel
+2  A: 

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.

Donny Kurnia
Dont forget that $result is an array of rows, so you need to add in $result = $this->db->get('table')->row()->whateverthefieldnameis;
Phil Sturgeon
A: 

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 :).

kevinpajak