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

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟