Dont load image_lib multiple times. Add image_lib in autoload libs and change

$this->load->library(‘image_lib’, $config);
to

$this->image_lib->initialize($config);

Dont load image_lib multiple times. Add image_lib in autoload libs and change

$this->load->library(‘image_lib’, $config);
to

$this->image_lib->initialize($config);

How to validate multiple forms on one page in CodeIgniter?               

In this tutorial I will show how to create a controller and a view in CodeIgniter to render, validate and output forms, validation error messages and results for multiple forms on one page.

We will start from creating the controller.

The controller

On top of every CodeIgniter controller script file we need to put the following line

1
if (!defined('BASEPATH')) exit('No direct script access allowed');

which prevents from accessing the controller from the outside.

Now we’ll create a CodeIgniter controller script for rendering forms, validation and getting the validation results. As you probably know all controller scripts are placed in application/controller directory in you app’s root folder and each script has the same filename as the class inside.

More about this you can find [here; link for external CI site or other article].

Let’s create a class called “Forms” which will extends from the main CodeIgniter controller class (CI_Controller)

1
class Forms extends CI_Controller {

Then we have to create a class constructor to initialize parent class constructor first to we will have access to all CI_Controller methods then we load form helper which contains functions that assist in working with forms.

1
2
3
4
5
6
7
// Controller constructor
public function __construct()
{
    parent::__construct();
    // Load form helper required to validate forms
    $this->load->helper('form');     
}

We will write a method (function) for defining form elements and passing that data to the view so it can be rendered. Index function is executed by default when accessing controller. In index method we’ll define three forms. First one will go with username and key text input fields and the rest will go with just a username text input field. And the end of this function we take all the arguments and pass it on to our view which will render the forms.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Prepare data for the view to output the forms
public function index()
{
    // Defining form1 elements
    $data['form_1'] = array('class' => 'main_form', 'id' => 'form_1');
    $data['form_1']['username'] = array('name' => 'username', 'value' => 'Default username');
    $data['form_1']['key'] = array('name' => 'key', 'value' => 'Key');
    $data['form_1']['submit'] = array('value' => 'Submit!', 'type' => 'submit');
 
    // Defining form2 elements
    $data['form_2'] = array('class' => 'main_form', 'id' => 'form_2');
    $data['form_2']['username'] = array('name' => 'username', 'value' => 'Default username');
    $data['form_2']['submit'] = array('value' => 'Submit!', 'type' => 'submit');
 
    // Defining form3 elements
    $data['form_3'] = array('class' => 'main_form', 'id' => 'form_3');
    $data['form_3']['username'] = array('name' => 'username', 'value' => 'Default username');
    $data['form_3']['submit'] = array('value' => 'Submit!', 'type' => 'submit');     
    // Load the forms view and pass data to the view
    $this->load->view('forms', $data);       
}

New we’ll create a function that will assign validation rules to each of form elements and return the validation status.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Form validation function
private function validate_form($validation_rules, $callback=NULL)
{
    $array_index = 0;  
    // Load the form validation CodeIgniter library
    $this->load->library('form_validation');                     
    // Loop through validation rules and set it up for each element
    foreach($validation_rules as $form_rules)
    {
        list($name, $title, $rules) = $form_rules;
        $this->form_validation->set_rules($name, $title, $rules);
    }                          
    // Run the validation and resturn the result
    return $this->form_validation->run();
}

Now is the time to focus on main validation function. It will be picking up the form name to be validated from the current URL than it will run it against validation rules that we specify for each form element.

So for example for form_1 we are setting up validation rules for username and key input fields. In that case username and key fields will be required. We can use more advanced validation but now we will stick up with basic validation so you could get a better overview of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Validate forms with set rules
    public function validate()
    {      
        // Get the form name from URI
        $form_name = $this->uri->segment(3);
 
        // Setting validation rules for form1
        $rules['form_1'] = array(
            array('username', 'Username', 'required'),
            array('key', 'Key', 'required'),
        ); 
        // Setting validation rules for form2
        $rules['form_2'] = array(
            array('username', 'Username', 'required'),
        );
        // Setting validation rules for form3
        $rules['form_3'] = array(
            array('username', 'Username', 'required'),
        );
        // Validate specific form according to form name given in URL
        $this->validate_form($rules[$form_name]);   
        // Return to forms and display the result
        $this->index();     
    }  
}

So now we have finished writing our forms controller. New we are going to create a view which will hold our forms.

The view

In our view we will make sure that we output any validation errors, generate three forms and get the validation results. So first of all we’ll get (if there are any) validation errors.

1
<?php echo validation_errors(); ?>

Then we will generate three forms with elements specified in controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<h2>Form 1</h2>
<?php   
print form_open(base_url('forms/validate/form_1'), $form_1) .
form_input($form_1['username']) .
form_input($form_1['key']) .
form_input($form_1['submit']) .
form_close();
?>
 
<h2>Form 2</h2>
<?php   
print form_open(base_url('forms/validate/form_2'), $form_2) .
form_input($form_2['username']) .
form_input($form_2['submit']) .
form_close();
?>
 
<h2>Form 3</h2>
<?php   
print form_open(base_url('forms/validate/form_3'), $form_3) .
form_input($form_2['username']) .
form_input($form_2['submit']) .
form_close();
?>

At the end of our view we will output validation results.

1
2
3
4
5
6
7
<?php
if (isset($result) && $result)
{
    echo '<h1>Result</h1>';
    echo $result;
}
?>

If you followed this tutorial now you should be able to validate all three forms according to submission.

You can download the source code for this tutorial at the bottom.

Good luck and have fun with validation.

How to validate multiple forms on one page in CodeIgniter?               

In this tutorial I will show how to create a controller and a view in CodeIgniter to render, validate and output forms, validation error messages and results for multiple forms on one page.

We will start from creating the controller.

The controller

On top of every CodeIgniter controller script file we need to put the following line

1
if (!defined('BASEPATH')) exit('No direct script access allowed');

which prevents from accessing the controller from the outside.

Now we’ll create a CodeIgniter controller script for rendering forms, validation and getting the validation results. As you probably know all controller scripts are placed in application/controller directory in you app’s root folder and each script has the same filename as the class inside.

More about this you can find [here; link for external CI site or other article].

Let’s create a class called “Forms” which will extends from the main CodeIgniter controller class (CI_Controller)

1
class Forms extends CI_Controller {

Then we have to create a class constructor to initialize parent class constructor first to we will have access to all CI_Controller methods then we load form helper which contains functions that assist in working with forms.

1
2
3
4
5
6
7
// Controller constructor
public function __construct()
{
    parent::__construct();
    // Load form helper required to validate forms
    $this->load->helper('form');     
}

We will write a method (function) for defining form elements and passing that data to the view so it can be rendered. Index function is executed by default when accessing controller. In index method we’ll define three forms. First one will go with username and key text input fields and the rest will go with just a username text input field. And the end of this function we take all the arguments and pass it on to our view which will render the forms.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Prepare data for the view to output the forms
public function index()
{
    // Defining form1 elements
    $data['form_1'] = array('class' => 'main_form', 'id' => 'form_1');
    $data['form_1']['username'] = array('name' => 'username', 'value' => 'Default username');
    $data['form_1']['key'] = array('name' => 'key', 'value' => 'Key');
    $data['form_1']['submit'] = array('value' => 'Submit!', 'type' => 'submit');
 
    // Defining form2 elements
    $data['form_2'] = array('class' => 'main_form', 'id' => 'form_2');
    $data['form_2']['username'] = array('name' => 'username', 'value' => 'Default username');
    $data['form_2']['submit'] = array('value' => 'Submit!', 'type' => 'submit');
 
    // Defining form3 elements
    $data['form_3'] = array('class' => 'main_form', 'id' => 'form_3');
    $data['form_3']['username'] = array('name' => 'username', 'value' => 'Default username');
    $data['form_3']['submit'] = array('value' => 'Submit!', 'type' => 'submit');     
    // Load the forms view and pass data to the view
    $this->load->view('forms', $data);       
}

New we’ll create a function that will assign validation rules to each of form elements and return the validation status.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Form validation function
private function validate_form($validation_rules, $callback=NULL)
{
    $array_index = 0;  
    // Load the form validation CodeIgniter library
    $this->load->library('form_validation');                     
    // Loop through validation rules and set it up for each element
    foreach($validation_rules as $form_rules)
    {
        list($name, $title, $rules) = $form_rules;
        $this->form_validation->set_rules($name, $title, $rules);
    }                          
    // Run the validation and resturn the result
    return $this->form_validation->run();
}

Now is the time to focus on main validation function. It will be picking up the form name to be validated from the current URL than it will run it against validation rules that we specify for each form element.

So for example for form_1 we are setting up validation rules for username and key input fields. In that case username and key fields will be required. We can use more advanced validation but now we will stick up with basic validation so you could get a better overview of it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Validate forms with set rules
    public function validate()
    {      
        // Get the form name from URI
        $form_name = $this->uri->segment(3);
 
        // Setting validation rules for form1
        $rules['form_1'] = array(
            array('username', 'Username', 'required'),
            array('key', 'Key', 'required'),
        ); 
        // Setting validation rules for form2
        $rules['form_2'] = array(
            array('username', 'Username', 'required'),
        );
        // Setting validation rules for form3
        $rules['form_3'] = array(
            array('username', 'Username', 'required'),
        );
        // Validate specific form according to form name given in URL
        $this->validate_form($rules[$form_name]);   
        // Return to forms and display the result
        $this->index();     
    }  
}

So now we have finished writing our forms controller. New we are going to create a view which will hold our forms.

The view

In our view we will make sure that we output any validation errors, generate three forms and get the validation results. So first of all we’ll get (if there are any) validation errors.

1
<?php echo validation_errors(); ?>

Then we will generate three forms with elements specified in controller.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<h2>Form 1</h2>
<?php   
print form_open(base_url('forms/validate/form_1'), $form_1) .
form_input($form_1['username']) .
form_input($form_1['key']) .
form_input($form_1['submit']) .
form_close();
?>
 
<h2>Form 2</h2>
<?php   
print form_open(base_url('forms/validate/form_2'), $form_2) .
form_input($form_2['username']) .
form_input($form_2['submit']) .
form_close();
?>
 
<h2>Form 3</h2>
<?php   
print form_open(base_url('forms/validate/form_3'), $form_3) .
form_input($form_2['username']) .
form_input($form_2['submit']) .
form_close();
?>

At the end of our view we will output validation results.

1
2
3
4
5
6
7
<?php
if (isset($result) && $result)
{
    echo '<h1>Result</h1>';
    echo $result;
}
?>

If you followed this tutorial now you should be able to validate all three forms according to submission.

You can download the source code for this tutorial at the bottom.

Good luck and have fun with validation.

FROM_UNIXTIME(unix_timestamp), FROM_UNIXTIME(unix_timestamp,format)

Returns a representation of the unix_timestamp argument as a value in ‘YYYY-MM-DD HH:MM:SS’ or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone. unix_timestamp is an internal timestamp value such as is produced by the UNIX_TIMESTAMP() function.

If format is given, the result is formatted according to the format string, which is used the same way as listed in the entry for the DATE_FORMAT() function.

mysql> SELECT FROM_UNIXTIME(1196440219);
        -> ‘2007-11-30 10:30:19’
mysql> SELECT FROM_UNIXTIME(1196440219) + 0;
        -> 20071130103019.000000
mysql> SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(),
    ->                      ’%Y %D %M %h:%i:%s %x’);
        -> ‘2007 30th November 10:30:59 2007’
Note: If you use UNIX_TIMESTAMP() and FROM_UNIXTIME() to convert between TIMESTAMP values and Unix timestamp values, the conversion is lossy because the mapping is not one-to-one in both directions. For details, see the description of the UNIX_TIMESTAMP() function.