Setelah kita membuat databases dbbelajarcrud dan membuat tabel admin, berikutnya melakukan konfigurasi atau setting framework codeigneter dan saatnya sekarang kita membuat control, model dan view
1. Membuat Control yakni control Admin.php pada lokasi application\controllers\Admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller{
private $view = "backend/v_admin/";
public function __construct()
{
parent::__construct();
//Codeigniter : Write Less Do More
$this->load->model('M_admin');
}
function index()
{
$read = $this->M_admin->GetAll();
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Lihat Admin",
'read'=> $read
);
$this->load->view($this->view.'read', $data);
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends CI_Controller{
private $view = "backend/v_admin/";
public function __construct()
{
parent::__construct();
//Codeigniter : Write Less Do More
$this->load->model('M_admin');
}
function index()
{
$read = $this->M_admin->GetAll();
$data = array(
'judul' => "DATA ADMIN",
'sub' => "Lihat Admin",
'read'=> $read
);
$this->load->view($this->view.'read', $data);
}
}
2. membuat Model dengan model M_admin.php pada lokasi application\models\M_admin.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class M_admin extends CI_Model{
private $table = 'admin';
private $pk = 'kd_admin';
.
public function GetAll()
{
$this->db->order_by("nama_admin", 'asc');
return $this->db->get($this->table);
}
}
defined('BASEPATH') OR exit('No direct script access allowed');
class M_admin extends CI_Model{
private $table = 'admin';
private $pk = 'kd_admin';
.
public function GetAll()
{
$this->db->order_by("nama_admin", 'asc');
return $this->db->get($this->table);
}
}
3. membuat view yakni read.php dengan lokasi penyimpanan application\views\backend\v_admin\read.php
<table border="1" width="60%">
<tr>
<td>No</td>
<td>Kode</td>
<td>Nama</td>
</tr>
<?php
$no=1;
foreach ($read->result_array() as $row) {
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $row['kd_admin'] ?></td>
<td><?php echo $row['nama_admin'] ?></td>
</tr>
<?php
$no++;
}
?>
</table>
<tr>
<td>No</td>
<td>Kode</td>
<td>Nama</td>
</tr>
<?php
$no=1;
foreach ($read->result_array() as $row) {
?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $row['kd_admin'] ?></td>
<td><?php echo $row['nama_admin'] ?></td>
</tr>
<?php
$no++;
}
?>
</table>
Tidak ada komentar:
Posting Komentar