这是我的控制器页面
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
消息:未定义的变量:购物车项
文件名:购物车/车体.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
您可以将多个值存储为$data
array key
,然后仅发送$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.
希望对您有所帮助。
尝试以下
$this->load->view('cart/cartbody',['getAllCate'=>$getALlCat, 'cartItems'=> $this->cart->contents()]);