I have the following code in CodeIgniter Controller.It returns the Invalid argument supplied for foreach()
error when it enable to retrieve the data from the model. I want to display the "No records found" instead of the Invalid argument.. error .
Here is my full code
Controller
function registered_customers_bod(){
// get the data and pass to records Variable
if($query = $this->mod_reports->registered_customers_bod())
{
$data['records'] = $query ;
}else{
$data['records'] = 'No records found';
}
$data['report_name'] = $this->input->post('report_name');
// load the view passing data Variable
$this->load->view('admin/reports/user_reports_bod.html',$data);
}
View - user_reports_bod
<?php if (isset($records)) : $i = 1; $delivered_qty_total = 0; foreach ($records as $row) : ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row->cus_id; ?></td>
<td><?php echo $row->cus_name; ?></td>
<td><?php echo $row->cus_email; ?></td>
<td><?php echo $row->cus_phone; ?></td>
<td><?php echo $row->cus_mobile; ?></td>
<td><?php echo $row->cus_addr_city; ?></td>
<td><?php echo $row->user_status; ?></td>
<td><?php echo gmdate("Y/m/d", $row->user_timestamp); ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
Model
function registered_customers_bod(){
$to = new DateTime($this->input->post('to'));
$from = new DateTime($this->input->post('from'));
$this->db->select(array(
'tbl_customer_registration.cus_id',
'tbl_customer_registration.cus_name',
'tbl_customer_registration.cus_email',
'tbl_customer_registration.cus_phone',
'tbl_customer_registration.cus_mobile',
'tbl_customer_registration.cus_addr_no',
'tbl_customer_registration.cus_addr_street',
'tbl_customer_registration.cus_addr_city',
'tbl_user_registration.user_status',
'tbl_user_registration.user_reason_status',
'tbl_user_registration.user_timestamp',
));
$this->db->from('tbl_customer_registration');
$this->db->join('tbl_user_registration', 'tbl_user_registration.user_id=tbl_customer_registration.user_id');
$this->db->group_by("tbl_customer_registration.cus_id"); //view single record that contain two contact numbers
$this->db->where('user_timestamp >=',$to->getTimestamp());
$this->db->where('user_timestamp <=',$from->getTimestamp());
$query = $this->db->get();
return $query->result();
}
if($query = $this->mod_reports->registered_customers_bod()){
$data['records'] = $query ;
}else{
$data['records'] = new stdClass(); // change here
}
the reason behind the error is when no record is found from db your $data['records']
is a String and which is surely Invalid argument for foreach()
and instead of isset($records)
in view use empty($records)
and put else in view EDIT
$arr = (array)$records;
if(!empty($arr)){
$i = 1;
$delivered_qty_total = 0;
foreach($records as $row) { ?>
<tr>
<td><?php echo $i++; ?></td>
<td><?php echo $row->cus_id; ?></td>
<td><?php echo $row->cus_name; ?></td>
<td><?php echo $row->cus_email; ?></td>
<td><?php echo $row->cus_phone; ?></td>
<td><?php echo $row->cus_mobile; ?></td>
<td><?php echo $row->cus_addr_city; ?></td>
<td><?php echo $row->user_status; ?></td>
<td><?php echo gmdate("Y/m/d", $row->user_timestamp); ?></td>
</tr>
<?php } ?>
}
else { echo "No records found";}