6
Created by Vineet Kumar Saini www.vineetsaini.wordpress.com Add-Edit-Delete in Codeigniter in PHP First of all we create a database i.e. company CREATE DATABASE `company`; Now we will create a table i.e. registration CREATE TABLE `company`.`registration` ( `id` INT( 5 ) NOT NULL AUTO_INCREMENT , `name` VARCHAR( 100 ) NOT NULL , `email` VARCHAR( 100 ) NOT NULL , `address` VARCHAR( 255 ) NOT NULL , `phone` VARCHAR( 12 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB; Now set the database name in the database.php file in codeigiter Application folder -> config folder -> database.php $db['default']['hostname'] = 'localhost'; $db['default']['username'] = 'root'; $db['default']['password'] = ''; $db['default']['database'] = 'company'; Now we will create a controller i.e. emp.php in the controller folder <?php class emp extends CI_Controller { /* public function __construct() //php5 Constructor { parent::__construct(); $this->load->helper('url'); $this->load->model('emp_model'); } */ function emp() //php4 Constructor by class name { parent::CI_Controller(); $this->load->helper('url'); $this->load->model('emp_model'); } function GetAll()

Add edit delete in Codeigniter in PHP

Embed Size (px)

Citation preview

Page 1: Add edit delete in Codeigniter in PHP

Created by Vineet Kumar Saini

www.vineetsaini.wordpress.com

Add-Edit-Delete in Codeigniter in PHP

First of all we create a database i.e. company

CREATE DATABASE `company`;

Now we will create a table i.e. registration

CREATE TABLE `company`.`registration` (

`id` INT( 5 ) NOT NULL AUTO_INCREMENT ,

`name` VARCHAR( 100 ) NOT NULL ,

`email` VARCHAR( 100 ) NOT NULL ,

`address` VARCHAR( 255 ) NOT NULL ,

`phone` VARCHAR( 12 ) NOT NULL ,

PRIMARY KEY ( `id` ) ) ENGINE = InnoDB;

Now set the database name in the database.php file in codeigiter

Application folder -> config folder -> database.php

$db['default']['hostname'] = 'localhost';

$db['default']['username'] = 'root';

$db['default']['password'] = '';

$db['default']['database'] = 'company';

Now we will create a controller i.e. emp.php in the controller folder

<?php

class emp extends CI_Controller

{

/* public function __construct() //php5 Constructor

{

parent::__construct();

$this->load->helper('url');

$this->load->model('emp_model');

}

*/ function emp() //php4 Constructor by class name

{

parent::CI_Controller();

$this->load->helper('url');

$this->load->model('emp_model');

}

function GetAll()

Page 2: Add edit delete in Codeigniter in PHP

Created by Vineet Kumar Saini

www.vineetsaini.wordpress.com

{

$data['query']=$this->emp_model->emp_getall();

$this->load->view('emp_viewall',$data);

}

public function operation()

{

if(isset($_POST['btn']))

{

if(empty($_POST['id']))

{

$this->add_new_data();

}

else

{

$this->updating();

}

}

}

function add_new()

{

$this->load->view('form');

}

public function add_new_data()

{

$this->emp_model->add_data();

$this->GetAll();

}

public function update($id)

{

// $id=$this->input->get('id'); Get id from query string

$data['value']=$this->emp_model->get_data_id($id);

$this->load->view('form',$data);

}

public function updating()

{

$this->emp_model->update_data();

$this->GetAll();

}

public function delete($id)

{

//echo $id;exit;

$this->load->model('emp_model');

Page 3: Add edit delete in Codeigniter in PHP

Created by Vineet Kumar Saini

www.vineetsaini.wordpress.com

$delete=$this->emp_model->delete_data($id);

$this->GetAll();

}

}

?>

Now we create a model emp_model.php in the model folder

<?php

class Emp_model extends CI_Model

{

function Emp_model()

{

parent::CI_Model();

$this->load->database();

}

function emp_getall()

{

$query=$this->db->get('registration');

return $query->result();

}

function add_data()

{

$name=$_POST['name'];

$email=$_POST['email'];

$address=$_POST['address'];

$phone=$_POST['phone'];

$value=array('name'=>$name,'email'=>$email,'address'=>$address,'phone'=>$phone);

$this->db->insert('registration',$value);

//ECHO "Succesfully Inserted?";

}

function delete_data($id)

{

$delete = "delete from registration where id='$id'";

$this->db->query($delete);

}

/* OR

function delete_data($id)

{

$this->db->delete('registration',array('id'=>$id));

}*/

function get_data_id($id)

Page 4: Add edit delete in Codeigniter in PHP

Created by Vineet Kumar Saini

www.vineetsaini.wordpress.com

{

$query = $this->db->get_where('registration',array('id' => $id),1);

return $query;

}

function update_data()

{

$id=$_POST['id'];

$name=$_POST['name'];

$email=$_POST['email'];

$address=$_POST['address'];

$phone=$_POST['phone'];

$value= array('name'=>$name,'email'=>$email,'address'=>$address,'phone'=>$phone);

$this->db->update('registration',$value,array('id' => $id));

//ECHO "Succesfully Inserted?";

}

}

?>

Now we create a views i.e emp_viewall.php in the views folder

<center>

<h3>

<u>Display Data From Database Using Codeigniter in PHP</u></h3>

<table cellspacing="0" cellpadding="2" border="1" width="50%">

<tr>

<th>S.No</th>

<th>Name</th><th>Email</th><th>Address</th>

<th>phone</th><th>&nbsp;</th><th>&nbsp;</th>

</tr>

<?php

$i=1;

foreach($query as $row)

{

?>

<tr>

<td><?php echo $i; ?></td>

<td><?php echo $row->name; ?></td>

<td><?php echo $row->email; ?></td>

<td><?php echo $row->address; ?></td>

<td><?php echo $row->phone; ?></td>

<td><a href="<?php echo base_url().'/index.php/emp/update/'.$row->id;

Page 5: Add edit delete in Codeigniter in PHP

Created by Vineet Kumar Saini

www.vineetsaini.wordpress.com

>">Edit</a></td>

<td><a href="<?php echo base_url().'/index.php/emp/delete/'.$row->id;

?>">Delete</a></td>

</tr>

<?php

$i++;

}

?>

<tr><td colspan="7"><a href="<?php echo base_url();?>/index.php/emp/add_new">Add

New</a></td></tr>

</table>

</br>

</center>

Now we create another views i.e form.php for updating data in the views folder

<?php

$name='';

$email='';

$address='';

$phone='';

$id='';

$submit='Add User';

if(isset($value) && !empty($value))

{

foreach($value->result() as $row)

{

$name=$row->name;

$email=$row->email;

$address=$row->address;

$phone=$row->phone;

$id=$row->id;

$submit='Update User';

}

}

?>

<html>

<head></head>

<body>

<form name="ajaxform" id="ajaxform" action="<?php echo base_url().'/index.php/emp/operation'?>"

method="POST">

<table border="1">

Page 6: Add edit delete in Codeigniter in PHP

Created by Vineet Kumar Saini

www.vineetsaini.wordpress.com

<tr>

<th>Name</th>

<td><input type="text" name="name" value="<?php echo $name; ?>"/></td>

</tr>

<tr>

<th>Email </th>

<td><input type="text" name="email" value="<?php echo $email; ?>"/></td>

</tr>

<tr>

<th>Address</th>

<td><input type="text" name="address" value="<?php echo $address; ?>" /></td>

</tr>

<tr>

<th>Phone</th>

<td><input type="text" name="phone" value="<?php echo $phone; ?>" /></td>

</tr>

</table>

<input type="hidden" name="id" value="<?php echo $id; ?>" />

<input type="hidden" id="btn" value="<?php echo $submit; ?>" name="btn"/>

<input type="submit" id="simple-post" value="<?php echo $submit; ?>" name="simple-post"/>

</form>

</body>

</html>

Output

Run the code using following url in your browser

http://localhost/projectname/index.php/emp/GetAll

Thanks!!