首页 > 解决方案 > 它的显示变量未定义

问题描述

这是我的控制器页面

class Cart extends CI_Controller{ 

公共函数索引(){

    $getALlCat  = $this->Product_model-> getAllCategory();
     $data = array();

     //Retrieve cart Data
     $data['cartItems']  = $this->cart->contents();


    //print_r($data);exit;
    //$this->load->view('cart/index' ,$data);
    ///$this->load->helper('url');
 / /$this->load->view('cart/header',['getAllCate'=>$getALlCat], $data);    //including header and 
footer   
    $this->load->view('cart/cartbody',['getAllCate'=>$getALlCat], $data);

    $this->load->view('common/footer');    //including header and footer
  }
}



**This is View page**



<tbody>
                        <?php if($this->cart->total_items() > 0){ foreach($cartItems as $item){    ?>
                        <tr>
                            <td><img src="<?php echo $item['product_image']; ?>" width="50"/></td>
                            <td><?php echo $item['product_name']; ?></td>
                            <td><?php echo '$'.$item["product_price"].' USD'; ?></td>
                            <td><input type="number" class="form-control text-center"></td>
                            <td><?php echo '$'.$item["subtotal"].' USD'; ?></td>
                            <td>Are you sue</td>
                     </tr>
                     <?php } }else{ ?>
                        <td colspan="6"><p>Your cart is empty.....</p></td>
                        <?php } ?>
                    </tbody>






**Error Showing like this**

A PHP Error was encountered
Severity: Notice

消息:未定义变量:cartItems

文件名:购物车/cartbody.php

行号:217

回溯:

文件:C:\xampp\htdocs\shop\application\views\cart\cartbody.php 行:217 功能:_error_handler

文件:C:\xampp\htdocs\shop\application\controllers\Cart.php 行:33 功能:查看

文件:C:\xampp\htdocs\shop\index.php 行:315 功能:require_once

标签: codeigniter

解决方案


您可以将多个值存储为一个array key$data然后仅发送$data以查看并使用这些键来检索值。我已经在下面更正了您的代码,它应该可以满足您的需要。我已经在必要时提到了评论。

 public function index() {

    $data = array();

    $data['getALlCate']  = $this->Product_model-> getAllCategory(); // store it in an array instead (I've changed it to getALlCate from getALlCat, so you don't have to change anything in your view)

    //Retrieve cart Data
    $data['cartItems']  = $this->cart->contents();

    //$this->load->view('cart/header', $data); // including header and footer   
    $this->load->view('cart/cartbody', $data);

    $this->load->view('common/footer');    //including header and footer
  }
}
// you can retrieve data in view by accessing $cartItems and $getALlCate variables.

希望它可以帮助你。


推荐阅读