Select data from database using CodeIgniter
CodeIgniter PHP MySQL

Select data from database using CodeIgniter

Mishel Shaji
Mishel Shaji

In this post, we’re going to learn how to select data from database using CodeIgniter.

Related posts:

You can download the Source code from below link and run on your Local server.

Files to refer:

  • cisite\application\views\examples\select.php
  • cisite\application\controllers\Example.php
  • cisite\application\models\ExSelect.php
  • cisite\application\config\routes.php

DOWNLOAD SOURCE CODE

Before continuing, create a database named cisite and a table named ExSelect.

idbookauthor
int
varchar(100)varchar(100)
CREATE TABLE cisite.ex_select ( id INT NOT NULL , book VARCHAR(100) NOT NULL , author VARCHAR(100) NOT NULL);
INSERT INTO cisite.ex_select VALUES(1, 'Wings of fire', 'Dr. APJ Abdul Kalam');
INSERT INTO cisite.ex_select VALUES(2, 'War and Peace', 'Tolstoy');
INSERT INTO cisite.ex_select VALUES(3, 'Jungle Book', 'R. Kippling');
INSERT INTO cisite.ex_select VALUES(4, 'Das Capital', 'Karl Marks');
INSERT INTO cisite.ex_select VALUES(5, 'My Experiments With Truth', 'M. K. Gandhi');

1) Create a view: select.php and paste the following code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>CI Select</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
</head>
<body>
<form action="selectdata" method="POST">
<input type="text" name="book" list="book" placeholder="Select a book">
<button>Find Author</button>

<datalist id="book">
<option value="Wings of fire">
<option value="War and Peace">
<option value="Jungle Book">
<option value="Das Capital">
<option value="My Experiments With Truth">
</datalist>
</form>
</body>
</html>

2) Create a controller: Examples.php and paste the following code:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Examples extends CI_Controller{

public function index($page="index")
{
switch ($page) {
case 'selectdata':
$this->SelectData();
return;
case 'select':
$this->load->view($page);
break;
default:
show_404();
break;
}
}

public function SelectData()
{
$this->load->model('ExSelect');
$book=$this->input->post('book');

echo $this->ExSelect->SelectData($book);
}
}
?>

3) Create a model: ExSelect.php and paste the following code:

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

class ExSelect extends CI_Model{

public function __construct()
{
parent::__construct();
$this->load->database();
}

public function SelectData($val)
{
$condition = "book='".$val."'";
$this->db->select('*');
$this->db->from('ex_select');
$this->db->where($condition);
$this->db->limit(1);
$res=$this->db->get();

if($res->num_rows()>=1)
{
foreach($res->result() as $row)
{
$result=$row->author;
return $result;
}
}
}
}

If you have not created a new routing, create one as shown below:

Add this to routes.php:

//Examples start
$route['examples'] = 'Examples';
$route['examples/(:any)'] = 'Examples/index/$1';
//Examples End