Codeigniter best tricks and hacks

Hi Friends,
Today I am sharing you some best tricks i have used in couple of projects, may be these things are common but we need these in all of our projects and we can find it on google but by different different authors, i am providing all the codeigniter tips tricks and hacks in this single article. if i missed any other trick then please let me know through comments, I will update it to my article.

Now please find the

Codeigniter best tricks and hacks

1: Remove index.php from url codeigniter

Solution : we need to update the .htaccess with following code:

2: Call Controller function or varriable from view

Solution : We need to pass the controller self object to its view to call the function or varriable of that controller . please follow instructions:

3: Db prefix in core queries

Solution : in database.php we need to pass swap_pre in configuration index in db config array. please see example:
In database.php there is a config array start with following:

So in this list there is a index named db_prefix
this is general usage in CI auto qyuiry builder class
but if we are writing the core queries then we need to pass any string like:

now we can use the swap_pre value appending before table name in our query as following:

4: Call model and its function from library and helpers

Solution : we can load any model from helper or library by instantiating the ci global object using call by refrence as following:

5: Get rendered html of your view in a Controller’s varriable

Solution : Use third parameter as true in this load view

Eg:

check value already exists in codeigniter

Callback function of codeigniter which checks whether the value already exists in particular column of table or not.

This function work on add and edit both cases,it return false if value exists and return true if value not exists.

following is the function that you need to put in your controller file:

Note: change the table name in function and in edit case you can change the primary key!= with your table’s key name.

Now time to using this callback function for adding and editing forms.We need to pass the callback function in validation rules.

For the time of adding you can pass the rule in your add action like as:

and in your edit action you can use the rule as following:

Thanks for reading full article , Now use it in your project and if you face any trouble then comment here.

Custom form validation in Codeigniter

Hi Geeks,
Today i am sharing you the Custom form validation example in codeigniter. Suppose we have a custom requirement to validate the data with our own custom rules like email already exist in database, So for this type on need can be resolve only with custom callback functions .these are the following steps to create a custom validation for any form.
Step 1: Form Layout form.php in views

Now , We have 4 fields in this form –
first name,last name, email and password
for validate these fields codeigniter already provided rules in form_validation library:
Some of the rules are following listed:
valid_email,required,valid_email,valid_url,min_length[5]|max_length[12] etc.

But Now we need to detect that email already registered in Database ,So we need to create a callback function for validating the email already check in database.

Note:Codeigniter itself providing rule for ‘value already exists in database’ but here our purpose is to demonstrate the custom form validation in codeigniter thats why we are assuming that this need a custom form validation.
Step 2. Create a callback to match the field’s value in database

Step 3. Load Form validation library in controller’s Action or Constructor method as following:

Step 4. Now pass the array of validation rules for the form in add method of user controller as following:

Please comment here if you have any query regarding to this form validation snippet or not cleared anything.

CONVERT HTML TO PDF IN CODEIGNITER Using MPDF

Hi Geeks,
Today I am Sharing you the code for making pdf or download pdf files in codeigniter. In this Code snippet we have used the MPDF Kit for generating pdf of html views or dynamic templates. So Following are the steps to create/convert HTML to PDF In Codeigniter using MPDF

Prerequisites :- GD LIBRARY SHOULD BE ACTIVE ON YOUR WEB SERVER

Step 1: Download the Mpdf  Class and methods from here or  https://www.mediafire.com/?9qmqaw2yqlo5caa
Now extract the zip file and put the mpdf folder to application >> third_party folder (this folder Exists in Application folder in codeigniter app )
Step 2. Now make a ci library to call/communicate the mpdf methods and classes from your ci controllers

Now go to libraries folder in application folder and create a file named : M_pdf.php having following code snippet.

Step 3: Now in any of your controller where you want to use or convert the view to pdf output

You can see the calling of our created libary by loading the libarray using this->load_library, and pass the data whatever you required in your view or get the data from model and just pass to your pdf view and use/render that data in your view just like a normal view and controllers.

for sample , We have created a method named save_download having following code

 

By using this code you will able to make a functionality to convert html to pdf in codeigniter:
If you face any struggle in this please Comment below – Thanks & Cheers 🙂

How to remove index.php from Codeigniter url

Here we will learn how to remove index.php from codeigniter url.

Each of codeigniter constructed url contains the value index.php and excluding root url. No url cannot be run without index.php

Assume a scenario , we have a website named www.myciportal.com

Where we didn’t remove the index.php from url and i have a category with following details

category name: techbuzz

So by default it’s url will create like  following

default url : www.myciportal.com/index.php/techbuzz

it will not run like this way

www.myciportal.com/techbuzz

For make this possible we have to work on config and htaccess both and steps are following to achieve this task

If you are using mod_rewrite to remove the index.php in each url construct of codeigniter then you have to follow these steps:
Step 1: In CI Project root Folder of your project create a .HTACCESS file and place following code in it

Comments :: In above code myprojectname is passed with RewriteBase , which is the demo project name of our ci project,if you are running on main domain then Redirect base will be / only , or if you are running your project in any directory then you have to specify that name.
Now Step 2:
in your project >> application/config/config.php
Replace following line

with

Now you can run your Codeigniter project and you can see the index.php has been removed from every Controller Action