if records does not exist in database like that form i want to display but empty form displaying
Model code
No record found in database, Invalid argument supplied for foreach() like that error display and in view page form will not display if
data found in database it will display please help me
public function buyer_details() {
$this->db->select('*');
$this->db->from('customer_otherdetails');
$this->db->where('customerid_fk', $this->session->id);
$query = $this->db->get();
// return $query->result();
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
//add all data to session
$newdataa = array(
'address' => $row->address,
'pincode' => $row->pincode,
'city' => $row->city,
'district' => $row->district,
'state' => $row->state,
'country' => $row->country,
);
}
$this->session->set_userdata($newdataa);
return $query->result();
}
}
controller code
$data['buyerdetails'] = $this->Profile_model->buyer_details();
$this->load->view('buyerdetails', $data);
view page
<?php
foreach ($buyerdetails as $row) {
?>
<li class="has-sub">
<span>Area</span>
<span><?php echo $row->address; ?></span>
<a data-toggle="collapse" data-parent="#accordion" href="#collapsefive">
<?php
if (empty($buyerdetails) == $this->session->userdata('address')) {
?>
<span class="profile-edit">Add</span>
<?php } else { ?>
<span class="profile-edit">Edit</span>
<?php } ?>
</a>
<div style="clear:both;"></div>
</li>
<div class="sub">
<div class="panel-body ">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 text-center">
<label style="display:inline;">Area</label>
<input type="hidden" name="id" value="<?php echo $row->customerid_fk; ?>" />
<input style="width:50%;height:20px;" type ="text" name="address" class="form-control" value="<?php echo $row->address; ?>"/></br>
<p style="font-size: 12px;"><b>Note:</b> Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</p>
<input type="submit" value="Save" class="btn btn-success" style="width:70px;">
<!-- <button type="button" class="btn btn-success">Save</button>-->
<button type="button" class="btn btn-warning cancel-name">Cancel</button>
</div>
</div>
</div>
</div>
<?php } ?>
The problem is that buyer_details
does not always return something. You need to put a return outside of the if($query->num_rows() > 0)
condition.
public function buyer_details()
{
$this->db->select('*');
$this->db->from('customer_otherdetails');
$this->db->where('customerid_fk', $this->session->id);
$query = $this->db->get();
$rows = $query->result(); //so you only have to call result once
if($query->num_rows() > 0)
{
foreach($rows as $row)
{
//add all data to session
$newdataa = array(
'address' => $row->address,
'pincode' => $row->pincode,
'city' => $row->city,
'district' => $row->district,
'state' => $row->state,
'country' => $row->country,
);
}
$this->session->set_userdata($newdataa);
//put the return outside the if
//return $query->result();
}
return $rows; //this will be an empty array if no data found
}
$rows
may be an empty array if no data is found. But foreach
won't complain about an empty array. An empty array is still a valid argument.