CI Update Delete

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

How to Update and Delete Data from tblcategory

1. Create Model : cateModel.php


<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class CateModel extends CI_Model {


public function __Construct()
{
parent::__construct();
}
public function gettable(){
$query=$this->db->get('tblcategory');
return $query->result();
}
function getonerow($id){
$this->db->where('cate_id',$id);
$query=$this->db->get('tblcategory');
return $query->row();
}
}
2. Edit code in view to add list of category(table of category) : cate_added.php

<html>
<head><title>form add Category</title>
</head>
<body>

<form method="post" action="<?php echo site_url('Category/savedata');?>"


enctype="multipart/form-data">
<table border=0>
<tr><td>Brand</td>
<td><input type="text" name="txtname"></td></tr>
<tr><td>Icon </td>
<td><input type="file" name="img1"></td></tr>
<tr><td>Detail</td>
<td><input type="text" name="txtdec"></td></tr>
<tr><td colospan=2>
<input type="submit" name="submit" value="Add"></td></tr>
</table>
</form>
<table border=1>
<?php
foreach($this->m->gettable() as $row){
echo"<tr>
<td>$row->cate_id</td>
<td>$row->cate_name</td>
";
?>
<td><img src="../../uploads/<?php echo $row->img ; ?>" width=50>
<td><?php echo $row->dec ; ?></td>

<?php
echo"
<td><a href='".site_url('Category/edit/'.$row->cate_id)."'>Edit</a> |
<a href='".site_url('Category/delete/'.$row-
>cate_id)."'>Delete</a>
</td>
";
}
echo"</tr>";
?>
</table>

</body>
</html>
3. Add function in controller : category.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends CI_Controller {


public function __Construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('CateModel', 'm');
}

public function index()


{
$this->load->view('cate_added', array('error' => ' ' ));
}
public function savedata()
{
$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'] = $_FILES['img1']['name'];

$this->load->library('upload',$config);
$this->upload->initialize($config);

$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
if (!$this->upload->do_upload('img1'))
{
$error = array('error' => $this->upload->display_errors());

$this->load->view('cate_added', $error);
}
else
{
$data=array(
'cate_name'=>$this->input->post('txtname'),
'img'=>$picture,
'dec'=>$this->input->post('txtdec')
);
$this->db->insert('tblcategory',$data);
redirect('Category/index');
}
}

function edit($id){
$row=$this->m->getonerow($id);
$data['r']=$row;
$this->load->view("cate_edit",$data);

function update(){
$id=$this->input->post('id');
$name=$this->input->post('txtname');
$dec=$this->input->post('txtdec');

$config['file_name'] = $_FILES['img1']['name'];

$this->load->library('upload',$config);
$this->upload->initialize($config);

$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
$data = array(
'cate_name'=>$name,
'img'=>$picture,
'dec'=>$dec
);
$this->db->where('cate_id',$id);
$this->db->update('tblcategory',$data);
redirect("Category/index");
}

function delete($id){
$id=$this->db->where('cate_id',$id);
$this->db->delete('tblcategory');
redirect("Category/index");

}
4. Go to view folder create file’s name cate_edit.php
<html>
<head><title>form add Category</title>
</head>
<body>

<form method="post" action="<?php echo site_url('Category/update');?>"


enctype="multipart/form-data">
<table border=0>
<tr><td>id</td>
<td><input type="text" name="id" value="<?php echo $r-
>cate_id; ?>"></td></tr>
<tr><td>Brand</td>
<td><input type="text" name="txtname" value="<?php echo $r-
>cate_name; ?>"></td></tr>
<tr><td>Icon </td>
<td><input type="file" name="img1"></td></tr>
<tr><td>Detail</td>
<td><input type="text" name="txtdec" value="<?php echo $r-
>dec; ?>"></td></tr>
<tr><td colospan=2>
<input type="submit" name="submit" value="update"></td></tr>
</table>
</form>
<table border=1>
<?php
foreach($this->m->gettable() as $row){
echo"<tr>
<td>$row->cate_id</td>
<td>$row->cate_name</td>
";
?>
<td><img src="../../uploads/<?php echo $row->img ; ?>" width=50>
<td><?php echo $row->dec ; ?></td>

<?php
echo"
<td><a href='".site_url('Category/edit/'.$row->cate_id)."'>Edit</a> |
<a href='".site_url('Category/delete/'.$row-
>cate_id)."'>Delete</a>
</td>
";
}
echo"</tr>";
?>
</table>

</body>
</html>

You might also like