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:
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.
When you open it on browser with following URL https://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:
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
public function insert_record()
{
$data = $this->input->post();
if(!empty($data))
{
$this->home_model->insert_article($data);
}
$this->load->view('insert_article');
}
<?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";
}
}
?>
Fill Article name, description and date then it will go insert_record function of home control to insert new record.
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);
}
public function show_article($id)
{
$query = $this->db->get_where('article', array('id' => $id));
return $query->row_array();
}
<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>
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);
}
public function update_article($data,$id)
{
$this->db->where('id', $id);
$this->db->update('article', $data);
echo "Record Updated";
}
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');
}
public function delete_article($id)
{
$this->db->where('id', $id);
$this->db->delete('article');
}
<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.