i have a trouble with my foreach loop, it always shows warning that is: Invalid argument supplied for foreach(), can you have a look and help me solve this problem. This is my controller:
function prod_detail($id_sp){
$this->load->model('product_model');
$data['prod_detail'] = $this->product_model->getProdDetailByProdId($id_sp);
$data['prod_errors'] = $this->product_model->getProdDataError($id_sp);
$data['error_repairing'] = $this->product_model->getProdDataErrorRepairing($id_sp);
$data['rows']= $this->membership_model->getUserData();
$data['main_content'] = 'backend/home/manproduct/prod_detail_view';
$this->load->view('includes/admin/template', $data);
}
This is my model:
function getProdDataError($id_sp){
$this->db->where('id_sp', $id_sp);
$this->db->where('status', 0);
$query = $this->db->get('loi');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
function getProdDataErrorRepairing($id_sp){
$this->db->where('id_sp', $id_sp);
$this->db->where('status', 1);
$query = $this->db->get('loi');
if($query->num_rows()>0){
foreach ($query->result() as $row){
$data[]=$row;
}
return $data;
}
}
And here is my view:
if($ud->status==0){
echo 'Hoạt động';
}else if($ud->status==1){
echo '<b>Lỗi<br><ul></b>';
foreach ($prod_errors as $err) {
echo '<li>'.$err->ten_loi.'</li>';
}
echo '</ul>';
}else if($ud->status==2){
echo '<b>Đang sửa lỗi<br><ul></b>';
foreach ($error_repairing as $err) {
echo '<li>'.$err->ten_loi.'</li>';
}
echo '</ul>';
}else if($ud->status==3){
echo 'Chưa lắp đặt';
}
It's ok when $ud->status==1
but it shows a warning message when $ud->status==2
. btw, i use codeigniter to develop my web, can you help?
try this (adding checks on $prod_errors/$error_repairing before trying to iterate them)
if($ud->status==0)
{
echo 'Hoạt động';
}else if($ud->status==1)
{
echo '<b>Lỗi<br><ul></b>';
// first check $prod_errors exists and is not null before iterating it
if (isset($prod_errors))
{
foreach ($prod_errors as $err)
{
echo '<li>'.$err->ten_loi.'</li>';
}
}
else{
echo "No Data found";
}
echo '</ul>';
}else if($ud->status==2)
{
echo '<b>Đang sửa lỗi<br><ul></b>';
// first check $error_repairing exists and is not null before iterating it
if (isset($error_repairing))
{
foreach ($error_repairing as $err)
{
echo '<li>'.$err->ten_loi.'</li>';
}
}
else{
echo "No Data found";
}
echo '</ul>';
}else if($ud->status==3)
{
echo 'Chưa lắp đặt';
}