Sample PHP code - implemented in CodeIgniter

This shows how my procedural PHP sample code can be easily implemented 
in CodeIgniter, a PHP framework. It is very basic and has only limited 
filtering of input. This means that input will be sent directly to the 
database for processing, as is, without any checking for invalid or 
malicious input, so it is not safe for a production environment.  

The basic architecture, in terms of folders and files, is described 
first, followed by a description of the various files.

The sample application assumes that the document root on the web server will 
have a folder called 'CodeIgniter', with the same contents that CodeIgniter 
will have right out of the box after you download and unzip it.

This folder has three subfolders. The only one we will be working in is the 
"application" subfolder. We will create two files in its "views" subfolder,
a third file in its "controllers" subfolder and a fourth file in its "models" 
subfolder. We will also need to customize the settings in the file 
"database.php" in subfolder "config".

Finally, we need to make sure that there is a database we can use in MySQL, 
and a table "Cars" and a user with a password and appropriate privileges.

When we are finished, we are going to point our browser at: 
http://localhost/CodeIgniter/index.php/form 

The logical place to start is with the main controller, which is a file 
called "form.php" in the controllers folder.

It looks like this:


<?php
// This is file 'CodeIgniter/application/controllers/form.php':

class Form extends CI_Controller {

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

  function index()
    {
     $this->load->helper(array('form', 'url'));
     $this->load->library('form_validation');
     $this->form_validation->set_rules('Make', 'make', 'required');
     $this->form_validation->set_rules('Model', 'model', 'required');
     $this->form_validation->set_rules('Year', 'year', 'required');

     if ($this->form_validation->run() == FALSE)
       {
	$this->load->view('myform');
       }
     else
       {
        $this->cars_model->set_cars();
        $data['cars'] = $this->cars_model->get_cars();
        $this->load->view('formsuccess', $data);
       }
    }
}

?>



<?php
// This is file 'CodeIgniter/application/models/cars_model.php':

class Cars_model extends CI_Model {

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

public function get_cars()
  {
   $query = $this->db->get('cars');
   return $query->result_array();
  }

public function set_cars()
  {
   $a = $this->input->post('Make');
   $b = $this->input->post('Model');
   $c = $this->input->post('Year');
   $sql = 'INSERT INTO `cars` (`Make`, `Model`, `Year`) ';
   $sql .= 'VALUES (\''.$a.'\', \''.$b.'\', \''.$c.'\');';
   $this->db->query($sql);
  }


}

?>
 

<?php
// This is file 'CodeIgniter/application/views/myform.php':
?>

<html>
<head>
<title>My Form</title>
</head>
<body>

<h1>Please enter the following information for your new car:</h1>

<?php echo validation_errors(); ?>

<?php echo form_open('form'); ?>

<h5>Make</h5>
<input type="text" name="Make" value="" value="" size="50" />

<h5>Model</h5>
<input type="text" name="Model" value="" size="50" />

<h5>Year</h5>
<input type="text" name="Year" value="" size="50" />

<div><input type="submit" value="Submit" /></div>

</form>

</body>
</html>
 

<?php
// This is file 'CodeIgniter/application/views/formsuccess.php':
?>

<html>
<head><title>My Form</title></head>
<body>

<?php

echo '<h1>Thank you</h1>
We have entered the following new record into our database: ';

foreach ($cars as $cars_item): 
   $lastitem = $cars_item['Make'].', '.$cars_item['Model'].', ';
   $lastitem .= $cars_item['Year'].'<br />'; 
endforeach;

echo $lastitem;

echo "<br />";
echo anchor('form', 'Enter another car!'); 

?>

</body>
</html>
 

You can create the database, table and user needed as follows, 
from the MySQL console:

use mysql;
-- show databases;
DROP DATABASE IF EXISTS Yourcars;
CREATE DATABASE Yourcars;

use Yourcars;
CREATE TABLE `cars` 
(
`id` int NOT NULL AUTO_INCREMENT
, PRIMARY KEY  (`id`)
, UNIQUE KEY `id` (`id`)
, `Make`  varchar(20) default NULL
, `Model` varchar(20) default NULL
, `Year`  int default NULL
, `Date & Time Entered` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP
);

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, 
ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON cars.* TO 
'youruser'@'yourhost' IDENTIFIED BY 'yourpassword';





Valid XHTML 1.0 Transitional Valid CSS!
 




MCP icon
MCTS icon