Invalid argument supplied for foreach() - El Forum - 07-20-2010
[eluser]pipiet06[/eluser]
Hi All..
i have a bug with foreach
Code: <h4>A PHP Error was encountered</h4>
<p>Severity: Warning</p>
<p>Message: Invalid argument supplied for foreach()</p>
<p>Filename: ed/summary.php</p>
<p>Line Number: 48</p>
this the models
Code: function get_data($limit,$start,$query,$fields,$startdate,$enddate,$idsite,$idfleet,$idprefix,$eqplan,$category,$type,$idcomponent)
{
if($query)
{
$fields = str_replace("[","",$fields);
$fields = str_replace("]","",$fields);
$fields = str_replace("\"","",$fields);
$fields = explode(",",$fields);
$search = array();
foreach ($fields as $fd)
{
$search[$fd] = $query;
}
$query = $this->db->query("SELECT a.*, b.description
FROM tblvims_summary a
LEFT JOIN tblid_event AS b ON b.id = a.id_event
OR_LIKE $search
LIMIT $start,$limit"
);
return ($query->num_rows() > 0) ? $query->result_array() : FALSE;
}
else
{
$query = $this->db->query("SELECT a.*, b.description
FROM tblvims_summary a
LEFT JOIN tblid_event AS b ON b.id = a.id_event
where startdate='$startdate' AND enddate='$enddate' AND idsite='$idsite' AND
idfleet='$idfleet' AND idprefix='$idprefix' AND eqplan='$eqplan' AND
category='$category' AND a.type='$type' AND a.idcomponent='$idcomponent'
LIMIT $start,$limit"
);
return ($query->num_rows() > 0) ? $query->result_array() : FALSE;
}
}
this is the controllers
Code: function get_data() {
$limit = ($this->input->post('limit'))? $this->input->post('limit'):10;
$start = ($this->input->post('start'))? $this->input->post('start'):0;
$query = $this->input->post('query');
$fields = $this->input->post('fields');
$startdate = $this->input->post('startdate');
$enddate = $this->input->post('enddate');
$idsite = $this->input->post('idsite');
$idfleet = $this->input->post('idfleet');
$idprefix = $this->input->post('idprefix');
$eqplan = $this->input->post('eqplan');
$category = $this->input->post('category');
$type = $this->input->post('type');
$idcomponent = $this->input->post('idcomponent');
// echo"$limit, $start";
// exit;
$this->load->model('ed/MSummary');
$query = $this->MSummary->get_data($limit,$start,$query,$fields,$startdate,$enddate,$idsite,$idfleet,$idprefix,$eqplan,$category,$type,$idcomponent);
$total = count($query);
$rowData = array("total"=>$total);
if($total > 0){
foreach ($query as $row){
$rowData['rows'][] = array(
'idvims_summary'=>$row['idvims_summary']
,'startdate'=>$row['startdate']
,'enddate'=>$row['enddate']
,'idsite'=>$row['idsite']
,'idfleet'=>$row['idfleet']
,'idprefix'=>$row['idprefix']
,'eqplan'=>$row['eqplan']
,'category'=>$row['category']
,'type'=>$row['type']
,'idcomponent'=>$row['idcomponent']
,'id_event'=>$row['id_event']
,'description'=>$row['description']
,'rating'=>$row['rating']
);
}
}
else{
$rowData['rows'] = "";
}
echo json_encode($rowData);
}
How can I achieve this problem.?
can someone help me?
Thanks before :lol:
Invalid argument supplied for foreach() - El Forum - 07-21-2010
[eluser]davidbehler[/eluser]
Quote:If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.
from http://php.net/manual/en/function.count.php
That being said, your error is quite easy to fix.
Your model returns either an array or FALSE. count() will return the number of elements in your array or 1 if the returned values from your model is FALSE.
Instead of
I would use
Code: if(is_array($query) {
}
or
Code: if($total > 0 AND $query !== FALSE){
}
Hope that helps.
|