You are on page 1of 2

4/3/2019 php - Load multiple models within same function of controller code igniter - Stack Overflow

Home

PUBLIC

Stack Overflow

Tags
Load multiple models within same function of controller code Ask Question
Users
igniter
Jobs

I have a page where I want to display dynamic two select boxes.One


Teams select box get data from one model and then other select box get from
Q&A for work 1 other.Then after getting the data it will save the query or result of both
of them and then view into one view page .
Learn More
I want to save the data in array of two queries and this is my
controller:

function index(){

$data['main_content'] = 'stockInView';

$this->load->model('itemsModel');
$query2 = $this->itemsModel->getAllItems();
if ($query2)
{
$data['records'] = $query2;

$this->load->model('categoryModel');
$query = $this->categoryModel->getAllCategories();
if ($query)
{
$data['records'] = $query;

$this->load->view('dashboardTemplate/template',$data);
}

And then I display them in a foreach loop . But it is only able to display
the 2nd query results or in this case display only categories in one
select box and not the items in other select box.I am doing this for the
first time so I don't know how to do it.

php ajax codeigniter

edited Jan 12 '13 at 13:17


Sachin Prasad
3,557 9 41 82

asked Jan 12 '13 at 10:32


user1972143
39 1 8 18

3 Answers

Сhange the line $data['records'] = $query2; when working with


category model to a different $data array element key like this:
2 $this->load->model('itemsModel');
$query2 = $this->itemsModel->getAllItems();
if ($query2)
{
$data['itemRecords'] = $query2;
}

$this->load->model('categoryModel');
$query = $this->categoryModel->getAllCategories();
if ($query)
{
$data['categoryRecords'] = $query;
}

$this->load->view('dashboardTemplate/template',$data);

Then in your view you use $itemRecords and $categoryRecords arrays


to output them to their select boxes.

answered Jan 12 '13 at 12:03


By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Sergey Telshevsky
https://stackoverflow.com/questions/14292452/load-multiple-models-within-same-function-of-controller-code-igniter 1/2
4/3/2019 php - Load multiple models within same function
10.1k of controller
6 41 code 73 igniter - Stack Overflow

You are over writing the value of $data['records'] = $query2; with


$data['records'] = $query; So end you will only get second value. You
0 should different array fields like $data['records2'] = $query2;
$data['records'] = $query2;

answered Jan 12 '13 at 10:47


Sankaran
1 2

1 ohh.. thanks a lot.. ok tell me how can i do this.. if i select one option from
first select box then it will give me options in 2nd select box according to
that.. i think it will be done by applying the onchange function of javascript.. i
dont know how to i do it here ,, and also if it will be done without refresing by
using ajax or jquery then it will be more good – user1972143 Jan 12 '13 at
10:54

@user1972143 this is a completely different topic, learn AJAX to update the


second select box values when selecting a value from the first one, it can be
easily achieved with jQuery, there are lots of information regarding this
available – Sergey Telshevsky Jan 12 '13 at 12:06

You have two model which is itemsModel and categoryModel. If you


load this two model in construct function then these two model are all
0 time loded in this controller. So you can load this two model in
Construct function. have a look the following code....

public function __construct() {


parent::__construct();
$this->load->model('itemsModel');
$this->load->model('categoryModel');
}

public function index() {

$data['main_content'] = 'stockInView';

$query2 = $this->itemsModel->getAllItems();
if ($query2) {
$data['itemRecords'] = $query2;
}

$query = $this->categoryModel->getAllCategories();
if ($query) {
$data['categoryRecords'] = $query;
}

$this->load->view('dashboardTemplate/template', $data);
}

edited Jan 14 '13 at 6:27

answered Jan 14 '13 at 4:38


Sharif
548 2 10

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
https://stackoverflow.com/questions/14292452/load-multiple-models-within-same-function-of-controller-code-igniter 2/2

You might also like