3 interesting Codeigniter tips

Finally we’ve started doing all our development using a framework, Codeigniter. It has a small learning curve, simple to use and very flexible with how much you want to adhere to MVC pattern. You have the control to code every damn thing in the controller or have proper models and views to help the controller.

Three nice tips/tricks I found in Codeigniter after googling a lot.

1. How to print the last query executed by codeigniter when using active record?

The following function returns the sql query executed. You may need this for debugging purposes.

$this->db->last_query();

2. How to add a function in controller which can’t be accessed through URL?

Usually, if you add a function func in controller c, then it can be accessed by /c/func. If you want to code a function, which you don’t want people to access, then you need to name the function _func(). If you have a function named with a _ in front of it, then you can’t access it through the URL.

3. How to store the string generated by a view in a variable?

When you load a view using $this->load->view(‘view.php’, $data); the view gets rendered in the browser. But if you want to store the HTML string generated by the view in a variable, then add a third parameter TRUE, which will return the generated string.

The code will be
$output = $this->load->view("view.php", $data, TRUE);

Leave a Reply

Your email address will not be published. Required fields are marked *


five − 3 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>