Sie sind auf Seite 1von 8

Praktek CRUD CodeIgniter (Bag.

1)

Setting di Folder Application :


A. FOLDER CONFIGS
1. FILE DATABASE.PHP

Yang diisi yang berwarna merah….!!


2. FILE CONFIG.PHP

3. ROUTES.PHP
B. FOLDER CONTROLLERS

REKAM FILE NYA DENGAN NAMA Siswa.PHP

<?PHP
class Siswa extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->model('siswa_m');
}
public function index()
{
$this->load->view('siswa_v');
}
public function add()
{
$this->load->view('siswa_form_v');
}
public function save()
{
$data['nis'] = $this->input->post('nis');
$data['nama'] = $this->input->post('nama');
$data['alamat'] = $this->input->post('alamat');

$this->siswa_m->save($data);
$this->load->view('siswa_v');
}
public function edit()
{
$row = $this->siswa_m->view($this->uri->segment(3))->row();
$data['nis'] = $row->nis;
$data['nama'] = $row->nama;
$data['alamat'] = $row->alamat;

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

public function update()


{
$data['nis'] = $this->input->post('nis');
$data['nama'] = $this->input->post('nama');
$data['alamat'] = $this->input->post('alamat');

$this->siswa_m->update($data);
$this->load->view('siswa_v');
}

public function delete()


{
$this->siswa_m->delete($this->uri->segment(3));
$this->load->view('siswa_v');
}
}
?>

C. FOLDER MODELS

REKAM FILE NYA DENGAN NAMA Siswa_M.PHP

<?PHP
class Siswa_M extends CI_Model
{
public function view_all()
{
$sql = "select * from tbl_siswa";
return $this->db->query($sql);
}

public function view($id)


{
$sql = "select * from tbl_siswa where nis='".$id."'";
return $this->db->query($sql);
}

public function save($data)


{
$sql = "insert into tbl_siswa values('".$data['nis']."', '".$data['nama']."', '".$data['alamat']."')";
$this->db->query($sql);
}

public function update($data)


{
$sql = "update tbl_siswa set nama='".$data['nama']."', alamat='".$data['alamat']."' where nis='".$data['nis']."'";
$this->db->query($sql);
}

public function delete($id)


{
$sql = "delete from tbl_siswa where nis='".$id."'";
$this->db->query($sql);
}
}
?>
D. FOLDER VIEWS

1. REKAM FILE NYA DENGAN NAMA SISWA_V.PHP

<html>
<head>
<title>Daftar siswa</title>
</head>
<body>
<h1>Daftar siswa</h1>
<a href="<?PHP echo site_url(); ?>/siswa/add">Add</a>
<table border="1">
<tr>
<th>nis</th>
<th>Nama</th>
<th>Alamat</th>

<th>Aksi</th>
</tr>

<?PHP
$query = $this->siswa_m->view_all();

foreach($query->result() as $row)
{
?>

<tr>
<td><?PHP echo $row->nis; ?></td>
<td><?PHP echo $row->nama; ?></td>
<td><?PHP echo $row->alamat; ?></td>

<td align="center">
<a href="<?PHP echo site_url(); ?>/siswa/edit/<?PHP echo $row->nis; ?>">Edit</a> |
<a href="<?PHP echo site_url(); ?>/siswa/delete/<?PHP echo $row->nis; ?>">Delete</a>
</td>
</tr>
<?PHP
}

if($query->num_rows() == 0)
{
?>

<tr>
<td colspan="6">Tidak ada data</td>
</tr>

<?PHP
}
?>

</table>
</body>
</html>

2. REKAM FILE NYA DENGAN NAMA SISWA_FORM_V.PHP

<html>
<head>
<title>Data siswa</title>
</head>
<body>
<h1>Data Siswa</h1>

<?PHP
if($this->uri->segment(2) == "add")
{
?>

<form action="<?PHP echo site_url(); ?>/siswa/save" method="post">


<?PHP
}
else
{
?>

<form action="<?PHP echo site_url(); ?>/siswa/update" method="post">

<?PHP
}
?>

<table>
<tr>
<td>nis</td>
<td><input type="text" name="nis" <?PHP if(!empty($nis)) echo 'value="'.$nis.'" readonly'; ?> /></td>
</tr>
<tr>
<td>Nama</td>
<td><input type="text" name="nama" <?PHP if(!empty($nama)) echo 'value="'.$nama.'"'; ?> /></td>
</tr>
<tr>
<td>Alamat</td>
<td><input type="text" name="alamat" <?PHP if(!empty($alamat)) echo 'value="'.$alamat.'"'; ?> /></td>
</tr>

</table>

<?PHP
if($this->uri->segment(2) == "add")
{
?>

<input type="submit" value="Save" />

<?PHP
}
else
{
?>

<input type="submit" value="Update" />

<?PHP
}
?>
<input type="button" value="Cancel" onClick="history.go(-1);" />
</form>
</body>
</html>