duanshai4484 2016-02-01 01:47
浏览 42
已采纳

Codeigniter与foreach循环有错误

my problem is that, when i try to get a product detail from the table, i click on the product's name and it will lead me to product_detail, but when i run i show the problem is:

Invalid argument supplied for foreach()

Can you have a look and tell me what can i do to solve this problem? this is my view:

<?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>

And this is my controller

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);
}

And this is my 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;
    }
}
  • 写回答

2条回答 默认 最新

  • dongxi7704 2016-02-01 05:25
    关注

    You Are fetching only one row not an array..

     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);
    

    This Wont Give You an array Its only gives You row..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害