CodeIgniter Tutorial: How to display data from database using CodeIgniter

This video learn how to display data from database using CodeIgniter with XAMPP



1- Go codeigniter.com to download source

2- Go MyphpAdmin to create database name 'students'

3- Create table 'students'

Fields:
studentid varchar(255) Primary Key
firstname varchar(255)
lastname  varchar(255)
sex varchar(255)

4- Database Configuration in CodeIgniter

5- Update config.php in application/config

$config['base_url'] = 'http://localhost/student/';
$config['index_page'] = '';

6- Update autoload.php in application/config

$autoload['libraries'] = array('database');

7- Create students.php (Save to application/controllers)

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

class Students extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model('Students_model');
}

public function index()
{
$this->data['students'] = $this->Students_model->get_all();
$this->load->view('students', $this->data);
}

}
?>

8- Create Students_model (Save to application/models)

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

class Students_model extends CI_Model {

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

function get_all() {
$this->db->select('studentid, firstname, lastname, sex');
$query = $this->db->get('students');
return $query->result_array();
}

}
?>

9- Create students.php (Save to application/views)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Students</title>
</head>
<body>
<div align="center">

<table border="1" cellpadding="2px" width="600px">
<tr>
<th>No.</th>
<th>First Name</th>
<th>Last Name</th>
<th>Sex</th>
</tr>
<?php
foreach ($students as $student){
 $studentid = $student['userid'];
 $firstname = $student['username'];
 $lastname = $student['password'];
 $sex = $student['sex'];
?>
    <tr>        
            <td>
            <?php echo $studentid; ?>
   </td>
            <td>
            <?php echo $firstname; ?>                          
   </td>
   <td>
            <?php echo $lastname; ?>                          
   </td>
   <td>
            <?php echo $sex; ?>                          
   </td>
</tr>        
        <?php } ?>
    </table>
</div>
</body>
</html>

10- Update routes.php in application/config

$route['default_controller'] = 'Students';
$route['student'] = "Students";

11- Create .htaccess (Save to web root)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /student/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

12- Demo
http://localhost/student/

Read more blog:
http://video-tutorial-from-youtube.blogspot.com/

Follow Us with Facebook
https://www.facebook.com/pages/PHP-Tutorial/782239958529506

Share this

Related Posts

Previous
Next Post »