Professional Documents
Culture Documents
Praktikum Pertemuan 7
Praktikum Pertemuan 7
1. <?php
class Table extends CI_Controller {
function index() {
$this->load->library('table');
$data = array(
array('NIM', 'Nama', 'Alamat'),
array('1411500123', 'Budi', 'Jakarta'),
array('1412500456', 'Luhur', 'Jogjakarta'),
array('1413500789', 'Cakti', 'Bandung'));
echo $this->table->generate($data);
}
}
2. <?php
class Table extends CI_Controller {
function index() {
$this->load->library('table');
$this->load->database();
echo $this->table->generate($query);
}
}
3. <?php
class Table extends CI_Controller {
function index() {
$this->load->library('table');
$this->table->set_heading('NIM', 'Nama', 'Alamat');
$this->table->add_row('1411500123', 'Budi', 'Jakarta');
$this->table->add_row('1412500456', 'Luhur', 'Jogjakarta');
$this->table->add_row('1413500789', 'Cakti', 'Bandung');
echo $this->table->generate();
}
}
4. <?php
class Table extends CI_Controller {
function index() {
$this->load->library('table');
$this->table->set_heading(array('NIM', 'Nama', 'Alamat'));
echo $this->table->generate();
}
}
5. <?php
class Table extends CI_Controller {
function index() {
$this->load->library('table');
$template = array(
'table_open' => '<table border="1" cellpadding="4" cellspacing="0">',
'thead_open' => '<thead>',
'thead_close' => '</thead>',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'tbody_open' => '<tbody>',
'tbody_close' => '</tbody>',
'row_start' => '<tr>',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>');
$this->table->set_template($template);
echo $this->table->generate();
}
}
6. Mahasiswa.php
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->pagination->initialize($config);
$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
$data["result"] = $this->Mahasiswa_model->fetch_mhs($config["per_page"],
$page);
$data["links"] = $this->pagination->create_links();
$data["page"] = $page;
$this->load->view('Mahasiswa_view', $data);
}
function delete()
{
$this->Mahasiswa_model->delete($this->uri->rsegment(3));
$this->index();
}
function form_update()
{
$data['mhs'] = $this->Mahasiswa_model->getMahasiswa($this->uri-
>rsegment(3));
$this->load->view('Update_view', $data);
}
function update()
{
$this->Mahasiswa_model->update($this->input->post('old_nim'), $this->input-
>post('var'));
$this->index();
}
}
?>
Mahasiswa_view.php
<?php
echo "<h1>List Mahasiswa</h1>";
echo "<table border='1' cellpadding='5' cellspacing='0'>";
echo "<tr>
<th>No.</th>
<th>NIM</th>
<th>Nama</th>
<th>Alamat</th>
<th>Action</th>
</tr>";
$no = $page+1;
foreach($results as $content) {
echo "<tr>
<td>$no.</td>
<td>$content->nim</td>
<td>$content->nama</td>
<td>$content->alamat</td>
<td>".
anchor('mahasiswa/delete/'.$content->nim, 'Delete', 'title="Delete
Data"') . " | " .
anchor('mahasiswa/form_update/'.$content->nim, 'Update',
'title="Update Data"') .
"</td>
</tr>";
$no++;
}
echo "</table>";
echo "Halaman: ".$links; echo "<br>";
echo anchor('mahasiswa/form_tambah/', 'Tambah', 'title="Tambah Data"');
?>
Mahasiswa_model.php
<?php
function retrieve()
{
$query = $this->db->get('mhs');
if($query->result()){
foreach ($query->result() as $content) {
$data[] = array(
$content->nim,
$content->nama,
$content->alamat
);
}
return $data;
} else {
return FALSE;
}
function add($arg)
{
$data = array (
'nim' =>$arg[0],
'nama' =>$arg[1],
'alamat' =>$arg[2],
);
$this->db->insert('mhs', $data);
}
function delete($id)
{
$this->db->where('nim', $id);
$this->db->delete('mhs');
}
function getMahasiswa($id)
{
$this->db->where('nim', $id);
$query = $this->db->get('mhs');
if($query->result()){
foreach ($query->result() as $content) {
$data = array(
$content->nim,
$content->nama,
$content->alamat
);
}
return $data;
} else {
return FALSE;
}
}
if ($query->num_rows() > 0) {
foreach ($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
}
}
7. Sessions.php
<?php
class Sessions extends CI_Controller {
public function index() {
$this->load->library('session');
$this->session->set_userdata('username','budi luhur');
$this->load->view('session_view');
}
Session_view.php
<html>
<head>
<meta charset = "utf-8">
<title>Session</title>
</head>
<body>
Selamat Datang <?php echo $this->session->userdata('username'); ?>
<br>
<?php
if($this->session->userdata('username') !== null) {
echo "<a href = 'http://localhost/budiluhur/index.php/sessions/unset_sessions'>
Klik </a> untuk unset session";
}
?>
</body>
</html>