0

This is my first day playing with CI and I really like it so far but have a problem I can't solve on my own. The issue is that I need to generate single view with two controller functions. One div should include selected row by ID from table A and other div should loop foreach on array from table B.

    public function index()//div A
{
            $data['query'] = $this->db->get_where('beer', array('id' => 1));
    $this->load->view('corp/corp_view', $data);     
}

public function loadList() //div B
{
    $data['q'] = $this->db->get_where('list', array('id' => 1));
    $this->load->view('corp/mentor_list_view', $data);
}   

I tried to solve this for few hours by creating another view for loadList() and then including it in the the main view like "$this->load->view()" but I'm getting the values from the index() function query table 'beer' not 'list' table as intended. Again I'm new to this and would appreciate your help.

Thank you for your help.

2
  • I don't completely understand your question. Are you trying to load both views on a specific page? So foo.com/index.php/index loads both the views in index() and loadList()? Can you be a little more descriptive? Commented Jun 15, 2011 at 0:24
  • Hi Fox. I need to query two tables and show result of both on same page. I was trying to have two functions in the controller each is performing separate query. I guess the question could be what I could do to combine the returning data on one page. Code example would help. Commented Jun 15, 2011 at 0:32

1 Answer 1

2

Thanks for the extra info, i can help yah out now.

In Codeigniter if you want to make a function that is not able to be called by the user just precede it with a '_'. So in your case:

public function index()//div A
{
    $data['query'] = $this->db->get_where('beer', array('id' => 1));
    $data['query2'] = $this->_mySecondQuery();
    $this->load->view('corp/corp_view', $data);     
}

public function _mySecondQuery() //div B
{
    return $this->db->get_where('list', array('id' => 1));
}

Now in the index page you have access to both queries. Btw, I would not suggest doing much DB work in the controller. DB work is meant to be done in Models. For more info on those see: Codeigniter Models

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

4 Comments

I tried to use your example but I get error ->Call to undefined function _mySecondQuery(). What could be the reason for this error? Thanks
ahha sorry i forgot to use $this-> to reference the function in the class. Sorry about that
Thank you very much for your help. Now I'll try to separate the code to controller and model. What would be your way of doing this? I really want to learn good practices of doing this.
Id suggest this tut, it's what i first used when i started. BEWARE, check the link i posted in the answer for the up to date info on models. Some little thing have changed. Also, please choose this as the correct answer if it helped :D net.tutsplus.com/articles/news/codeigniter-from-scratch-day-2

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.