数组在控制器中正确打印,但未在视图页面中打印

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

print_r($listb) 在控制器中正常工作。但是当我们将这个数组传递给查看页面时,它在查看页面中不起作用。控制器和视图页面以及模型功能如下:

控制器

 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`

查看

<?php
print_r($hhh);
?>

型号

 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();
    }

您的 $listb['hhh'] 在 foreach 循环中,您每次都在为 $list 的每个元素覆盖它。在控制器中,您在 foreach 循环中打印它,这就是为什么它提供输出但不在您的 view.

中的原因

模型中

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

在控制器中

public function index() 
{
        $this->load->helper('url');

        $data['banner'] = $this->banner_model->viewbanner();

        $data1['list1'] = $this->featured_model->viewfeaturedlist(1); 

        $this->load->view('user/templates/sidebar', $data);
        $this->load->view('user/templates/header');
        $this->load->view('user/templates/featured', $listb);
        $this->load->view('user/templates/footer');
    }

}

可见

<?php
    foreach ($list1 as $new_list1) 
    {
        echo "<p>".$new_list1['table_field']."</p>"
    }

对这部分一无所知

   $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);
            }
        }
    }

在您的控制器中更改此行:

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

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

在视图中:

<?php 

foreach ($fff as $product){

  echo $products[0]['prd_id']."<br>"; 
  echo $products[0]['prd_name']."<br>";  //and so on
}
      $hhh=array();

      foreach ($data1['list1'] as $list1) {
                $list = explode(',', $list1->fet_list);
                $type = $list1->fet_type;

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

                        $hhh[] = $this->featured_model->viewb2bproduct($pid);
                        //print_r($listb);
                    }
                }
            }
        $listb['hhh']=$hhh;
       $this->load->view('user/templates/featured', $listb);