Populating Select Controls

Pass an array to the view from the controller. Loop through each item adding the item to the select box. If we submit the form we probably want to remember the values selected by the user

 

 <!-- Render Customer Names List Box -->

Client

All Customers customerID==$search[‘customer_id’]){ $selectedTag = “selected “; } else { $selectedTag = “”; } ?> value=”customerID ?>”>customerName ?>

</div>

Alternatively we can render the control based on a local array

 <!-- Report Option -->
$report = array(
     array('value' => '1', 'label' => 'Billed'),
     array('value' => '2', 'label' => 'Paid'),
     array('value' => '3', 'label' => 'Outstanding')
);

foreach($report as $item) {
     if($item['value']==$search['report_id']){
          $selectedTag = "selected";
     } else {
          $selectedTag = "";
     }

     echo "< option " . $selected . $item['label'] . "";
}

We pass the value of the report ID into the view i the variable $search[‘report_id’]

Server Side Validation

The simple example below might be used for example on a simple registration form requiring  an email address, and a password. If the data being passed to the controller meets the validation requirements, we redirect to the members page, if the validation fails, we redirect back to the login form.

$this->load->library('form_validation')
$this->form_validation->set_rules('email','EMail','required|valid_email')
$this->form_validation->set_rules('password','Password','required')

if ($this->formvalidation->run()==TRUE) {
     redirect('main/members')
} else {
    $this->load->view('login')
}

we need the form validation library, and that generally would be loaded in the controllers constructor

Validation Options

There’s a whole bunch, and you chain them together, as in the example above

  1. required
  2. numeric
  3. max_length[10]
  4. min_length[5]

Custom Validation

In addition to the standard validation rules provided by code igniter, we can create our own custom callback functions to check for specific user requirements. For example if developing a user registration system we might want to determine if the email, or the username have already been taken.

When we add custom rules through the use of callback functions, we also need to specify the error message that should be displayed. This can be coded directly in the controller, or directly added to the config file (however, the problem with this approach is that this code will possibly be overwritten with updates of code igniter.

Select Controls

In many use cases you may not have a default selection, but you do require the user to select an option from the list. Here’s a nice HTML only based approach to implimenting this.


<select>
    <option disabled selected value> -- select an option -- </option>
    <option>Option 1</option>
    <option>Option 2</option>
    <option>Option 3</option>
</select>

Codeigniter

CodeIgniter is a powerful PHP framework with a very small footprint, built for developers who need a simple and elegant toolkit to create full-featured web applications.

If your not currently using an MVC framework, you could do a lot worse than check out Codeigniter, its simple to install and use, and its free. Whats not to like ?

Setting up codeigniter for a new project is one of those things that you occassionally need to do, and inevitably you can never quite remember precisely what you need to do to to get things up and running. Here’s our step by step process

1 – Download the latest version from https://codeigniter.com/

2 – Create a new folder for your project under your web servers htdocs directory (e.g. myApp)

3 – Copy the codeigniter folders into your project folder, myApp/applications, myApp/system etc.

4 – Open up your web browser and navigate to localhost/myApp. You should see the default code igniter welcome page.

5 htaccess file: Removing the index.php file

By default, the index.php file will be included in your URLs:

example.com/index.php/news/article/my_article

You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the “negative” method in which everything is redirected except the specified items:

General Configuration

For small projects everything kind of works out of the box, but you’ll probably want to make a few configuration changes to make life easier, add a database etc.

Changes to application/config.php

you’ll probably want to make URL’s in your app relative to your top level domain. Generally you’ll use codeigniters functions base_url() or site_url(). These values get set right here.

$config[‘base_url’] = ‘http://localhost/myApp/&#8217;;

Adding a Database

If the application requires a database, you’ll need to supply credentials in the following configuration file:  application/config/database.php

Feckin Computers

This is a blog for programmers, and those who want to learn a little bit more about the art of computer programming.

I must hear the phrase “Feckin Computers !”, a thousand times a day, and its a phrase that I find myself using with increasing frequency after almost 30 years in this crazy industry.

We are perhaps being a bit unfair as the source of the problem is usually the programmer rather than the hardware, and in many cases, we make the same basic mistakes over and over and over again, and thats understandable, programming is a tough gig, and its getting harder.