lusiton
lusiton

Reputation: 19

CodeIgniter view problem

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.

Upvotes: 0

Views: 622

Answers (1)

Obto
Obto

Reputation: 1417

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

Upvotes: 2

Related Questions