skip to Main Content

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


  1. If that’s your actual code,

    class My_model extends CI_Model{
      public function fun_name($param){
        if($param = one){
           return $this->db->get_where('table', array('id' => '$param'));
        }
      }
    }
    

    then the issue is in this line below –

        if($param = one){
    

    change it to

        if($param == 'one'){
    

    .

    and in this

    return $this->db->get_where('table', array('id' => '$param'));
    

    change it to this line below because you don’t need quotes around variable

    return $this->db->get_where('table', array('id' => $param));
    

    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.

    Login or Signup to reply.
  2. If you want to select different ids from the same table using one query then you can do something like following:

    Model:

    class My_model extends CI_Model{
    
      public function fun_name($params){
        $this->db->where_in('id', $params);
        return $this->db->get('table');
      }
    }
    

    Controller:

    class My_controller extends CI_Controller{
       public function get_data(){
           $this->load->model('my_model');
           $params = array('first', 'sec', 'third');
           $data['values'] = $this->my_model->fun_name($params);
    
           $this->load->view('index', $data);
       }
    }
    

    You can also see the Codeigniter documentation:
    https://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

    Login or Signup to reply.
  3. How about changing

    if($param = 'one'){
    

    to

    if($param == 'one'){
    
    Login or Signup to reply.
  4. Can you try?

    your controller

    class My_controller extends CI_Controller{
       public function get_data(){
           $this->load->model('my_model');
           $data['data'] = array(
               'first'   =>   $this->my_model->fun_name('param1'),
               'second'  =>   $this->my_model->fun_name('param2'),
               'third'   =>   $this->my_model->fun_name('param3')
            );
           $this->load->view('index', $data);
       }
    }
    

    model

    class My_model extends CI_Model{
    
      public function fun_name($param){
           return $this->db->get_where('categories', array('type' => $param))->row()->category_name;
      }
    
    }
    

    view

    <table>
        <tr>
            <td>id</td>
            <td>name</td>
            <td>type</td>
        </tr>
        <?php 
        $i = 0;
        foreach($data as $key => $value) {
            $i++;
            ?>
        <tr>
            <td><?php echo $i; ?></td>
            <td><?php echo $key; ?></td>
            <td><?php echo $value; ?></td>
        </tr>
        <?php } ?>
    </table>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search