Codeigniter - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Basics

CodeIgniter Basics

shape Description

Follow the below steps to create CodeIgniter’s sample program and know CodeIgniter Basics.

shape Step-1

Firstly, under application/controllers folder, create a controller namely "mycontroller.php". Write the following code: [php] <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mycontroller extends CI_Controller { public function __construct() { parent::__construct(); } function index() //index is the default method { $this->load->view('myview');//Loading view page namely myview.php } } ?> [/php]

shape Step-2

Under application/views folder, create a view namely "myview.php". Write the following code: [php] <?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <html> <body> <div id="container"> <h1>Hello!</h1> <div id="body"> <p>Welcome to Splessons</p> <img class="image_center" src="<?php echo base_url('images/download.png')?>"/> </div> </div> </body> </html> [/php]

shape Step-3

Changing the default controller has been showed in Codeigniter Configuration chapter earlier. So, as an extension, place Learn More tag on the top right corner and right click on the link. Thus the page calls the index method(function) within mycontroller class and gets redirected to myview page.

shape Step-4

Test the application now by typing http://localhost/codeigniter/index.php/mycontroller. The following page appears as a result. (excluding styles)

shape Step-5

To make name dynamic i.e retrieving from database, then add following code in mymodel.php under application/models folder. [php] <?php defined('BASEPATH') OR exit('No direct script access allowed'); class Mymodel extends CI_Controller { public function __construct() { parent::__construct(); } //Retrieve name from database function get_names() { $this->db->select('name'); $this->db->from('names'); $res=$this->db->get()->row_array(); return $res; } } [/php] Then replace index() method in mycontroller with the below code and check for output. [php] function index() //index is the default method { $this->load->model('mymodel');//Load model $data['names']=$this->mymodel->get_names();//get names from database and store in a variable $this->load->view('myview',$data);//Passing Data to view and Loading view page namely myview.php } [/php]

Summary

shape Key Points

The CodeIgniter Basics chapter draws out following important points.
  • CodeIgniter Controller updates the Views page.
  • Index function acts as the intermediate.

shape Programming Tips

  1. Check whether Xampp is ON or not.
  2. If Xampp is unable to start, quit out the skype first if it is ON, later on close any other applications that are ON and make ports free so that xampp uses those ports and gets started. Once Xampp started those applications can be reopened.
  3. Avoid CamelCased, Uppercased, Hyphened, Space Separated Controller, Model, View names and database table names. Eg: MYCONTROLLER(Upper cased), MyController(Camel Cased), my-controller(Hyphened), my controller(spaced).
Use my_controller(If needed to join multiple names) instead which might leads to errors when moved to live because local servers use windows OS(with Case insensitive feature) but live servers use Linux OS(Case sensitive feature).
  • Check whether base_url is correctly set or not.
  • check default_controller is correctly set or not.
  • Always the first letter of controller and model name should be capitalized only in the line.Class My_controller extends CI_controller unless codeigniter is of latest version.
  • Without above line also codeigniter works but to add these lines is always advisable which tells the browser that direct access of script is not allowed unless BASEPATH is followed. This line checks whether data flow is through the front controller(index.php) or not, following the codeigniter architecture instead of simply directory structure, i.e. Trying to access the page(following directory architecture) using http://localhost/codeigniter/mycontroller(file name)/mycontroller(class name)/index(method name) throws error.So, http:/localhost/codeigniter/index.php/mycontoller/mycontroller/index gives correct result. If controller file name and controller class name are same then only can be used once like http:/localhost/codeigniter/index.php/mycontroller/index.