0

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

1
  • You don't. That goes against everything MVC architecture is about. You perform your code in the controller and/or model, and you use that data in the view. Commented Mar 25, 2014 at 21:43

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Nono. I've figured out how to do that. I wan't to do an UPDATE query based on information from my view, inputs in a form :)
Easy, you'll just have to fetch those values by $_POST['fieldName'] then do the thing you want to do ;) This is actually done to the 'controller/functionName' where functionName is where your form is submitted.
0

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

Comments

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.