BLOG DALAM KEADAAN SEDANG DI REPARASI||MOHON MA`AF APABILA MASIH BANYAK KEKURANGAN||TERIMAKASIH TELAH BERKUNJUNG

CARA MEMBUAT CRUD CODEINGNITER DENGAN MUDAH, SCRIP MEMBUAT CRUD CODEINGNITER

CARA MEMBUAT CRUD CODEINGNITER DENGAN MUDAH, SCRIP MEMBUAT CRUD CODEINGNITER - Gak perlu lama-lama harus membuat pembukaan yang bagus dan mudah dibaca oleh pengunjung, langsu aja nieh ane opas dari blog tetangga CARA MEMBUAT CRUD CODEINGNITER DENGAN MUDAH, SCRIP MEMBUAT CRUD CODEINGNITER semoga bisa bermanfaat untuk kita semua. http://misdasari.blogspot.com

Berikut ini adalah Lampiran Script untuk membuat Sebuah Crud,,
Constroler:

 <?php
Class Tutorial extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('back/model_tutor');
$this->load->helper('download');
}
function index()
{

if($this->session->userdata('status')=='admin'){

$data['data']=$this->model_tutor->tampilData();
if($nis = $this->session->userdata('nis'))
$data['nama']=$this->model_tutor->nama($nis);
$data['links']=$this->pagination->create_links();
$this->load->view('back/header',$data);
$this->load->view('back/v_tutorial',$data);
}
else {
redirect('login');

}}
function download()
{
$pdf = $this->uri->segment(3);
$data = file_get_contents("file/".$pdf);
$dl=force_download($pdf,$data);
echo $dl;
}
function form_tambah()
{
if($nis = $this->session->userdata('nis'))
$data['nama']=$this->model_tutor->nama($nis);
$this->load->view('back/header',$data);
$this->load->view('back/form_tutorial');
}
function hapus()
{
$sql=$this->model_tutor->hapus();
if($sql){
redirect ('back/tutorial');
}

}

function edit()
{

if($nis = $this->session->userdata('nis'))
$data['nama']=$this->model_tutor->nama($nis);
$id=$this->uri->segment(4);
$data['data']=$this->model_tutor->per_id($id);
$this->load->view('back/header',$data);
$this->load->view('back/edit_tutorial',$data);
}
function update()
{
$id_user=$this->input->post('idtutor');
$data=array(
'namatutor'=>$this->input->post('namatutor'),
'tutorial'=>$this->input->post('tutorial'),
);
$this->model_tutor->update($id_user,$data);
redirect('back/tutorial');
}

}



Pada Model:

<?php
Class Model_tutor extends ci_model
{
function tampilData()
{
$config["base_url"] = base_url() . "back/tutorial/index";
$config["total_rows"] = $this->db->count_all('tutorial');
$config["per_page"] =5;
$config['prev_link']='<< Prevv';
$config['next_link']='Next >>';
$config["uri_segment"] = 4;
$offset=$this->uri->segment(4);
if(empty($offset)){
$offset=0;
}
$page=$config["per_page"];
$this->pagination->initialize($config);
$query=$this->db->get('tutorial',$page,$offset);
if($query->num_rows()>0)
{
return $query->result();
}
else
{
return array();
}
}

function tambah($data)
{
$tambah=$this->db->insert('tutorial',$data);
return $tambah;
}

function caridata(){
$c = $this->input->POST('cari');
$b = $this->input->POST('Angka');
if($b == "1")
$this->db->like('namatutor',$c);
$query = $this->db->get('tutorial');
return $query->result();
}

  public function record_count()
{
return $this->db->count_all("tutorial");
}
public function fetch_tutorial($limit, $start)
{
$this->db->limit($limit, $start);
$query = $this->db->get("tutorial");
if ($query->num_rows() > 0) {
foreach ($query->result() as $row)
{
$data[] = $row;
}
return $data;
}
return false;
}
function nama($nis)
{

$sql = "SELECT * FROM user WHERE nis = ? ";
$query = $this->db->query($sql, array($nis));
if ($query->num_rows() > 0)
{
$row = $query->row($sql);
return $row->nama;
}
}
function hapus()
{
$id = $this->uri->segment(4);
$this->db->where('idtutor',$id);
$hapus=$this->db->delete('tutorial');
return $hapus;
}
   
function per_id($id)

{
$this->db->where('idtutor',$id);
$query=$this->db->get('tutorial');
return $query->result();
}

function update($id,$data)

{
$this->db->where('idtutor',$id);
$update=$this->db->update('tutorial',$data);
return $update;
}



}

Pada View (ini untuk View utama.a)
<div id="kiri"><h2>DAFTAR TUTORIAL</h2><label style='float:right;background:none;'><a href="<?php echo base_url();?>back/tutorial/form_tambah">Tambah</a></label>
<div id="isi">
<br>

<table width="750" bgcolor="#ccc" cellpadding="2" cellspacing="1" class="widget-small">
<tr bgcolor="#FFF" align="center">
<td><strong>Id .</strong></td>
<td><strong>Nama Tutorial</strong></td>
<td><strong>Tutorial</strong></td>
<td colspan='2'><strong>Aksi</strong></td></tr>
<?php
$i=1;
foreach($data as $row):?>

<tr bgcolor='#D6F3FF'>

<td><b><?php echo $i+$this->uri->segment(4);?></b></td>
<td><?php echo $row->namatutor;?></td>
<td><?php echo $row->tutorial;?></td>
<td><a href="tutorial/edit/<?php echo $row->idtutor;?>">Edit</a></td>

<td> <a href="<?php echo base_url();?>back/tutorial/hapus/<?php echo $row->idtutor;?>" onclick="return confirm('Apakah Benar Anda Akan Menghapus Ini??')">delete</a> </td> </tr>
<?php
$i++;
endforeach;?>
</table><br />



<table class="widget" align="center"><tr><td></td></tr></table>
</div>
</div>

<div id="tengah-footer"><p><center><?php echo $links; ?></p> <img src="<?php echo base_url();?>images/bg-kiri-bawah.png" /></div>
<div id="footer"><br /><br /><br />
</div>
</body>
</html>

View (ini View Edit)
<div id="kiri"><h2>EDIT TUTORIAL</h2>
<div id="isi">
<br>
<body>
<?php foreach ($data as $row):?>
<?php echo form_open_multipart('back/tutorial/update');?>
<div id="kanan">
        <p align="center" >
        <table width="70%" height="177" border="1"  cellpadding="0" cellspacing="0">
<tr>
<td height="42" colspan="11"  background="<?php echo base_url();?>./images/menu.jpg"><h2><h1>Edit Data TUTORIAL </h1></h2></td>
</tr>
<tr>
<td>ID Data</td>
<td><input type="hidden" name="idtutor" value="<?php echo $row ->idtutor ?>"><br></td></tr>
<tr>
<td>Nama Tutorial</td>
<td><input type="text" name="namatutor" value="<?php echo $row ->namatutor ?>"><br></td></tr>
<tr>
<td>tutorial</td>
<td><input type="text" name="tutorial" value="<?php echo $row ->tutorial ?>"><br></td></tr>
<tr>
<td><input type="Submit" value="Update"></td>
</tr>
</table>
<?php form_close();?>
<?php endforeach;?>
</body>
</html>
View (ini View tambah)
 <?php
if($this->input->post('simpan')){
$data = array(
'namatutor' => $this->input->post('namatutor')
);
if($_FILES['tutorial']){
$dir = "file/";
$file = time().$_FILES['tutorial']['name'];
$tmp = $_FILES['tutorial']['tmp_name'];
move_uploaded_file($tmp,$dir.$file);
$data['tutorial'] = $file; 
}
$this->db->insert('tutorial',$data);
redirect('back/tutorial');
}
?>
<form method='post' enctype='multipart/form-data'>
<table width="40%" border="1" align="center">
<hr>
<tr>
<td colspan="6" align="center">
<h1>Tambah Daftar TUTORIAL</h1></td></tr>
<tr><td>Nama Tutorial:</td>
<td><input type='text' cols='60' rows='10' name='namatutor' /></td></tr>
<tr><td>tutorial:</td>
<td><input type="file" name="tutorial" value="<?php echo set_value('tutorial'); ?>"><?php echo form_error('tutorial');?><br></td></tr>
<td></td></tr>
<td><input type="submit" name='simpan' value="Simpan"></td>
</pre></table>
</form>


Artikel Terkait:

0 komentar: