0

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?

1 Answer 1

3

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';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Oh it shows no data found now, but i did insert a data with $status==1 in my database, what is the problem, can you help
Thank you alot, i did find my problem. i forgot to insert a data for the product that i am looking for, it is hard to explain because the problem here is no data to show. anw i fixed it, thanks
I hope you don't mind me editing your otherwise fine answer to emphasize the changes you've introduced ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.