Skip to main content

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);



}

}

?>

Comments

Popular posts from this blog

Free Article Submission Websites:

EzineArticles.com Ezine Articles remains at the top of the pile by virtue of enjoying a PageRank of 6 from Google. The website has become a comprehensive source of articles available for your reading pleasure online. It is a great platform for marketers to post their content. The best thing about using Ezine Articles is that it is completely free to use. So, despite the high PageRank among article submission websites and the ability to add two text links per article, you don’t have to pay a dime for posting articles here. SearchWarp.com Search Warp is similar to Ezine Articles in the sense that it is also a free website for submitting articles. It also has a high PageRank, 5. The added advantage you gain by using Search Warp is adding 3 text links per page you upload to the directory. This makes it one of the top article submission websites you can use for free. eHow.com eHow has the highest PageRank among article submission sites, 8. When it comes to eHow, you need to pro

SEO Learning Notes

Advanced Internet Marketing Course 1. Introduction of SEO and Search Engines     (What are SEO, Benefits of SEO, and Features of SEO?) 2. Types of SEO     (How Many Types of SEO)     (Off Page and On Page)     (Off Page Description and On Page Description) 3. About Off Page      Directory      Article      Blog      Forum      Social Bookmark      Classified      Network Creation 4. About On Page      Meta Tag      Content      Anchor Text      Alt Tag      Error Checking and Solution      Broken link Checking and Solution 5. Competitor Analysis 6. Keywords Research & Analysis 7. Content Copywriting & Optimization 8. Sitemap Creation & Submission 9. RSS Creation & Submission 10. About Robots.txt 11. Static & Dynamic Websites 12. Website Planning & Structure 13. About SMO 14. About SEM 15. About PPC 16. Social Media & Network Marketing 17. Website Analyt

How To Start Increasing Your Facebook Page Likes

How To Start Increasing Your Facebook Page Likes 1. Set Up Your Page Timeline   : The first thing you need to do once you have created your Facebook page is to upload a proper profile picture and cover image on your page. Then go to the  About   section of your page and fill up all the details with relevant information so that people will be able to find your page easily. ·         Category:  Choose the category that best describes your page. ·         Page Name:  If you page has less than 200 likes, then you will be able to change the name of your page. ·         Topics : In this section, add up to 3 words that best describes your page. This will help people discover your page easily. ·         Facebook Web Address : It is self-explanatory. ·         Start Date:  You can specify the date you created your Facebook page. ·         Short Description:  This is a really important section. Consider this as the meta description of your Facebook page. Add a short but rele