I created a method in the model to get data from the database and then call it in the controller for multiple time with different method parameters($param) values. This is working for only one parameter value which one comes first. I Know i am doing it in wrong way but i am unable to find the solution for this. please help.
Actually, I am trying to have category based on their type(‘wordpress’, ‘magento’, ‘shopify’) form database
My_model.php
class My_model extends CI_Model{
public function fun_name($param){
return $this->db->get_where('categories', array('type' => '$param'));
}
}
My_controller.php
class My_controller extends CI_Controller{
public function get_data(){
$this->load->model('my_model');
$data = array(
'first' => $this->my_model->fun_name('wordpress'),
'second' => $this->my_model->fun_name('magento'),
'third' => $this->my_model->fun_name('shopify')
);
$this->load->view('index', $data);
}
}
4
Answers
If that’s your actual code,
then the issue is in this line below –
change it to
.
and in this
change it to this line below because you don’t need quotes around variable
Obviously your code can be optimised better i.e. you may not need if – else the way you are doing but again without knowing the full scenario it’s not easy to recommend a solution.
If you want to select different ids from the same table using one query then you can do something like following:
Model:
Controller:
You can also see the Codeigniter documentation:
https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data
How about changing
to
Can you try?
your controller
model
view