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>';
        }
        ?>

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

OK - so I tried to follow this example: stackoverflow.com/questions/3041616/… 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
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.

1 Comment

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)
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();"

9 Comments

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
Your model should be called Trees, and be loaded in the controller: $this->load->model('Trees');
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
So in the model you have return $query->result(), right? I can't see any other problem with your code.
Yeah - I've revised the code above - no errors, but no results either.
|
1

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

4 Comments

@Corey db queries should not go directly in a controller. They should be in a model or a DA layer.
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.
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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.