Shota Shota - 12 days ago 10
jQuery Question

Show different div if returned data is empty in CI

I am new in PHP Code Igniter. How can I display different div based on returned data from model. If there are no data returned this div is returned

<div id='error_div' class='error'>No data here</div>


But if there is a minimum of 1 data. This div should be displayed:

<?php foreach($vendor as $result){ ?>
<div id='displayed_div'>
<table id="table_list">
<tr>
<td>ID Vendor</td>
<td><input type="text" value="<?php echo $result['id_vendor']; ?>" /></td>
</tr>
<tr>
<td>Vendor</td>
<td><input type="text" value="<?php echo $result['vendor']; ?>"</td>
</tr>
</table>
</div>
<?php }; ?>


My controller to fetch data

public function detailed_vendor(){
$id_klasifikasi = $this->uri->segment(3);
$data['vendor'] = $this->model_vendor->show_vendor_by_kls($id_klasifikasi);

$this->load->view('view_vendor_detail',$data);
}


This is my model:

function show_vendor_by_kls($id_klasifikasi){
$this->db->select('*');
$this->db->from('vendor');
$this->db->join('vendor_detail', 'vendor.id_vendor = vendor_detail.id_vendor');
$this->db->join('klasifikasi', 'vendor_detail.id_klasifikasi = klasifikasi.id_klasifikasi');
$this->db->where('vendor_detail.id_klasifikasi', $id_klasifikasi);
$this->db->where('vendor.st_aktif', 1);

return $this->db->get()->result_array();
}

Answer

It doesn't matter if you're brand new on any PHP Framework or not using any frameowrk either, as long as the Server Side's Code is a PHP language, then checking using empty keyword on variable is acceptable after all:

<?php

if ( !empty( $vendor ) ) {
 foreach($vendor as $result){  ?>
   <div id='displayed_div'>
     <table id="table_list">
       <tr>
         <td>ID Vendor</td>
         <td><input type="text" value="<?php echo $result['id_vendor']; ?>" /></td>
       </tr>
       <tr>
         <td>Vendor</td>
         <td><input type="text" value="<?php echo $result['vendor']; ?>"</td>
       </tr>
    </table>
  </div>
<?php 
 }
}
else { ?>
  <div id='error_div' class='error'>No data here</div> 
<?php 
} ?>
Comments