Tutorial Crud Codeigniter With Mysql
Tutorial Crud Codeigniter With Mysql
Tutorial ini saya dedikasikan untuk yang baru terjun di framework codeigniter,
dan para pemula yang ingin belajar secara otodidak.
Crud merupakan kewajiban dasar yang harus di kuasai dalam pembuatan
website atau aplikasi berbasis web, CRUD sendiri didefinisikan menjadi Create,
Read, Update dan Delete. Baik sekarang kita buat Database terlebih dahulu
Buat Database dengan nama db_crud dan create table table_biodata dengan
struktur berikut:
3.
4.
$phone
$address
= $this->input->post('phone');
= $this->input->post('address');
$data = array(
'firstname'=> $firstname,
'lastname'=> $lastname,
'hobby'=> $hobby,
'phone'=> $phone,
'address'=> $address
);
$where = array('id' => $id);
$this->db->update('table_biodata',$data,$where);
}
function do_delete($id)
{
$where = array('id' => $id);
$query = $this->db->delete('table_biodata',$where);
return $query;
}
}
5.
Browse.php
<!DOCTYPE html>
<html>
<head>
<link href="<?php echo base_url() ?>bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<?php echo $this->load->view('alert');?>
<div class="container">
<ul class="breadcrumb wellwhite">
<li>CRUD<span class="divider"></span></li>
<li>Read </li>
</ul>
<h3>Tabel Biodata</h3>
<table class="table table-striped">
<thead>
<tr class="info">
<th>No</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Hobby</th>
<th>Address</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $no=1; foreach ($query->result() as $key): ?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $key->firstname ?></td>
<td><?php echo $key->lastname ?></td>
<td><?php echo $key->hobby ?></td>
<td><?php echo $key->address ?></td>
<td>
<a class="btn btn-warning" href="<?php echo base_url()?>biodata/edit/<?php echo $key->id ?>">
<span class="glyphicon glyphicon-pencil"></span> Edit
</a>
Form_add.php
<!DOCTYPE html>
<html ang-app="">
<head>
<link rel="stylesheet" href="<?php echo base_url()?>bootstrap/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<ul class="breadcrumb wellwhite">
<li>CRUD<span class="divider"></span></li>
<li>Insert </li>
</ul>
<form
class="form-horizontal"
role="form"
method="post"
base_url()?>biodata/insert">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 ">First Name</label>
action="<?php
<div class="col-sm-10">
<input type="text" class="form-control"
placeholder="FirstName" name="firstname"
required="required">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2">Last Name</label>
<div class="col-sm-10">
<input type="text" class="form-control"
placeholder="LastName" name="lastname"
required="required">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2">Hobby</label>
<div class="col-sm-10">
<input
type="text"
class="form-control"
placeholder="Hobby"
name="hobby"
required="required">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2">Phone</label>
<div class="col-sm-10">
<input
type="number"
class="form-control"
placeholder="Phone"
name="phone"
required="required">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 ">Address</label>
<div class="col-sm-10">
<textarea rows="8" class="form-control" placeholder="Address" name="address"> </textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<hr>
<button class="btn btn-success " >
<span class="glyphicon glyphicon-user"></span> Save
</button>
</div>
</div>
</form>
</html>
Form_edit.php
<!DOCTYPE html>
<html>
<head>
<link href="<?php echo base_url() ?>bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<ul class="breadcrumb wellwhite">
<li>CRUD<span class="divider"></span></li>
<li>Update </li>
</ul>
<?php foreach($query->result() as $key) : ?>
<form
class="form-horizontal"
role="form"
method="post"
action="<?php
echo
base_url()?>biodata/update">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 ">First Name</label>
<div class="col-sm-10">
<input type="hidden" class="form-control" value="<?php echo $key->id ?>" name="id" >
<input type="text" class="form-control"
value="<?php echo $key->firstname ?>"
name="firstname" >
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2">Last Name</label>
<div class="col-sm-10">
<input type="text" class="form-control"
value="<?php echo $key->lastname ?>"
name="lastname">
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2">Hobby</label>
<div class="col-sm-10">
<input type="text" class="form-control" value="<?php echo $key->hobby ?>" name="hobby">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2">Phone</label>
<div class="col-sm-10">
<input type="number" class="form-control" value="<?php echo $key->phone ?>" name="phone" >
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 ">Address</label>
<div class="col-sm-10">
<textarea rows="8" class="form-control" name="address"><?php echo $key->address
?></textarea>
</div>
</div>
<?php endforeach; ?>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<hr>
<button class="btn btn-success " >
<span class="glyphicon glyphicon-user"></span> Update
</button>
</div>
</div>
</form>
</html>