Archive for the Category » Code Igniter «

Convert CI version 1.7 to 2.0

You may have some projects build using Code Igniter version 1.7.1 / 1.7.2 or earlier. And now Code Igniter 2+ version released. So eventually a question should arise in your mind “How to convert old projects into new version easily”

Here are 8 steps by which you can easily make compatible your old projects into new version of CI.

1.  Replace new system folder and index.php file.
2.  All Controllers now extend CI_Controller instead of Controller
3.  in all controllers constructor
function __construct()
replace parent::Controller(); by parent::__construct();
4.  All Models now extend CI_Model not extend Model
5.  All models constructor function __construct()
function Model() replace by function __construct()
6.  All model files replace orwhere(‘..’) by or_where (‘..’), orderby(‘..’) by order_by(‘..’), groupby(‘..’) by group_by(‘..’)  [ it's a change of CI 2+]
7. Open application/config/config.php,
Set encryption key like this:
$config['encryption_key'] = 'aminulsumonvi';
8. Create a folder named logs at application/logs and folder permission 0777

Enjoy your codeigniter version 1.7+ is now fully comply with version 2.0+.

Integrate Re-Captcha in CI

In order to implement Re-Captch in Code Igniter  you have to ad lines of code in
1. view file
2. controller
3. config file (2 files, config.php & form_validation.php)

View File (ex: form_login_view.php): more »

Character problem in CI active recordset

How to turn off single quote (‘) in database query?
Simply add false after query as follows:

$query = $this->db->where(“e.event_date>=” , “‘$today’”, false);

Third parameter automatically prevent to generate single quote.

Setup Port Based WAMP Server

Step 1: Open c:\wamp\bin\apache\apache2.2.*\conf\httpd.conf

Step 2: httpd.conf-> NEAR line 469

# Virtual hosts

#Include conf/extra/httpd-vhosts.conf

replace by [remove front #]

# Virtual hosts

Include conf/extra/httpd-vhosts.conf

Step 3: Open c:\wamp\bin\apache\apache2.2.*\conf\extra\httpd-vhosts.conf

Step 4: httpd-vhosts.conf-> Remove all text in this file and add following as much as port you like to add

#

# Use name-based virtual hosting.

#

NameVirtualHost *:80

<virtualhost *:80>

ServerName localhost

DocumentRoot “D:\wamp\www”

</virtualhost>

Listen 81

<virtualhost *:81>

ServerName localhost

DocumentRoot “D:\wamp\www\job\public”

</virtualhost>

Step 5: Test by http://localhost:81 you will get job project.

Add new project: Go to httpd-vhost-> add 82 83 84 85 86 and pointing to the

directory where index.php is located

Best practice of sharing common variable among functions of a class

here is two ways of share value of variable all through member functions of a class

class Clients extends Controller {
function __construct(){
parent
::Controller();
$uri_array = $this->uri->uri_to_assoc(1);
$this->folder_id = $uri_array['folder']; //This not works
$this->session->set_userdata("folder_id",$uri_array['folder']);
}

public function sample() {
$folder_id
= $this->folder_id; //ERROR: Message: Undefined property: Clients::$folder_id
$folder_id = $this->session->userdata('folder_id');
}
}

Taking mysql db backup in a second

Easy way to backup of database(mysql) using CI

$this->load->dbutil();
$this->load->helper(‘download’);
$this->load->helper(‘file’);

$prefs = array(
‘tables’      => array(),  // Array of tables to backup.
‘ignore’      => array(‘ssc_dakhil_exam’),           // List of tables to omit from the backup
‘format’      => ‘zip’,             // gzip, zip, txt
‘filename’    => ‘mybackup.zip’,    // File name – NEEDED ONLY WITH ZIP FILES
‘add_drop’    => TRUE,              // Whether to add DROP TABLE statements to backup file
‘add_insert’  => TRUE,              // Whether to add INSERT data to backup file
‘newline’     => “\n”               // Newline character used in backup file
);

$backup =& $this->dbutil->backup($prefs);
//write_file(‘c:\mybackup.txt’, $backup);
force_download(‘mybackup.zip’, $backup);

Multiple image manipulation(resize/crop/thumb/rotate)in a single script using CI

This is really nightmare to sort out why loop operation get success only for first time. Say, with or without loop i want to resize two images in a single script. i write the resize code but when i execute i found only first image is resized but not second one. WHY??????

after search and search, R&D, test and test finally able to find out the reason. a single line of extra code resolve my issue. and now i am able to resize 100 images inside a loop or write code multiple times to manipulate multiple images. This is the extra line of code:

$this->image_lib->initialize($config);

and here i am croping 100 of images using CI

foreach (glob(“./newspaper-jobs/prothom-alo/2009-05-22/*.jpg”) as $filename)
{
list($width, $height, $type, $attr) = getimagesize($filename);
//here are some logic. set it as you need or discard this portion but adjust variables by yourself.
$crop_x_axis = 0;
if($width<$height)
{
$crop_height_width = $width;
}
else // $width>$height
{
$crop_height_width = $height;
$crop_x_axis = ($width-$height)/2;
}

unset($config);
//Crop an image(Height = Width) Depend on current Height and Width of image
$config['image_library'] = ‘gd2′;
echo $config['source_image'] = ‘./source/image/location/’.basename($filename);
$config['new_image'] = ‘./destination/loc/’.basename($filename);
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
echo $config['width'] = $crop_height_width;
echo $config['height'] = $crop_height_width;
echo $config['x_axis'] = $crop_x_axis;
$config['y_axis'] = 0;

$this->load->library(‘image_lib’, $config);
$this->image_lib->initialize($config);

if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}
$this->image_lib->clear();
}

Enjoy image resizing/croping/or whatever it is !!!

Using CI, Crop an Image

$config['image_library'] = ‘gd2′;
$config['source_image'] = ‘./source/img/loc/aminul.jpg’;
$config['new_image'] = ‘./dest/img/loc/new.jpg’;
$config['create_thumb'] = TRUE;
$config['width'] = ’300′;
$config['height'] = ’300′;
$config['x_axis'] = ’600′;
$config['y_axis'] = ’350′;

//$this->image_lib->initialize($config);
$this->load->library(‘image_lib’, $config);

if ( ! $this->image_lib->crop())
{
echo $this->image_lib->display_errors();
}

Enjoy Croping with CI

File upload in CI [Short Tip: Don't load library first]

I found an interesting thing about CI file resize functionality. To resize an image we need to load image_lib library.

For multiple image upload and resizing, if we load this library first configuration variables later then it gives the following error:
Your server does not support the GD function required to process this type of image.\

instead first load library once:

$this->load->library(‘image_lib’);
Then use LOOP here ————————

$config['image_library'] = ‘gd2′;
$config['source_image'] = ‘./existing/img/loc/aminul.jpg’;
$config['create_thumb'] = TRUE;
//$config['new_image'] = ‘./new/image/location/new_amin.jpg’; // you can assign your image name and location
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->initialize($config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}

Now it should work fine without any error in CI for single or multiple file operation.

.htaccess file for server and localhost

it’s really shame to say, having working experience of around 1 and 1/2 year in code igniter i forget how to use .htaccess for localhost. honestly, i have no problem with .htaccess(for localhost and server) when environment configured as “Port based WAMP server”. however, before i forget let me write

There are two ways of environment setup. You can either configure your PHP, MySql, Apache as General/Conventional way or “Port based”. Both are fine and just a matter of different taste. I feel, no one have huge advantage over other. To give you an idea, say our project name is aminul_fool. In genearl way we create a folder in our root/htdocs/www and browse project as http://localhost/aminul_fool. On the other hand, when your choice port based setup then you can put your project (aminul_fool) anywhere of your computer(not necessary to put it into your web root) and configure port from /bin/apache/apache2.2.8/conf/httpd.conf and lastly browse your project as http://localhost:420.

Make sure your you activate rewrite_module feature. If you use WAMP then to do so, first run wamp server. and left click
wamp (white icon) -> apache -> apache modules-> rewrite_module(put tick mark)
now your server is totally able to handle .htaccess request.

Well, .htaccess file contenet is completely different for different type of environemnt you choose. If you choose conventional way then here is the .htaccess file content

Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^\.htaccess$ – [F]
RewriteRule ^favicon\.ico – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /aminul_fool/index.php?/$1 [L]

And when it’s Port based environment setup or Web Server(www.aminul_fool.com) here is the .htaccess file content

Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^\.htaccess$ – [F]
RewriteRule ^favicon\.ico – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

That’s all !! Restart wamp(for localhost) and enjoy restful url like:

http://localhost/aminul_fool/controller_novice/function_sumon

and for your server (aminul_fool.com)

http://www.aminul_fool.com/controller_novice/function_sumon