Archive for the ‘Tutorials’ Category
How do I deploy my CodeIgniter applications using Subversion
This little guide is intended to explain how do I update some of my applications running on development environment or production using Subversion and a simple hook.
This idea was based on the great article published time ago by Imran Nazar about automating deployment with Subversion.
Basically it updates a SVN repository in your development environment each time you commit a message with a signal string like *DEPLOY*. This assumes that you have a copy of your repository in your development environment.
How do I display result or error messages in CodeIgniter application
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.

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.
Simple Form Generation in CodeIgniter
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).
Debug CodeIgniter AJAX applications with FireIgnition
![]()
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.
Remove index.php from URL
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.




