我试图传递一个数组,当从数据库中查询它时,它会被初始化。
我试图传递并在视图中显示的产品数组只是抛出了一个错误,我不知道为什么。我使用的是codeigniter 3。
产品型号.php
function get_all_products()
{
$this->db->select("produceCode,description,category,supplier, quantityInStock, bulkSalePrice,photo");
$this->db->from('products');
$query = $this->db->get();
return $query->result();
}
产品控制器.php
public function listProducts()
{
$data['p']=$this->ProductsModel->get_all_products(/*2, $this->uri->segment(3)*/);
$this->load->view('products',$data);
}
产品.php
<?php foreach( $p as $row) { ?> // line of code thats causing the error
<div class="col-md-3">
<div class="card p-3">
<div class="text-center"> <!--<img src="../assets/images/products/thumbs/--><?php /*$row->photo;*/?>" width="200"> </div>
<div class="product-details"> <span class="font-weight-bold d-block">$ 7.00</span> <span><?php echo 'hi'/*$row->description;*/?></span>
<div class="buttons d-flex flex-row">
<div class="cart"><i class="fa fa-shopping-cart"></i></div> <button class="btn btn-success cart-button btn-block"><span class="dot">1</span>Add to cart </button>
</div>
</div>
</div>
</div>
<?php } ?>
任何建议都很好
1条答案
按热度按时间o75abkj41#
您在控制器中返回了一个名为
$data
的变量,但在视图中却在$p
中循环。试试这个:
foreach ($data['p'] as $row)