Posts Tagged ‘codeigniter’

Server recommendation for CodeIgniter applications

Posted in Articles on April 15th, 2010 by admin – Be the first to comment

One of the most notorious benefit about using CodeIgniter is that CI applications usually runs without problems on any common Linux server as well as Windows servers supporting PHP. This is a strong benefit versus other PHP frameworks that require special server settings or configuration in order to work.

So, lot of server companies may be suitable for CodeIgniter applications, but here are some recommendations for Hosting providers that we were using to successfully run CodeIgniter applications.

Managed VPS

For Managed VPS services we have been using WiredTree.

They provide a reliable service with very fast support that makes this ideal for applications that must be up most of the time.

Shared Hosting

For Shared Hosting, we were using A2Hosting.

They provide a good relation cost/benefit for shared accounts and reseller accounts with different plans and packages that makes this suitable for general purpose applications or websites.

advertisement

Using GeoIP in CodeIgniter application

Posted in Snippets Ideas on January 9th, 2010 by admin – 3 Comments

The company called Maxmind published free resources to use Geo localized IP services in your web applications. I remember this service is there from long time ago.

Now, we are interested in getting geo information from users in a website, so it could be a good idea to integrate GeoIP services as a CodeIgniter plugin.

@Burak posted a short but nice article at phpandstuff.com explaining how to integrate GeoIP in a PHP script.

So, based on that article, I’d integrate the GeoIP as a plugin, by following these steps:

1. Create a new plugin file under application/plugins/geo_ip.php and I put there the content that is inside geoip.inc

2. We’d need to copy the GeoIP.dat file to any location inside our application. I used resources/geo/GeoIP.dat as location.

3. Then we’d need to load the plugin file, we can do that on any particular controller file (on demand) or we can add it as autoloaded plugin in application/config/autoload.php

4. I went to the controller class where I want to use GeoIP to grab the user’s country name and code, and put this snippet:


        $gi = geoip_open('resources/geo/GeoIP.dat',GEOIP_STANDARD);
        $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
        $this->data['country_code'] = $country_code;
        $country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
        $this->data['country_name'] = $country_name;
        // close the database
        geoip_close($gi);

Update 2010-04-09: The code below was updated accordingly to Kubilay comment, thanks. Row 5 was switched with row 4.

That’s all, assuming that finally you will have the Country Code and Country Name under $this->data variable. You can see more info about why I use this way to pass information to view, under Using a Super Controller Class in CodeIgniter.

Related searches:

  • codeigniter geoip
  • integrate geo-ip for codeigniter
  • Maxmind codeigniter
  • geoip codeigniter
  • codeigniter maxmind
  • codeigniter ip country
  • geoip in codeigniter
  • codeigniter ip to country
  • geoip with codeigniter
  • geoip integration in codeigniter

How do I display result or error messages in CodeIgniter application

Posted in Tutorials on September 18th, 2009 by admin – 12 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.

Related searches:

  • codeigniter error to email
  • codeigniter flash message
  • codeigniter error message
  • codeigniter error messages
  • codeigniter set_flashdata
  • codeigniter message
  • codeigniter messages
  • flash message in CodeIgniter
  • flash message codeigniter
  • codeigniter flash messages

How do I use Constants in CodeIgniter applications?

Posted in Snippets Ideas on August 28th, 2009 by admin – Be the first to comment

Usually I need to use constants or pass them as parameter in a function.

I don’t like the idea to hardcode these values in the code, nor sometimes I don’t like to use the PHP define() function to define constants since later it is a mass to search where a constant for a given entity has been defined.

Recently I started to use constants by defining these constants in a model Class (as reference you can find more info about Object Constants here).

Let’s say we have an Order_model class that manages Orders in an e-commerce application built in CodeIgniter. That Order_model class will implement the methods for create, update, list or view orders, for example.

Now, imagine we want to define simple states for each order. So, an order can be APPROVED as 1 or PENDING as 0.

A natural way to do that (wrong way IMHO) is to set the numeric value (1 or 0 in this case) hardcoded.

Instead of that, I created two object constants, as follows:


class Order_model extends Model {

    var $tablename = 'Orders';
    var $tablename_details = 'OrderDetails';

    const ORDER_STATUS_PENDING = 0;
    const ORDER_STATUS_APPROVED = 1;

Then, imagine we have an update_order_status() function in that model that will change the order status:


function update_order_status($order_id, $new_status)

Later, in the Controller class that will call this update function, I’ll use the constant in this way:


$this->order_model->update_order_status($order_id, Order_Model::ORDER_STATUS_APPROVED);

By doing that, the next time I need to change or maintain that code, I’ll go directly to the model class.

Related searches:

  • codeigniter constants
  • codeigniter define constants
  • codeigniter const
  • CODEIGNITER CONSTANT
  • how to use constant in codeigniter
  • codeigniter define constant
  • Constant codeigniter
  • how to define constants in codeigniter
  • define constant in codeigniter controller
  • define constants in codeigniter

Profiling your CodeIgniter applications

Posted in Snippets Ideas on August 14th, 2009 by admin – Be the first to comment

The CI Output Class has enable_profiler() to set enable the profiler in CodeIgniter.

By using this operation, you can enable or disable the profiler in the output.


$this->output->enable_profiler(TRUE);

The profiler is very useful for debugging purposes, you can see a footer with information all the time while running your application in development phase.

read more »

Related searches:

  • hook in codeigniter
  • codeigniter profiler
  • codeigniter Hooks
  • codeigniter profiling
  • profiler codeigniter
  • Profiling codeigniter
  • codeigniter db profiler
  • ci enable profiler
  • ci enable profiler using
  • helpful codeigniter hooks