Learn New Things in Digital Marketing Techniques

Complete basic insert, view, edit, delete and update in Codeigniter

(1)first go to  application/config folder   & open  database.php  file  and  make changes your connection values as given below  guidance 


and change your connection values:

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


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


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


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


$db['default']['dbdriver'] = 'mysql';


(2)create table 'items':

create table  items
(name varchar(200));


(3)write code for model class & save it as  EmployeeModel.php under "Model "   folder:


<?php

class EmployeeModel extends CI_Model  {



function insertEmployee($employee){
$this->db->insert('items', $employee); // insert data into `trn_employee`
//table
}
}
?>

(4)write code for controller class & save it as  Employee.php under "controllers"   folder :

 <?php

class Employee extends CI_controller {



function index()

{



$this->load->database();



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

// create data
$employee = array(

'name' =>"Sunil",

);

// table column name should be same as data object key name

$this->EmployeeModel->insertEmployee($employee); // call the employee model


}


}


?>

(1)create table  'tbl_name':

create table tbl_name

(id int(5) primary key auto_increment,

f1   varchar(200),

f2  varchar(200),

f3  varchar(200));


(2) write code for   'form.php' save it into views folder of codeigniter:


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

Field 1 <input type = ‘text’  name='f1'><br>

Field 2 <input type = ‘text’  name='f2'><br>

Field 3 <input type = ‘text’  name='f3'><br>

<input type="submit"  name="submit"  value="submit">

</form>


(3)write code for  'site_model.php'  save it into models folder of codeigniter:

<?php

class site_model extends CI_Model

{


function __construct()
        {
            parent::__construct();
        }

public function insertdata($data)

{

$this->load->database();



$this->db->insert('tbl_name', $data);

}

}

?>

(4)write code for 'site.php'  save it into  models folder of   controllers:


<?php

class site extends CI_Controller

{

function __construct()

{

parent::__construct();

$db = $this->load->database();

}
function index()

{

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

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




}

function insert()

{




$this->load->database();

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




                     
                     
                     
                     
                       $data = array(
         
           'f1' => $this->input->post('f1'),
           'f2' => $this->input->post('f2'),
           'f3' => $this->input->post('f3')
         
        );
                     
$this->site_model->insertdata($data);


}

}


(5)then opend browser  & type    'http://localhost/ci/index.php/site'

(where ci  is  codeigniter )

=================================================================================
PLZ  NOTE:
=================================================================================
if  error of  about/blank page come then  just go to application/config  folder open  config.php file & type  following  :  

How to set base_url()??
In config/config.php set $config['base_url'] = ''; the project URL.
here your project url is  "http://localhost/ci/index.php "

hence 

$config['base_url'] = 'http://localhost/ci/index.php';

at place of    $config['base_url']=' ';


and change your form action code as given below:

<form action='<?php echo base_url()?>site/insert'  method=post>

Field 1 <input type = ‘text’  name='f1'><br>

Field 2 <input type = ‘text’  name='f2'><br>

Field 3 <input type = ‘text’  name='f3'><br>

<input type="submit"  name="submit"  value="submit">

</form>




=================================================================================


======================================================



(1)create table  'tbl_name':

create table tbl_name

(id int(5) primary key auto_increment,

f1   varchar(200),

f2  varchar(200),

f3  varchar(200));


(2) write code for   'upform.php' save it into views folder of codeigniter:


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

Field 1 <input type = ‘text’ name='f1'><br>

Field 2 <input type = ‘text’ name='f2'><br>

Field 3 <input type = ‘text’ name='f3'><br>

<input type="submit"  name="submit"  value="submit">

</form>


(3)write code for  'update_model.php'  save it into models folder of codeigniter:

<?php

class update_model extends CI_Model

{


function __construct()
        {
            parent::__construct();
        }

     
public function update($data) {
  $f1 = $data['f1'];
  unset($data['f1']);
  $this->db->where('f1', $f1);
  $this->db->update('tbl_name' ,$data);
  return true;
}

}

?>

(4)write code for 'up.php'  save it into  models folder of   controllers:


<?php

class up extends CI_Controller

{

function __construct()

{

parent::__construct();

$db = $this->load->database();

}
function index()

{

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

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




}

function upd()

{




$this->load->database();

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




                   
                   
                   
                   
                       $data = array(
        
           'f1' => $this->input->post('f1'),
           'f2' => $this->input->post('f2'),
           'f3' => $this->input->post('f3')
        
        );
                   
$this->update_model->update($data);



}

}

(5)Then  open browser   &  type    'http://localhost/ci/index.php/up'


(where ci  is   codeigniter )

=================================================================================
PLZ  NOTE:
=================================================================================
if  error of  about/blank page come then  just go to application/config  folder open  config.php file & type  following  :  

How to set base_url()??
In config/config.php set $config['base_url'] = ''; the project URL.
here your project url is  "http://localhost/ci/index.php "

hence 

$config['base_url'] = 'http://localhost/ci/index.php';

at place of    $config['base_url']=' ';


and change form code as given below:

<form action=' <?php echo base_url()?>up/upd '   method=post>

Field 1 <input type = ‘text’ name='f1'><br>

Field 2 <input type = ‘text’ name='f2'><br>

Field 3 <input type = ‘text’ name='f3'><br>

<input type="submit"  name="submit"  value="submit">

</form>


========================================================================
 How to display values  from   a  table :
========================================================================
(1)write code for tableview.php  file save it under  view folder :

<?php

foreach($query as $row)
{
 
  print "f1 value is =". $row->f1;
  echo '<br>';
  print "f2 value is =".$row->f2;
  echo '<br>';
  print "f3 value is =". $row->f3;
 
 print "<br>";
 
}

?>
(2) write   code for   display.php file  and save under  model folder:
<?php
class display extends CI_Model
{

  function getall()
  {
       $this->load->database();
       $query=$this->db->get('tbl_name');//employee is a table in the database
       return $query->result();
  }
}
?>
(3)write  code for  table.php file and save under   controller folder :
<?php

class table extends CI_Controller

{

 
  function All()
 
  {
 
       $this->load->model('display');
                  
                   $data['query']=$this->display->getall();
       $this->load->view('tableview',$data);
                  
 
  }

  }

?>
========================================================================
How to   delete   record from  table  :
========================================================================

(1)first    write code for   deleteform.php file   and save under  view folder:

<form action='<?php echo base_url()?>delete/del' method=post>

Field 1 <input type = ‘text’ name='f1'><br>

Field 2 <input type = ‘text’ name='f2'><br>

Field 3 <input type = ‘text’ name='f3'><br>

<input type="submit"   name="submit"  value="submit">

</form>

(2)write   code  for  delete_model.php  file :

<?php

class delete_model extends CI_Model

{


function __construct()
        {
            parent::__construct();
        }

    
public function deleterecord($data) {
  $f1 = $data['f1'];
  unset($data['f1']);
  $this->db->where('f1', $f1);
 //$this->db->update('tbl_name' ,$data);
  $this->db->delete('tbl_name');
  return true;
 
 
 
 
}

}

?>

(3) now  write code for  delete.php file  and save under  controller folder :

<?php

class delete extends CI_Controller

{

function __construct()

{

parent::__construct();

$db = $this->load->database();

}
function index()
{

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

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

}

function del()

{
$this->load->database();

$this->load->model('delete_model');
$data = array(
       
           'f1' => $this->input->post('f1'),
           'f2' => $this->input->post('f2'),
           'f3' => $this->input->post('f3')
       
        );
                   
$this->delete_model->deleterecord($data);



}

}

?>

Previous
Next Post »