阵列在控制器中正确打印,但未在视图页中打印

时间:2015-10-12 06:38:47

标签: php codeigniter codeigniter-2 codeigniter-3

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

4 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在模型中

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

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