Created
June 2, 2017 03:07
-
-
Save dwijonarko/0e01296f092e637679a13944608e0feb to your computer and use it in GitHub Desktop.
CRUD+Upload Codeigniter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- application/views/template/edit.php --> | |
<div class="container"> | |
<h1><?php echo $title ?></h1><hr> | |
<form class="form-horizontal" action="<?php echo base_url()?>index.php/mahasiswa/update" method="POST" enctype="multipart/form-data"> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="nim">Nomor Induk Mahasiswa</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="text" name="nim" id="nim" class="form-control" placeholder="Nomor Induk Mahasiswa" value="<?php echo $mahasiswa->nim ?>" readonly> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="nama">Nama Lengkap</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="text" name="nama" id="nama" class="form-control" placeholder="Nama Lengkap Mahasiswa" value="<?php echo $mahasiswa->nama ?>" > | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="tmp_lahir">Tempat Lahir</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="text" name="tmp_lahir" id="tmp_lahir" class="form-control" placeholder="Tempat Lahir Mahasiswa" value="<?php echo $mahasiswa->tmp_lahir ?>" > | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="tgl_lahir">Tanggal Lahir</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="date" name="tgl_lahir" id="tgl_lahir" class="form-control" value="<?php echo $mahasiswa->tgl_lahir ?>"> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="alamat">Alamat</label> | |
</div> | |
<div class="col-sm-8"> | |
<textarea name="alamat" id="alamat" cols="30" rows="10" class="form-control"><?php echo $mahasiswa->alamat ?></textarea> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="gender">Jenis Kelamin</label> | |
</div> | |
<div class="col-sm-8 radio"> | |
<label> | |
<input type="radio" name="gender" id="laki-laki" value="L" <?php echo $mahasiswa->gender=='L'?'checked':'' ?>> Laki-Laki | |
</label> | |
<label> | |
<input type="radio" name="gender" id="perempuan" value="P" <?php echo $mahasiswa->gender=='P'?'checked':'' ?>> Perempuan | |
</label> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="jurusan">Program Studi</label> | |
</div> | |
<div class="col-sm-8"> | |
<select name="jurusan" id="jurusan" class="form-control"> | |
<option value="TI" <?php echo $mahasiswa->jurusan=='TI'?'selected':'' ?> >Teknik Informatika</option> | |
<option value="TT" <?php echo $mahasiswa->jurusan=='TM'?'selected':'' ?> >Teknik Telekomunikasi</option> | |
<option value="TM" <?php echo $mahasiswa->jurusan=='TT'?'selected':'' ?> >Teknik Mekatronika</option> | |
<option value="TAB" <?php echo $mahasiswa->jurusan=='TAB'?'selected':'' ?> >Teknik Alat Berat</option> | |
</select> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="email">E-mail</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="email" name="email" id="email" class="form-control" placeholder="Alamat E-mail Mahasiswa" value="<?php echo $mahasiswa->email ?>" > | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="foto">Foto</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="file" name="foto" id="foto" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-9 col-sm-offset-3"> | |
<button type="submit" class="btn btn-primary" name="submit">Submit</button> | |
</div> | |
</div> | |
</form> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Pendaftaran Mahasiswa</title> | |
<link rel="stylesheet" type="text/css" href="<?php echo base_url() ?>css/bootstrap.min.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Input Data Mahasiswa</h1><hr> | |
<?php if (isset($error)): ?> | |
<div class="alert alert-danger"><?php echo $error ?></div> | |
<?php endif ?> | |
<form class="form-horizontal" action="<?php echo base_url()?>index.php/mahasiswa/create" method="POST" enctype="multipart/form-data"> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="nim">Nomor Induk Mahasiswa</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="text" name="nim" id="nim" class="form-control" placeholder="Nomor Induk Mahasiswa" > | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="nama">Nama Lengkap</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="text" name="nama" id="nama" class="form-control" placeholder="Nama Lengkap Mahasiswa" > | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="tmp_lahir">Tempat Lahir</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="text" name="tmp_lahir" id="tmp_lahir" class="form-control" placeholder="Tempat Lahir Mahasiswa" > | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="tgl_lahir">Tanggal Lahir</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="date" name="tgl_lahir" id="tgl_lahir" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="alamat">Alamat</label> | |
</div> | |
<div class="col-sm-8"> | |
<textarea name="alamat" id="alamat" cols="30" rows="10" class="form-control"></textarea> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="gender">Jenis Kelamin</label> | |
</div> | |
<div class="col-sm-8 radio"> | |
<label> | |
<input type="radio" name="gender" id="laki-laki" value="L"> Laki-Laki | |
</label> | |
<label> | |
<input type="radio" name="gender" id="perempuan" value="P"> Perempuan | |
</label> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="jurusan">Program Studi</label> | |
</div> | |
<div class="col-sm-8"> | |
<select name="jurusan" id="jurusan" class="form-control"> | |
<option value="TI" selected="selected">Teknik Informatika</option> | |
<option value="TT">Teknik Telekomunikasi</option> | |
<option value="TM">Teknik Mekatronika</option> | |
<option value="TAB">Teknik Alat Berat</option> | |
</select> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="email">E-mail</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="email" name="email" id="email" class="form-control" placeholder="Alamat E-mail Mahasiswa" > | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-3"> | |
<label for="foto">Foto</label> | |
</div> | |
<div class="col-sm-8"> | |
<input type="file" name="foto" id="foto" class="form-control"> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-sm-9 col-sm-offset-3"> | |
<button type="submit" class="btn btn-primary" name="submit">Submit</button> | |
</div> | |
</div> | |
</form> | |
<script src="<?php echo base_url() ?>js/bootstrap.min.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* class controller mahasiswa | |
*/ | |
class Mahasiswa extends CI_Controller | |
{ | |
function __construct() | |
{ | |
parent::__construct(); | |
$this->load->helper('url'); | |
$this->load->database(); | |
$this->load->library('session'); | |
} | |
public function index(){ | |
$this->load->view('mahasiswa/index'); | |
} | |
public function create(){ | |
$nim = $this->input->post('nim'); | |
$nama = $this->input->post('nama'); | |
$tmp_lahir = $this->input->post('tmp_lahir'); | |
$tgl_lahir = $this->input->post('tgl_lahir'); | |
$alamat = $this->input->post('alamat'); | |
$gender = $this->input->post('gender'); | |
$jurusan = $this->input->post('jurusan'); | |
$email = $this->input->post('email'); | |
$data = array( | |
'nim' => $nim, | |
'nama' => $nama, | |
'tmp_lahir' => $tmp_lahir, | |
'tgl_lahir' => $tgl_lahir, | |
'alamat' => $alamat, | |
'gender' => $gender, | |
'jurusan' => $jurusan, | |
'email' => $email, | |
); | |
$config['upload_path'] = './uploads/'; | |
$config['allowed_types'] = 'gif|jpg|png'; | |
$config['max_size'] = 100; | |
$config['max_width'] = 1024; | |
$config['max_height'] = 768; | |
$config['file_name'] = $nim; | |
$this->load->library('upload', $config); | |
if ( ! $this->upload->do_upload('foto')) | |
{ | |
$error = array('error' => $this->upload->display_errors()); | |
$this->load->view('mahasiswa/index', $error); | |
} | |
else | |
{ | |
$upload_data = $this->upload->data(); | |
$file_name = $upload_data['file_name']; | |
$data['foto']='uploads/'.$file_name; | |
if ($this->db->insert('mahasiswa', $data)) { | |
$this->session->set_flashdata('message','Data telah disimpan'); | |
redirect('mahasiswa/show'); | |
} | |
} | |
} | |
public function show(){ | |
$query = $this->db->get('mahasiswa'); | |
$this->load->library('table'); | |
$template = array( | |
'table_open'=> '<table class="table">', | |
); | |
$this->table->set_template($template); | |
$this->table->set_heading('Nomor Induk', 'Nama', 'Tempat Lahir','Tanggal Lahir','Alamat','Gender','Jurusan','Email','Action'); | |
foreach ($query->result() as $row) { | |
$links = anchor('mahasiswa/edit/'.$row->nim ,'Edit',array('class'=>'btn btn-sm btn-info')); | |
$links .= ' '; | |
$links .= anchor('mahasiswa/delete/'.$row->nim ,'Delete',array('class'=>'btn btn-sm btn-danger')); | |
$gender = ($row->gender=='L'? 'Laki-laki':'Perempuan'); | |
$jurusan = array( | |
'TI' =>'Teknik Informatika' , | |
'TM'=>'Teknik Mekatronika', | |
'TT'=>'Teknik Telekomunikasi', | |
'TAB'=>'Teknik Alat Berat' | |
); | |
$foto = "<img src='".base_url().$row->foto."' >"; | |
$this->table->add_row( | |
$row->nim, | |
$row->nama, | |
$row->tmp_lahir, | |
$row->tgl_lahir, | |
$row->alamat, | |
$gender, | |
$jurusan[$row->jurusan], | |
$row->email, | |
$foto, | |
$links | |
); | |
} | |
$data['title']='Data Pendaftaran Mahasiswa'; | |
$data['mahasiswa'] = $this->table->generate(); | |
$this->load->view('template/header',$data); | |
$this->load->view('template/content',$data); | |
$this->load->view('template/footer'); | |
} | |
public function edit($nim){ | |
$query = $this->db->get_where('mahasiswa', array('nim' => $nim)); | |
$data['title']='Edit Data Mahasiswa'; | |
$data['mahasiswa'] = $query->row(); | |
$this->load->view('template/header',$data); | |
$this->load->view('mahasiswa/edit',$data); | |
$this->load->view('template/footer'); | |
} | |
public function update(){ | |
$nim = $this->input->post('nim'); | |
$nama = $this->input->post('nama'); | |
$tmp_lahir = $this->input->post('tmp_lahir'); | |
$tgl_lahir = $this->input->post('tgl_lahir'); | |
$alamat = $this->input->post('alamat'); | |
$gender = $this->input->post('gender'); | |
$jurusan = $this->input->post('jurusan'); | |
$email = $this->input->post('email'); | |
$data = array( | |
'nim' => $nim, | |
'nama' => $nama, | |
'tmp_lahir' => $tmp_lahir, | |
'tgl_lahir' => $tgl_lahir, | |
'alamat' => $alamat, | |
'gender' => $gender, | |
'jurusan' => $jurusan, | |
'email' => $email, | |
); | |
$this->db->where('nim',$nim); | |
if ($this->db->update('mahasiswa', $data)) { | |
$this->session->set_flashdata('message','Data telah disimpan'); | |
redirect('mahasiswa/show'); | |
} | |
} | |
public function delete($nim){ | |
$this->db->where('nim',$nim); | |
if ($this->db->delete('mahasiswa')) { | |
$this->session->set_flashdata('message','Data telah dihapus'); | |
redirect('mahasiswa/show'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment