To blog or not to blog?

Codeigniter Hints and Tips

Web Design

CodeIgniterI'm actually feeling a bit guilty as I haven't added anything to this blog for quite some time, mainly because I've been so busy... but anyway to try and make up for it here's some hints and tips to help solve some real challenging problems I came across when using CodeIgniter...

How to get $_GET queries:

Now as many of you may have found out already, CodeIgniter basically discards  query strings (anything after the ? in your URL's) for what are essentially conceptual reasons. Now I have to say I do agree with their strategy but sometimes you absolutely have to use GET and there's no alternative.

After searching around I found many people giving examples which require modifying the main Codeigniter application. Now I don't know about you but this seems a bit overkill to me just to do a simple one off $_GET, so I looked around for something simpler. After much research and testing I found a nice simple solution. Edit your application/config/config.php file and change the line:

$config['uri_protocol'] = "AUTO";
...to...
$config['uri_protocol'] = "PATH_INFO";

This basically stops CodeIgniter generating 404 errors when your URL has a ? in it.

Now you need to get the data. As mentioned before $_GET doesn't work. The solution? Use $_REQUEST instead, its as simple as that.... Sure you have to be careful to make sure your cookies and $_POST variables don't get mixed up but it is a quick clean and simple solution.

How to pass URI segments as parameters to your default index function:

Usually passing segments to a CodeIgniter controller is easy. Lets say your URL is:

http://mysite/blog/show/32
or if you haven't got the redirect set up yet...
http://mysite/index.php/blog/show/32

You create a controller called blog.php with a function called show which looks like the following:

function show($id = '')

The value $id will then be set with then number 32....

The problem arises when you want to do the following:

http://mysite/blog/32
or if you havn't got the redirect set up yet...
http://mysite/index.php/blog/32

It seems like it should be as simple as setting up an index function like so:

function index($id = '')

But it doesn't work....

The reason it doesn't work is because CodeIgniter is looking for a function called 32 inside your blog.php which doesn't and can't exist. Really the correct path would be:

http://mysite/blog/index/32
or if you havn't got the redirect set up yet...
http://mysite/index.php/blog/index/32

...but that would be horrible.

Again there is a simple but not very obvious solution...

Edit your application/config/routes.php file and add the following:

$route['blog/(:num)'] = 'blog/index/$1';

Now when any URL containing blog/ with a number after it is submitted it will jump to your index function. You can even extend this to:

$route['blog/(:any)'] = 'blog/index/$1';

...but remember, that will mean no other functions in your blog class will work as they will all be redirected to your index controller and passed as a parameter!

How to load models inside a model:

Am I insane? Perhaps, but I occasionally I find the need to load a model inside another model. Unfortunately it appears that CodeIgniter, well at least version 1.7.1 of CodeIgniter (which is current at the time of writing) has a problem doing this. For example, if you load a model inside you model like this:

$this->load->model('my_model');
$this->my_model->my_function();

...it simply wont work.

Again once you know it the solution is simple....

$CI = &get_instance();
$CI->load->model('my_model');
$CI->my_model->my_function();

How to set a date field to the current date using the active record class:

Lets say you wanted to insert or update a bunch of fields in the database using the active records class but you also want to set an entry with the current date using the MySQL NOW() function. As you probably guessed the following doesn't work...

$my_data = array( 'created' => 'NOW()', 'field_1' => 'data', 'field_2' => 'data' );
$this->db->insert( 'my_table', $my_data );

The reason it doesn't work is because the active records class correctly escapes your NOW() string and executes SET `created`='NOW()' instead of SET `created`=NOW() which is what you want. Well it turns out the solution is simple...

$my_data = array( 'field_1' => 'data', 'field_2' => 'data' );
$this->db->set( 'created', 'NOW()', FALSE );
$this->db->insert( 'my_table', $my_data );

The FALSE tells it not to escape the field! Thats it, job done!

Goodbyes...

Hopefully I've shown a couple of very simple ways to achieve tasks which would otherwise be quite complicated. If I just save one person the hours of work it took me to solve some of these riddles then its all worth it!

(Updated: 16:16, Fri 8th May 2009)
Posted by Daniel: 17:36, Thu 7th May 2009

RE: Codeigniter Hints and Tips

hi there!

i'm having an issue with passing arguments in codeigniter. 'have followed the docs, etc. but couldn't figure this one out.  i have a controller which is working fine until i required a function to have 1 argument passed.  i.e. 

/mysite/index.php/mycontroller/myfunction/arg1 

i'm getting = /mysite/index.php/mycontroller/myfunction/arg1.php is not found! 

but when i enter /mysite/index.php/mycontroller/myfunction

i'm getting php error Message: Missing argument 1 for mycontroller::myfunction()

is this a configuration issue???

'hope you can shed some light in my dilemma :(

thanks

 

Posted by rayray: 1:17, Tue 25th Aug 2009

RE: Codeigniter Hints and Tips

ok, i think i found the problem...  it was a bug in my controller --- my bad ;)

Posted by rayray: 4:30, Tue 25th Aug 2009

RE: Codeigniter Hints and Tips

Cool, glad you got it sorted rayray...

Posted by Daniel: 7:52, Tue 25th Aug 2009

Login to leave a comment...

Username:
Password:

I want to create an account   I've forgotten my password/activation code