jigniter™

jQuery & CodeIgniter – Perfect combination for web application

Archive for the ‘Tutorials’ Category

How do I display result or error messages in CodeIgniter application

with 3 comments

Sometimes we need to display general messages or error messages to the user noticing about certain action, for example on success or failure.

Imagine you are submitting a form and then creating or updating the data in the database. You may be interested in noticing the user about the result for that action.

This is the way I use to display general messages (and error messages) in CodeIgniter application.

The result

This is how my message looks.

codeigniter-display-message

How to display the message?

Now, in order to display that information message, I do the following:

First, when submitting the form, after validating it, in the controller I call the model function that will save the entity (license in this case). I make sure that model function returns a result, and FALSE result in case of error (I usually return an array result on success with an ID element with the last inserted identifier if the result was an insert, or the just updated entity identifier if it was an update, but that is out of this article’s scope).

Controller

Imagine we are calling the following method in the controller:

$result = $this->license_model->create_or_update_license($agent_id, $state, $data);

Then, we need to see if that was an error or not. Based on that result, I will use CodeIgniter’s set_flashdata function in the Session library, as follows:

if ($result)
{
	$this->session->set_flashdata( 'message', array( 'title' => 'License created', 'content' => 'License has been saved sucessfully', 'type' => 'message' ));
	redirect('agent/licenses');	

} else
{
	$this->session->set_flashdata( 'message', array( 'title' => 'License error', 'content' => 'License could not be saved', 'type' => 'error' ));
	redirect('agent/licenses');
}

Once the Flash message has been set, I redirect the user to the form or a list of results. That is needed in order to get the flash working (you cannot just load the view in this case… well, you can but this method will not work in such case). When comparing $result TRUE or FALSE, please notice the different value for type. I am using type=message for successful messages, and type=error for error mesages.

View

In the view, just above the form, I use this snippet.



<?= @flash_message() ?>

To keep it simple, I just added @ to avoid any error message, but probably if you are looking for a better coding practice and you may not do that.

A Helper

Then, I put this flash message function as a helper function, so it is available when calling from the view.


function flash_message()
{
	// get flash message from CI instance
	$ci =& get_instance();
	$flashmsg = $ci->session->flashdata('message');

	$html = '';
	if (is_array($flashmsg))
	{
		$html = '<div id="flashmessage" class="'.$flashmsg[type].'">
			<img style="float: right; cursor: pointer" id="closemessage" src="'.base_url().'images/cross.png" />
			<strong>'.$flashmsg['title'].'</strong>
			<p>'.$flashmsg['content'].'</p>
			</div>';
	}
	return $html;
}

jQuery part

Finally, I added a simple effect to slide down and blink the message box, and adding the close functionality with a simple jQuery function. You can do something like this. Of course, you can modify it or just avoid using the jQuery effect if it is desired.


// first slide down and blink the message box
$("#flashmessage").animate({top: "0px"}, 1000 ).show('fast').fadeIn(200).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);

Some CSS styling

Finally, just add some CSS styling for #flashmessage, using message or error class names.


.message{
    border:1px solid #CCCCCC;
    width:300px;
    border:1px solid #c93;
    background:#ffc;
    padding:5px;
    color: #333333;
    margin-bottom:10px;
}

Enjoy it.

Written byadmin

September 18th, 2009 at 11:49 pm

Posted inTutorials

Tagged with , , ,

Simple Form Generation in CodeIgniter

with 7 comments

If you are using CodeIgniter, as well as any other PHP framework, you may notice that building CRUD forms is one of the most bother and routine tasks. Probably 80% of general web applications uses CRUD (create/read/update/delete).

In CodeIgniter, you can use any of the form generation libraries.

One of my preferred libraries is Form Generation Library by Frank Michel (see @macigniter’s thread here). It allows you to create clean XHTML forms with CodeIgniter. See demo here.

Using Form Generation Library

So, in order to use this library, you need to download it from here (library files only is enough if you don’t want the entire site structure).

Read the rest of this entry »

Written byadmin

August 2nd, 2009 at 6:30 pm

Posted inTutorials

Tagged with , ,

Debug CodeIgniter AJAX applications with FireIgnition

without comments

image

If you are building rich AJAX applications with CodeIgniter, you may encounter that traditional debug utilities may not be suitable for debugging tasks that involve AJAX function calls.

Firebug addon with its FirePHP plugin can enhance your debugging process.

Read the rest of this entry »

Written byadmin

July 26th, 2009 at 9:48 am

Posted inTutorials

Tagged with

Remove index.php from URL

with 9 comments

CodeIgniter is designed to be search-engine and human friendly. By default, index.php will be included in the URLs, but it can be easily removed using .htaccess file with some rules.

Read the rest of this entry »

Written byadmin

July 25th, 2009 at 2:28 pm

Posted inTutorials

Tagged with

Internationalization i18n in CodeIgniter

without comments

Developing web applications with i18n support is easier with the help of a PHP framework. CodeIgniter has many libraries and helpers that makes the life easier for developers that require i18n support in their applications.

Here are some libraries and articles that may help you to figure how to create i18n web applications:

Using the Language Class & Helper

The Language class provided in CodeIgniter may be the first place to check out. It supports an easy way load language files.

In addition, the Language helper assists you while working with language files. It simply provides a lang() helper function instead of calling $this->lang->item() each time.

I’d strongly recommend the Internationalization Views i18n Wiki page. That article will guide you how to add i18n support in views, controllers and configuration files. You can also review the Internationalization category in that Wiki in order to find more information regarding to i18n approaches.

Using a library

Internationalization (i18n) library for CodeIgniter is a library that makes possible to easily change between languages by adding the language code in the URL.

For example, you may be able to use the following URL to switch between languages:

  • domain.com/en/controller
  • domain.com/fr/controller

The following diagram (taken from Maestric website) illustrates the way this library works.

Written byadmin

July 16th, 2009 at 2:02 pm

Posted inTutorials

Tagged with ,

Learning CodeIgniter PHP Framework

without comments

CodeIgniter is a great PHP framework, well-documented and very easy to learn for developers that already used other frameworks such as Symfony, CakePHP, Zend or PHP from scratch.

Organizing stuff to read

If you are interested in learning CodeIgniter, I’d recommend the following resources:

  • CodeIgniter User guide. This is a must-read documentation for any new developer interested in CodeIgniter. By opening the Table of Content you can go chapter-by-chapter understanding the following concepts:
    • Server requirements and installation.
    • Getting started, CodeIgniter at a Glance, Supported features, Application Flow Chart to understand the application flow, MVC to understand Model-View-Controller design pattern if you are new to object oriented programming or patterns, and architectural goals.
    • General Topics. Here you can find information regarding how CodeIgniter works, PHP Style Guide, Writing Documentation standard, Security topics, Libraries and more.
    • Class Reference. Including dozens of classes that you need to understand which functions exposes.
    • Helper Reference. Introduces many helpers that may speed or boost your development process.

Learning by examples

Looking for examples may help you to learn CodeIgniter.

Setting up Codeigniter, as seen in If Only I Knew That! blog. Step by step guide that shows you how to setup a CodeIgniter installation and

A Gentle Introduction to CodeIgniter, from EEInsider, explains how MVC design pattern works in CI (controller, models and views).

Getting started with CodeIgniter, from IBM, explains how to create MVC applications quickly and easily.

From my point of view, the first time I learned CodeIgniter was reviewing the source code of BambooInvoice, an open source invoicing application (created by Derek Allard).

Other resources

Here is a list of additional resources that may help you in the process of learning CodeIgniter framework. TheFuzzOne published here a list of resources with tutorials and information for newbies as well as intermediate developers.

The forum is a key resource too, on CodeIgniter forum you can find solutions to common problems. It is recommended to search for an specific problem/solution before asking a new question or opening a new thread, since most of the typical problems new developers have, are actually resolved and explained with a solution.

Probably one of the most used and useful threads there is the Code and Application Development thread.

MVC and CRUD related threads.

Written byadmin

July 15th, 2009 at 11:53 am

Posted inTutorials

Tagged with