Search code examples
phpcodeignitercodeigniter-2codeigniter-3

Array printed correctly in controller,but not printed in the view page


print_r($listb) is working correctly in controller. but when we are passing this array to view page, its not working in the view page. Controller and view page and Model function is given below:

Controller

 public function index() {
                $this->load->helper('url');
                $this->load->view('user/templates/header');
                $data['banner'] = $this->banner_model->viewbanner();
                $this->load->view('user/templates/sidebar', $data);
                $data1['list1'] = $this->featured_model->viewfeaturedlist(1);
                $listb = array();
                foreach ($data1['list1'] as $list1) {
                    $list = explode(',', $list1->fet_list);
                    $type = $list1->fet_type;

                    foreach ($list as $pid) {
                        if ($type == 1) {

                            $listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
                            //print_r($listb);
                        }
                    }
                }

                $this->load->view('user/templates/featured', $listb);


                exit;
                $this->load->view('user/templates/footer');
            }

        }`enter code here`

View

<?php
print_r($hhh);
?>

Model

 public function viewb2bproduct($id) {
         $this->db->select('*');
        $this->db->from('jil_products');
        $this->db->where('prd_id', $id);
        $query = $this->db->get();
        return $query->result();
    }

Solution

  • In your controller Change this Line :

    $listb['hhh'] = $this->featured_model->viewb2bproduct($pid);
    

    to

    $listb['hhh'][] = $this->featured_model->viewb2bproduct($pid);
    

    In view :

    <?php 
    
    foreach ($fff as $product){
    
      echo $products[0]['prd_id']."<br>"; 
      echo $products[0]['prd_name']."<br>";  //and so on
    }