Well, after lots of playing around with my Multi-User database systems assignment in PHP, I have decided to try and have a go with CodeIgniter, a popular open source PHP framework.
The concept is very simple, a prewritten set of classes are available for you to use to make writing in PHP easier and quicker (supposedly!) it takes a bit of getting used to but once getting the hang of it, its a very powerful tool and there is a lot of help out there, for example today, I was trying to look up a value in a table when submitting a form, making sure that it doesn’t already exist and if it does, throw up an error message to the user, if its not there, continue the code. I natually had a Google and found the following prewritten class that I can just pop into the my CodeIgniter libraries folder and it’s readily available to user, find the code below (with thanks to: ScottNelle.com):
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Form_validation Class
*
* Extends Form_Validation library
*
* Adds one validation rule, "unique" and accepts a
* parameter, the name of the table and column that
* you are checking, specified in the forum table.column
*
* Note that this update should be used with the
* form_validation library introduced in CI 1.7.0
*/
class MY_Form_validation extends CI_Form_validation {
function My_Form_validation()
{
parent::CI_Form_validation();
}
// --------------------------------------------------------------------
/**
* Unique
*
* @access public
* @param string
* @param field
* @return bool
*/
function unique($str, $field)
{
$CI =& get_instance();
list($table, $column) = split("\.", $field, 2);
$CI->form_validation->set_message('unique', 'The %s that you requested is unavailable.');
$query = $CI->db->query("SELECT COUNT(*) dupe FROM $table WHERE $column = '$str'");
$row = $query->row();
return ($row->dupe > 0) ? FALSE : TRUE;
}
}
?> |
This is then uploaded to the /libraries folder in your system and then in the controller you simply use when needed, find a snippet of my registration.php code below:
$this->form_validation->set_rules('CustomerID', 'Username', 'required|min_length[8]|max_length[20]|unique[Customers.CustomerID]');
//Pay special attention to Customers.CustomerID this is basically TABLE.COLUMN that you wish to check
if ($this->form_validation->run() == FALSE)
{
$this->load->view('registration', $headerdata);
}
else
{
//Add data to the table
} |
There we go, my first go at CodeIgniter. I’m sure I’ll be posting more snippets shortly, With thanks to Ben Harris for suggesting CodeIgniter to me and giving me a fair amount of help with the code today.
Have fun!