codeigniter - 它的显示变量未定义
问题描述
这是我的控制器页面
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
解决方案
您可以将多个值存储为一个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.
希望它可以帮助你。
推荐阅读
- javascript - Javascript递归setTimeout不使用更新的值
- php - laravel 裁剪器问题不显示全尺寸图像?
- flutter - 使用 rootBundle 从资产加载 .txt 文件
- c# - 程序在我运行时抛出未处理的异常 IndexOutOfRange
- entity-framework - 迁移如何应用于数据库?
- python - 带重音的 SQLite 选择
- c - 如果它是const,如何更改数组?
- javascript - TypeScript:获取可选属性
- laravel - Laravel - 自引用关系不起作用
- python - 使用 exec 命令在 codeigniter 中执行 python