-1

im a newbie in CI, i have some question

i have controller like below

public function show_admin()
 {
  $data['data_admin'] = $this->M_Admin->get_admin();
  $data['page'] = 'show_admin';

  
  $this->load->view('admin/template',$data);
 }

and then i have template in my view folder, like below

<body>
  <div id="wrapper">
   <div id="header">
    <?php 
     $data['session'] = $this->session->all_userdata();
     $this->load->view('admin/header',$data);?>
   <div id="content">
    
    <?php 
   

    $this->load->view('admin/'.$page);?>
   </div>
   <div id="footer">
    <?php $this->load->view('admin/footer');?>
   </div>
  </div>
 </body>

and the show_admin (view file) like below

<div id="isi_content">
     <span><a href="c_add_admin"><img src="<?php echo base_url();?>assets/admin/images/add.png">Tambah</a></span>
     <table class="table table-hover table-bordered">
      <tr>
       <th>No</th>
       <th>Nama</th>
       <th>Username</th>
       <th>Login Terakhir</th>
       <th>Aksi</th>
      </tr>
      <?php
      $no=1;
      
      foreach($data_admin as $admin){
       $id = $admin['id_admin'];
       $url = site_url('admin/C_Admin/c_delete_admin');
       echo 
       '<tr>
        <td>'.$no.'</td>
        <td>'.$admin['nama_admin'].'</td>
        <td>'.$admin['username'].'</td>
        <td>'.$admin['tgl_last_visit'].'</td>
        <td class=aksi><a href='.site_url('admin/C_Admin/c_edit_admin/'.$admin['id_admin'].'').'><img src='.base_url().'assets/admin/images/edit.png></a>
         <a href=# onclick="return delete_data('.$id.',\''.$url.'\')"> <img src='.base_url().'assets/admin/images/delete.png></a></td>
       
       </tr>';
       $no++;
      }
      
      
      ?>
     </table>
    </div>

and that codes run nicely,

is that normal in codeIgniter use $this in view file??

im confuse about this.. thanks for your answer...

fajar ainul
  • 480
  • 2
  • 8
  • 27

1 Answers1

1

No, you should not use that session variable nor load any views on another view. This all should be done in the Controller. What you can do is break your code in pieces, like the header in its one view file and the footer in another view file, then you can load them in your controller like so:

public function show_admin()
{
    $data['data_admin'] = $this->M_Admin->get_admin();
    $data['page'] = 'show_admin';
    $head_data['session'] = $this->session->all_userdata();
    $this->load->view('header', $head_data);
    $this->load->view('admin/template',$data);
    $this->load->view('footer');
}
CodeGodie
  • 12,116
  • 6
  • 37
  • 66