Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

PRAKTIKUM PERTEMUAN 7

Andrew Narapati (1911501474)


PRAKTIKUM 7.3

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();

$query = $this->db->query('SELECT * FROM mhs');

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'));

$this->table->add_row(array('1411500123', 'Budi', 'Jakarta'));


$this->table->add_row(array('1412500456', 'Luhur', 'Jogjakarta'));
$this->table->add_row(array('1413500789', 'Cakti', 'Bandung'));

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);

$this->table->set_heading(array('NIM', 'Nama', 'Alamat'));

$this->table->add_row(array('1411500123', 'Budi', 'Jakarta'));


$this->table->add_row(array('1412500456', 'Luhur', 'Jogjakarta'));
$this->table->add_row(array('1413500789', 'Cakti', 'Bandung'));

echo $this->table->generate();
}
}

6. Mahasiswa.php
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mahasiswa extends CI_Controller {


function _construct()
{
parent::_construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Mahasiswa_model');
$this->load->library("pagination");
}

public function index()


{
$config = array();
$config["base_url"] = base_url() . "index.php/mahasiswa/index/";
$config["total_rows"] = $this->Mahasiswa_model->record_count();
$config["per_page"] = 2;
$config["uri_segment"] = 3;

$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);
}

public function form_tambah()


{
$this->load->view('Tambah_view');
}

public function submit()


{
$this->Mahasiswa_model->add($this->input->post('var'));
$data['submitted'] = TRUE ;
$this->load->view('Tambah_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

class Mahasiswa_model extends CI_Model {

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 update($id, $form)


{
$data = array(
'nim' => $form[0],
'nama' => $form[1],
'alamat' => $form[2]
);
$this->db->where('nim', $id);
$this->db->update('mhs', $data);
}

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;
}
}

public function record_count() {


return $this->db->count_all("mhs");
}
public function fetch_mhs($limit, $start) {
$this->db->limit($limit, $start);
$query = $this->db->get("mhs");

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');
}

public function unset_sessions() {


$this->load->library('session');
$this->session->unset_userdata('username');
$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>

You might also like