本文介绍了尝试获取子类别中非对象产品详细信息的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想显示所选子类别的一种产品,但出现错误消息:尝试获取非对象的属性
以查看产品详细信息,请如果控制器,模型和视图中存在错误,则可以对其进行纠正。
I want to show one product of sub-categories selected, but there was an error message: Trying to get property of non-object
in view product detail, please to be corrected if there are mistakes in the controller, model, and view.
我的错误在哪里?
查看
<?php foreach ($categories as $key=>$value) { ?>
<div class="col-sm-3">
<h5><?php echo $categories[$key]->categoriesName; ?></h5>
<?php if(count($categories[$key]->subs)>0) { ?>
<?php foreach ($categories[$key]->subs as $k=>$v) { ?>
<ul>
<li><a href="<?php echo base_url();?>member/detail/"><?php echo $v->productName; ?></a> </li>
</ul>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
<div class="row" id="productMain">
<div class="col-sm-4">
<div id="mainImage"><img src="<?php echo base_url('upload/'.$details->photo);?>"
alt="" class="img-responsive">div>
</div>
<div class="col-sm-4">
<div class="box">
<h1 class="text-center"><?php echo $details->productName;?></h1>
<p class="price">
<?php echo $details->price;?>
</p>
</div>
</div>
</div>
模型
public function get_categories() {
$query = $this->db->get('categories');
$return = array();
foreach ($query->result() as $category) {
$return[$category->categoriesID] = $category;
$return[$category->categoriesID]->subs =
$this->get_sub_categories($category->categoriesID);
}
return $return;
}
public function get_sub_categories($category_id) {
$this->db->where('categoriesID', $category_id);
$query = $this->db->get('product');
return $query->result();
}
public function get_details($product_id)
{
$this->db->where("productID",$product_id);
$query = $this->db->query("SELECT * FROM product");
$row = $query->row();
return $row;
}
控制器
public function detail($product_id){
$data=array('title' =>'Pasar Online | Detail Produk',
'username' => $this->session->userdata('username'),
'categories' => $this->categories_model->get_categories(),
'details' => $this->categories_model->get_details($product_id),
'isi' =>'member/detail');
$this->load->view('template_home/wrapper',$data);
}
print_r($ categories)
print_r($categories)
Array ( [1] => stdClass Object
( [categoriesID] => 1 [categoriesName] => Laptop [subs] => Array
( [0] => stdClass Object ( [productID] => P0001 [categoriesID] => 1 [productName] => AAA )
[1] => stdClass Object ( [productID] => P0002 [categoriesID] => 1 [productName] => BBB )
[2] => stdClass Object ( [productID] => P0004 [categoriesID] => 1 [productName] => CCC ) ) )
[2] => stdClass Object ( [categoriesID] => 2 [categoriesName] => TV [subs] => Array
( [0] => stdClass Object ( [productID] => P0005 [categoriesID] => 2 [productName] => loren epsum ) ) )
[3] => stdClass Object ( [categoriesID] => 3 [categoriesName] => Handphnone [subs] => Array
( [0] => stdClass Object ( [productID] => P0003 [categoriesID] => 3 [productName] => Manggoes )
[1] => stdClass Object ( [productID] => P0006 [categoriesID] => 3 [productName] => Pinneapple ) ) ) )
print_r($ details)
print_r($details)
stdClass Object ( [productID] => P0001 [categoriesID] => 1 [productName] => AAA ) 1
故障在循环 $ details
推荐答案
像这样尝试...在视图中:
Try like this...in view:
<?php foreach ($categories as $key=>$value) { ?>
<div class="col-sm-3">
<h5><?php echo $categories[$key]->categoriesName; ?></h5>
<?php if(count($categories[$key]->subs)>0) { ?>
<?php foreach ($categories[$key]->subs as $k=>$v) { ?>
<ul>
<li><a href="<?php echo base_url();?>member/detail">
<?php echo $v->productName; ?></a></li>
</ul>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
<div class="row" id="productMain">
<div class="col-sm-4">
<div id="mainImage"><img src="<?php echo base_url('upload/'.$details->photo);?>"
alt="" class="img-responsive">div>
</div>
<?php if(count($details)>0){
<div class="col-sm-4">
<div class="box">
<h1 class="text-center"><?php echo $details->productName;?></h1>
<p class="price">
<?php echo $details->price;?>
</p>
</div>
</div>
<?php } ?>
</div>
型号:
将product_id发送到哪里产品详细信息的条件
Send product_id in where condition for product details
public function get_details($product_id) {
$this->db->where("productID",$product_id);
$query = $this->db->query("SELECT * FROM product");
$row = $query->row();
return $row;
}
这篇关于尝试获取子类别中非对象产品详细信息的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!