 <pre><code> if($query = $this-&gt;mod_reports-&gt;registered_customers_bod()){ $data['records'] = $query ; }else{ $data['records'] = new stdClass(); // change here } </code></pre> <p>the reason behind the error is when no record is found from db your <code>$data['records']</code> is a <strong>String</strong> and which is surely <code>Invalid argument for foreach()</code></p> <p>and instead of <code>isset($records)</code> in view use <code>empty($records)</code></p> <p>and put else in view <strong>EDIT</strong></p> <pre><code>$arr = (array)$records; if(!empty($arr)){ $i = 1; $delivered_qty_total = 0; foreach($records as $row) { ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $i++; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row-&gt;cus_id; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row-&gt;cus_name; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row-&gt;cus_email; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row-&gt;cus_phone; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row-&gt;cus_mobile; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row-&gt;cus_addr_city; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $row-&gt;user_status; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo gmdate("Y/m/d", $row-&gt;user_timestamp); ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php } ?&gt; } else { echo "No records found";} </code></pre>