0

I am generating an html dropdown and I currently have the code to select the data from the database and generate the result in the view? Is this ok, or should I be placing it in the controller somehow. If so, how do I call that data from the view? This is my code:

<select id="f_treeindex">
            <?php
                $query = $this->db->query('SELECT id, tree_name FROM trees');

                foreach ($query->result() as $row)
                {
                    echo '<option value="' . $row->id . '">' . $row->tree_name . '</option>';
                };
            ?>
</select>

I want to keep as much DB data out of my views as possible.

REVISED:

MODEL:

function get_tree(){
    $query = $this->db->query('SELECT id, tree_name FROM trees');
    return $query->result_array();
}

CONTROLLER:

$data['trees'] = $this->Model_form->get_tree();

    if ($this->form_validation->run() == FALSE)
    {
        $this->load->view('view_form_group', $data);
    }

VIEW:

<?php
        foreach($trees as $tree){
            echo '<option value="' . $tree->id . '">' . $tree->tree_name . '</option>';
        }
        ?>
mheavers
  • 29,530
  • 58
  • 194
  • 315

4 Answers4

2

Your view should receive an array from the controller containing the data, then loop through the array to populate the select.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • OK - so I tried to follow this example: http://stackoverflow.com/questions/3041616/creating-foreach-loops-using-code-igniter-controller-and-view and I have one error. I've revised the code above. I'm not sure how I should be referencing the database query from the model in my controller. Any help would be appreciated – mheavers May 27 '11 at 17:37
2

Why is the query not contained in a model?

All database tables should have some form of an associated model. In this case, you might want to have a Tree_Model or something along those lines. The controller should be responsible for setting the model in its correct state (i.e. if there was a where predicate in your select, the controller would be responsible for giving that data to the model).

From there, it's more of a religious debate as to who should query the model - the controller or the view. I'd usually put the model query in the view as the model is already in the correct state (set by the controller) to keep my controller code light.

At the end of the day, neither the controller or the view should be querying the database directly.

Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
  • To whoever downvoted this, it's been edited, so you might want to take another look (I didn't thoroughly read the question prior to the first answer) – Demian Brecht May 27 '11 at 17:33
1

The query should go in the controller, and then be passed to the view to be displayed.

  • @Corey db queries should not go directly in a controller. They should be in a model or a DA layer. – martynthewolf May 27 '11 at 17:53
  • 1
    I didn't see he was writing the whole db query there, I thought he was doing the $this->Tree->find() or something, which should go in the controller, not the Model, but if he is writing customer queries (which that simple of one should not be) then it should go as a method in the model, correct. –  May 27 '11 at 17:56
  • So if I should not be writing custom queries, what should I do instead? Just trying to get an idea of best practices since I'm new to this – mheavers May 27 '11 at 18:02
  • I don't use codeignitor, but in cakePHP you would do something like this: $this->Tree->find('all',array('fields'=>array('id','tree_name'))); Most of the queries that you should want to do should be available using your models instead of direct db queries. That is one of the points of MVC is to abstract the database and turn it into an object, and negate the need to write so much sql. –  May 27 '11 at 18:48
1

Query goes in the Tree model where you have a function something like get_trees(), and you call it from the controller with something like $trees = $this->Tree->get_trees();

Then you pass it to the view and loop through it. That's the MVC way.

For your REVISED code you should also add in the Model "return $query->result_array();"

Alex
  • 170
  • 11
  • Ah - so why is trees undefined? Or rather, what do I have to do to define it. The error I get is: Message: Undefined property: Control_form::$trees. I just want to make a link from the controller to the data retrieved from the DB in the model – mheavers May 27 '11 at 17:46
  • Your model should be called Trees, and be loaded in the controller: $this->load->model('Trees'); – Alex May 27 '11 at 17:48
  • Hmmm - that fixed the error - but now I don't get my results in the dropdown. Man...sure seemed a whole lot easier to just dump the code into one compact statement in the view. Must be something I'm missing – mheavers May 27 '11 at 17:55
  • So in the model you have return $query->result(), right? I can't see any other problem with your code. – Alex May 27 '11 at 17:57
  • Yeah - I've revised the code above - no errors, but no results either. – mheavers May 27 '11 at 17:59
  • Oh I see, use $query->result() instead of $query->result_array(). It's because you use $tree->id to get the property of the row. – Alex May 27 '11 at 18:03
  • It works! Awesome! Thank you for the help everyone. I've been struggling to understand this whole approach for awhile now, and I think it's finally clicked. I'm not quite sure I understand the result_array() vs array() thing yet, but all else is good. – mheavers May 27 '11 at 18:07
  • Cool! Check out [link](http://codeigniter.com/user_guide/database/results.html) to see the difference between result and result_array – Alex May 27 '11 at 18:09
  • Cool - so seems like result_array() might be better, just in case something was empty. But in that case, what would I have to change in my view? – mheavers May 27 '11 at 18:10