我的问题是,当我尝试从表中获取产品详细信息时,我点击产品的名称,它将引导我到product_detail,但是当我运行时我显示问题是:
为foreach()提供的参数无效
你能看看并告诉我如何解决这个问题? 这是我的观点:
<?php foreach ($prod_detail as $ud) { }?>
<form method="POST" name="fr_update">
<input type="hidden" name="id_sp" value="<?php echo $ud->id_sp; ?>">
<div class="row" style="margin-bottom: 10px">
<div class="col-md-2">
<p>Tên sản phẩm</p>
</div>
<div class="col-md-4">
<input type="text" class="form-control" style="max-width: 365px;" name="ten_sp" readonly value="<?php echo $ud->ten_sp;?>">
</div>
<div class="col-md-2">
<p>Ngày sản xuất:</p>
</div>
<div class="col-md-4" style="padding-left: 0px">
<input align="left" type="date" class="form-control" style="max-width: 365px;" name="ngay_sx" readonly value="<?php echo $ud->ngay_sx;?>"/>
</div>
这是我的控制器
function prod_detail($id_sp){
$data['main_content'] = 'backend/home/manproduct/prod_detail_view';
$this->load->model('product_model');
$data['prod_detail'] = $this->product_model->getProdDetailByProdId($id_sp);
$data['rows']= $this->membership_model->getUserData();
$data['row']= $this->product_model->getProdData();
$this->load->view('includes/admin/template', $data);
}
这是我的product_model
function getProdDetailByProdId($ten_sp){
$this->db->where('ten_sp', $ten_sp);
$query = $this->db->get('products');
if ($query->num_rows() == 1) {
$data = $query->row();
return $data->$id_sp;
}
}
function getProdData(){
$this->db->where('ten_sp', $ten_sp);
$query = $this->db->get('products');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
答案 0 :(得分:1)
您只获取一行而不是数组..
function getProdDetailByProdId($ten_sp){
$this->db->where('ten_sp', $ten_sp);
$query = $this->db->get('products');
if ($query->num_rows() == 1) {
$data = $query->row(); //here
return $data->$id_sp;
}
}
$data['prod_detail'] = $this->product_model->getProdDetailByProdId($id_sp);
这不会给你一个数组它只给你排..
答案 1 :(得分:0)
$query->row();
此方法返回单个结果行。如果您的查询有多行,则只返回第一行。结果作为对象返回。
因此,如果您仍想使用foreach,则无需使用foreach
,然后使用$query->result()
代替$query->row()