Codeigniter query from view

I recently made a website controlled by a database, and I want to make an admin panel for it. How do I make a SQL query from a view? Maybe via a model? I haven't used a model before btw...

Upvotes: 0

Views: 262

Answers (2)

YouSer
YouSer

Reputation: 393

I don't why you have to query in view, but as far as I know it is not recommended (I don't know if it is even possible). But how about calling the function that does the query (which is from the model) from the controller, then store the results on a variable then pass them to the view for printing via foreach.

MODEL

function query($query){
   return $this->db->query($query);
}

CONTROLLER

function index(){
   $data['results'] = $this->modelName->query($myQuery);
   $this->load->view('viewName', $data);
}

VIEW

<html>
    foreach($results as $result){
        //echo every column of your table
    }
</html>

Upvotes: 2

Charlie
Charlie

Reputation: 9118

You would load data from the database into a model and pass the model to the view. The view would then post back to the controller with a modified version of the model and the controller would then modify the database.

http://ellislab.com/codeigniter/user-guide/database/index.html

Upvotes: 0

Related Questions