Welcome Guest, Not a member yet? Register   Sign In
Working Drop Down List Code Review Help
#1

[eluser]TerryT[/eluser]
I am new to CI and looking for some advice as to best practices from the experts. I want my users to be able to either select all projects or any individual project. The code below works. Can you let me know what needs to be done to it so it represents best practices and is bulletproof? Also what would be typically included for error trapping and messages? Thanks in advance for any advice.

Terry

Controller code:

Code:
$data = array();
$data['project_menu'] = $this->Items_model->getProjects();

Model Code:

Code:
function getProjects()
    {
    $query = $this->db->get('projects');
    return $query;
    }

View code:

Code:
<div class='dropdown'>
     <label>Projects</label>
    <p><select>
    &lt;?php echo '<option value="all">All Projects</option>'; ?&gt;  
        &lt;?php foreach($project_menu->result() as $row)
    {
        echo '<option value="' . $row->id . '">'. $row->proj_name . '</option>';    
    }
    ?&gt;
    </select></p>  
</div>
#2

[eluser]CroNiX[/eluser]
I would use the form helper myself...

Controller:
Code:
$data = array();
$data['project_menu'] = $this->Items_model->get_projects();  //shouldn't use camelCase for function names
$this->load->helper('form');  //load the form helper
Model:
Code:
function get_projects()
{
    $data = array();
    $query = $this->db->get('projects')->result();
    //form helper for dropdown requires array of id=>value
    foreach($query as $q)
    {
        $data[$q->id] = $q->proj_name;
    }
    return $data;
}
View:
Code:
<div class='dropdown'>
    <label>Projects</label>
    <p>&lt;?php echo form_dropdown('my_select', $project_menu); ?&gt;</p>
</div>
#3

[eluser]TerryT[/eluser]
Thank you for the help. I see this makes the view cleaner. Would you typically put any code in to check and make sure there is something returned in the query? If a user just gets started with the program, he will not have entered a project yet. The dropdown will just be empty, but I don't see that as a problem. Thanks again.

Terry
#4

[eluser]TerryT[/eluser]
The code suggested worked well. I wanted to add an "All Projects" selection at the top of the drop down. That took an hour to figure out on my own. Posting the code here for any newbies who may encounter the same issue:

Code:
//form helper for dropdown requires array of id=>value:
$query = $this->db->get()->result();

//Add the "All Projects" option to the beginning of the array:
$arraytemp = new stdClass;
$arraytemp->users_id = "0";
$arraytemp->projects_id = "0";
$arraytemp->proj_name = "All Projects";
array_unshift($query, $arraytemp);

Terry
#5

[eluser]CroNiX[/eluser]
I generally do this:
Code:
function get_projects()
{
    $data = array();
    $query = $this->db->get('projects')->result();
    //form helper for dropdown requires array of id=>value
    $data[0] = 'All Projects';  //add default first option
    foreach($query as $q)
    {
        $data[$q->id] = $q->proj_name;
    }
    return $data;
}
Generally I add a default first option with an ID of 0 (like you did), since most columns with an id are autoincremented and start at 1, so you would never have a choice with 0.

Then in your form for the dropdown, you can:
Code:
<p>&lt;?php echo form_dropdown('my_select', $project_menu, 0); ?&gt;</p>
add the default of 0 as the selected value in the dropdown.
[/code]

But if you want to make this reusable (since all form dropdowns require this format), make a helper:
Code:
function get_form_dropdown($table, $id_field, $value_field, $first_option = '')
{
    $CI =& get_instance();

    $values = $CI->db
        ->select($id_field)
        ->select($value_field)
        ->get($table)
        ->result();
    $data = array();
    //create the first option if one was supplied
    if( ! empty($first_option))
    {
        $data[0] = $first_option;
    }
    foreach($values as $value)
    {
        $data[$value->$id_field] = $value->$value_field;
    }
    return $data;
}
Then just load your helper in your controller and:
Code:
$data['project_menu'] = get_form_dropdown('projects', 'id', 'proj_name', 'All Projects');

And in a different project which uses a dropdown:
Code:
$data['project_menu'] = get_form_dropdown('users', 'userid', 'username', 'Select a User');
...or whatever
#6

[eluser]TerryT[/eluser]
Appreciate the insight. Although I was proud of figuring it out, your code is far cleaner.

Terry




Theme © iAndrew 2016 - Forum software by © MyBB