Thursday, May 29, 2014

Install and run PHP CodeIgniter under XAMPP in Windows 7



CodeIgniter is a powerful PHP framework 
based on the Model-View-Controller development pattern
Download page:
http://ellislab.com/codeigniter
Documentation page:
http://ellislab.com/codeigniter/user-guide/toc.html

1) Download CodeIgniter_2.1.4.zip and unzip to
C:\xampp\htdocs\xampp\codeigniter\
note I installed XAMPP under C:\xampp
2) Run
http://localhost/xampp/codeigniter/
welcome page should appear.
3) Create test.php under
application/controller
run
http://localhost/xampp/codeigniter/index.php/test
4) configure database\application\config\database.php
In mycase, conect to localhost test database with root and no password
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'test';

Example code:
5) controller test.php
C:\xampp\htdocs\xampp\codeigniter\application\controllers\test.php
<?php

class Test extends CI_Controller {
    public function index() {
        $data = array();
        $this->load->model('Users');
        $user = new Users();
        $user->setUsername("jiansen");
        $user->setPassword("pass1");
        $data['message']= $user->login();
        echo '<tt><pre>' . $data['message'] . '</pre></tt>';
       
        //$this->load->view('magazines');
        echo '<h2>This is a test</h2>';
        $this->load->view('test',$data);
    }
}

?>
6) model users.php
C:\xampp\htdocs\xampp\codeigniter\application\models\users.php
<?php
Class Users extends CI_Model
{

    private $_username;
    private $_password;

    function __construct()
    {
        parent::__construct();
    }
    public function setUsername($value)
    {
        $this->_username = $value;
    }
   
    public function setPassword($value)
    {
        $this->_password = $value;
    }
   public function login()
   {
        $this->load->database();
        $username = $this->_username;
        $password = $this->_password;
        $this -> db -> select('id, username, password');
        $this -> db -> from('users');
        $this -> db -> where('username', $username);
        $this -> db -> where('password', $password);
        $query = $this -> db -> get();
         if($query -> num_rows() == 1)
       {
           return "user name and pasword matched";
       }
       else
       {
         return "user name and pasword not matched";
       }
    }   
}
?>

7) view test.php
C:\xampp\htdocs\xampp\codeigniter\application\views\test.php
<?php
echo "from view ".$message;
?>

Video: Install and run PHP  CodeIgniter under XAMPP in Windows 7

No comments:

Post a Comment