CodeIgniter Tutorial for Beginners

PHP has lot of framework and cms for rapid development. Today we are going to tell you one of most popular framework of PHP that can speed up your development. It follows MVC Pattern in which separating logic from presentation. Most frameworks use MVC (Model View Controller) pattern that help to build full featured web applications. We can make big project in codigniter like ERP, CRM without any complexity. It gives a very easy way to integrate third party tools as library/plugins.

Codeigniter lets you creatively focus on your project by minimizing the amount of code needed for a given task. We will understand it step by step from downloading to development as follows:

1. Download Codeigniter

Go to https://www.codeigniter.com/download. Download  latest version of codigniter and put it htdocs in case of xampp or www in case of wamp folder after unzipping. You can rename it whatever you want to give your project.

 

2. Setup CodeIgniter in Local Machine

When you open it on browser with following URL http://localhost/CodeIgniter-3.1.0/CodeIgniter-3.1.0/  will have following page as shown below. Here you can learn complete setup of CodeIgniter in WAMP Server

It’s a default view page when you download codigniter. We can modify it according to our requirement to develop fully featured web applications. Here we will perform all basic database operation with codigniter . We have MYSQL database. You can choose any database in which you feel comfortable.

3. You can change your database configuration options by going in database.php file.  To find database.php file go in application folder then will have config folder, which contains database. phpfile as shown below:

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'discussdesk',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

 

4. Create Database discussdesk from phpMyAdmin after that create table article by running below Mysql code in SQL query Section

CREATE TABLE IF NOT EXISTS `article` (
  `id` int(10) NOT NULL,
  `name` varchar(15) NOT NULL,
  `description` varchar(100) NOT NULL,
  `date` varchar(10) NOT NULL
)

After creating table make id field to primary and auto increment through phpMyAdmin.


5. Open your routes.php file to change default controller which is welcome. Set or rename it what you want to give default controller name. Location of routes file is same as database file.

6. Before starting further process, we should know about Model, View and Controller because it plays main role in project and without understanding of these you can’t work in codigniter or any framework. So We are describing below:

  • Model:  The Model Class contains your database query or represents your database structure.
  • View: View Page contains information which will be show to user.it can be web page or any other page.
  • Controller: Controller serve as intermediary between the Model and view.it is responsible for loading resource from Model to View or directly to View Page.

7. Now open controller folder which is in application folder and make a new php file, give it name what you want controller name. In my case controller name is home. Make sure your controller or file name should be same as shown below:

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

class Home extends CI_Controller {
	public function __construct()
	{
	   parent::__construct();
	    //helpers
	   $this->load->helper('url');
	}

	public function index()
	{
		$this->load->view(‘home_view');
	}

}
?>

Here I have created home.php file in controller and make a function index to show view then load home view php file in index function. In my case, home_view is view file of View Section.

8. Now we have no home_view file so we have to create home_view file in View and put these following code into home_view.php file.

Now we will see simple Mysql Operation. You can find Mysql CRUD Operation in codeigniter from here

For Insertion Record in Codeigniter

  • In home controller add insert record function with following code as shown below and load insert_article view file in insert_record controller function.
public function insert_record()
	{
		  $data = $this->input->post();
		if(!empty($data))
		  {
			  $this->home_model->insert_article($data);
		  }
		  $this->load->view('insert_article');	
	}
  • Create home_model  Model with following code to insert record into article table:
<?php
classHome_model extends CI_Model
	{
		
		public function __construct()
		{
			parent::__construct();
			$this->load->database();
		}
		
		public function insert_article($data)
		{
   $query = $this->db->insert('article', $data);
if($query=='1')
		   {
			echo "Record Inserted";
		   }			
	}
?>
  • Create insert_article file in view folder to show view for inserting article.

Fill Article name, description and date then it will go insert_record function of home control to insert new record.

For Showing Record in Codeigniter

Create show_records function in home controller to show all articles from article Table as shown below:

public function show_records()
	{
		$data['article'] = $this->home_model->show_articles();
		$this->load->view('show_article',$data);
	}
  • Create show_articles function to fetch all record from article table with select query :
public function show_article($id)
		{
			$query = $this->db->get_where('article', array('id' => $id));
			return $query->row_array();
		}

 

  • Create show_article.php file in view folder to show all articles as like this:
<table align="center" border='1'>
<tr>
<td>No.</td>
<td>Article Name</td>
<td>Article Descrption</td>
<td>Date</td>

</tr>
<?php $i =1; foreach($article as $article_data){ ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $article_data['name']; ?></td>
<td><?php echo $article_data['description']; ?></td>
<td><?php echo $article_data['date']; ?></td>

</tr>
<?php $i++; } ?>

</table>

For Updating  Record in Codeigniter

  • Createupdate_record function in home controller with following code to update article as shown below:
public function update_article($id)
	{
		$data = $this->input->post();
		if(!empty($data))
		{
		  $this->home_model->update_article($data,$id);
		}
		$data['article'] = $this->home_model->show_article($id);
		$this->load->view('update_article',$data);
	}
  • Create update_article function in home_model function to update article with following code as below:
public function update_article($data,$id)
		{
			$this->db->where('id', $id);
			$this->db->update('article', $data);
			echo "Record Updated";
		}
  • Create update_article.php view file to show all records which will be updated .A user will select and update one of them.

For Deleting  Record in Codeigniter

Createdelete_article function in home controller with following code to delete  article as shown below:

public function delete_record()
	{
		$data['article'] = $this->home_model->show_articles();
		$this->load->view('delete_records',$data);	
		
	}
	public function delete_article($id)
	{
	 $this->home_model->delete_article($id);
	redirect('home/delete_record');
	}
  • Add another function in Model with name delete_article with paremeter as shown below:
public function delete_article($id)
		{
			$this->db->where('id', $id);
			$this->db->delete('article');				
		}
  • Create View file in which user will select and delete article as shown below:
<table align="center" border='1'>
<tr>
<td>No.</td>
<td>Article Name</td>
<td>Article Descrption</td>
<td>Date</td>
<td>Action</td>

</tr>
<?php $i =1; foreach($article as $article_data){ ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $article_data['name']; ?></td>
<td><?php echo $article_data['description']; ?></td>
<td><?php echo $article_data['date']; ?></td>
<td><a href="<?php echo base_url(); ?>index.php/home/delete_article/<?php echo $article_data['id']; ?>">Delete </a></td>

</tr>
<?php $i++; } ?>

</table>

Now we have completed all basic database operation still have any confusion, feel free to ask us in comment section.

Share this Article on Social Media

All of my Scripts are ready to customized as per your requirement. Feel free to contact for script customization.

Contact me at discussdesk@gmail.com

"Note : It will be charged as per your customization requirement :)"

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments