4

I have query that runs in the controller:

$data['query'] = $this->Member->select_sql($id);    
$this->load->view('myform');

and then outputs data in the view:

foreach ($query->result() as $row):
   echo $row->post_title;
   echo $row->post_user_id;
endforeach;

So this outputs me a list of posts made by a user. Now I would like to run one more query that would for each post loop through my user table and output user information next to each post. (I dont want to select data from a view or joint those 2 tables at this time in MySQL)

Any ideas?

3
  • 1
    I'm not sure that this is such a good idea - the whole point of MVC is to enforce clean code structure. You're not supposed to have business logic in views. Commented Jan 15, 2010 at 23:33
  • this is only for a testing purpose, to concerning performance. thank you anyway. Commented Jan 15, 2010 at 23:52
  • 1
    @Veeti There is nothing wrong with calling upon the Model from the View. See stackoverflow.com/questions/1973221/… Commented Jan 16, 2010 at 10:29

3 Answers 3

7

Although it is not a good practice, the "cleanest" approach would be as follows:

  • Grab the CI instance in the View
  • Load the model containing your desired data extraction query functions
  • Run the function from the model in the view

So, in the view:

$CI =& get_instance();
$CI->load->model('modelname');
$result = $CI->modelname->functionname();
var_dump($result);

Tested and working.

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

Comments

5

Inject the database adapter or appropriate table object into the View.

From your code above, I'd assume this would be

$data['userModel'] = $this->User;

Then use it from there to run your query, e.g.

$user = $userModel->select_sql($row->post_user_id);

Comments

0

Simply

<?php 
                        $qryd='select * from '.$tbname.'';
                        $queryd = $this->db->query($qryd);
                        $resultset = $queryd->result_array();
                    ?>

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.