Explore Yourself !!!


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
May 22, 2009, 2:02 am
Filed under: Code Igniter | Tags: ,

$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]
May 22, 2009, 1:28 am
Filed under: Code Igniter | Tags: , ,

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

$this->load->library(‘image_lib’); //don’t use it

But if we load this library first and assign 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 make ready your configuration variables and then load the library as

$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->load->library(‘image_lib’, $config);
if ( ! $this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}

now it should work fine without any error in CI.



.htaccess file for server and localhost
April 25, 2009, 10:24 am
Filed under: Code Igniter | Tags: , ,

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



The MVC Architecture & Layout(Decorator Design Pattern)

[Note: 100% copy and paste from symfony web site]
If you are used to developing PHP websites without a framework, you probably use the one PHP file per HTML page paradigm. These PHP files probably contain the same kind of structure: initialization and global configuration, business logic related to the requested page, database records fetching, and finally HTML code that builds the page.
You may use a templating engine to separate the logic from the HTML. Perhaps you use a database abstraction layer to separate model interaction from business logic. But most of the time, you end up with a lot of code that is a nightmare to maintain. It was fast to build, but over time, it’s more and more difficult to make changes, especially because nobody except you understands how it is built and how it works.
As with every problem, there are nice solutions. For web development, the most common solution for organizing your code nowadays is the MVC design pattern. In short, the MVC design pattern defines a way to organize your code according to its nature. This pattern separates the code into three layers:
• The Model layer defines the business logic (the database belongs to this layer). You already know that symfony stores all the classes and files related to the Model in the lib/model/directory.
• The View is what the user interacts with (a template engine is part of this layer). In symfony, the View layer is mainly made of PHP templates. They are stored in varioustemplates/ directories as we will see later on today.
• The Controller is a piece of code that calls the Model to get some data that it passes to the View for rendering to the client. When we installed symfony the first day, we saw that all requests are managed by front controllers (index.php and frontend_dev.php). These front controllers delegate the real work to actions.

MVC Architecture

MVC Architecture

The Layout

First, if you have a closer look at the mockups, you will notice that much of each page looks the same. You already know that code duplication is bad, whether we are talking about HTML or PHP code, so we need to find a way to prevent these common view elements from resulting in code duplication.
One way to solve the problem is to define a header and a footer and include them in each template:

Header Footer

Template using Header Footer

But here the header and the footer files do not contain valid HTML. There must be a better way. Instead of reinventing the wheel, we will use another design pattern to solve this problem: the decorator design pattern. The decorator design pattern resolves the problem the other way around: the template is decorated after the content is rendered by a global template, called a layout in symfony:

Decorator Design Pattern

Decorator Design Pattern



How simple to create an excel file from database using CI
December 11, 2008, 12:08 pm
Filed under: Code Igniter | Tags: ,

How simple to create an excel file from database using CI (don’t care about table(s) or data)
function export_excel()
{
$query = $this->db
->select(‘*’)
->from(‘customer’)
->get();
to_excel($query , ‘export_master’); // outputs export_master.xls
}

system/helper/common_helper.php
or
application/helper/common_helper.php
field_data();
if ($query->num_rows() == 0) {
echo ‘

The table appears to have no data.

‘;
return FALSE;
} else {
foreach ($fields as $field) {
$headers .= $field->name . “\t”;
}

foreach ($query->result() as $row) {
$line = ”;
foreach($row as $value) {
if ((!isset($value)) OR ($value == “”)) {
$value = “\t”;
} else {
$value = str_replace(‘”‘, ‘”"‘, $value);
$value = ‘”‘ . $value . ‘”‘ . “\t”;
}
$line .= $value;
}
$data .= trim($line).”\n”;
}

$data = str_replace(“\r”,”",$data);

header(“Content-type: application/x-msdownload”);
header(“Content-Disposition: attachment; filename=$filename.xls”);
echo “$headers\n$data”;
return TRUE;
}
}
?>
[NOTE/DISCLAIMER: 100% copy and paste from http://codeigniter.com/wiki/Excel_Plugin/ ]



Power of CI 1.7 [More Powerful MVC]
October 29, 2008, 11:42 pm
Filed under: Code Igniter

Code igniter 1.7 version is really make programmer’s life more easier. Save a lot of time and made more tricky validation stuffs. moreover, control all validation of entire site from a single page.

The real power of this version is validation. How? By an example you have 30 forms in your e-commerce site and you add one field to feedback form. You not need to write a single line of code in your controller and model. Impossible? Not at all if you formerly write your model in a good manner. Which mean if you named all fields same(database table field name and html control name). So what you need is only add one field in your form_validation.php file and add additional field in your view file. that’s it.

Lets draw an example You want to add visitors contact number in contact us form. Add the following line in your form_validation.php

array(‘field’ => ‘contact_number’,'label’ => ‘contact number’,'rules’ => ‘required’),

and a text box in your view file which might locate at view/contact/contact_us.php

[input type="text" name="contact_number" value="<php echo set_value('fr', $default['fr']); >”]

that’s all. :) you have not opened your controller and model file. Real power of MVC :)



Codeigniter 1.7 released finally
October 26, 2008, 7:40 pm
Filed under: Code Igniter | Tags: , ,

This is a greate news for CI(Code Igniter) community that, CI 1.7 released finally. With many more features and corrections as earlier releases. But this version have some exciting new features. For example validation is one of them. Previously, validation takes number of lines of code to control which is now less amont of lines. Moreover, most powerful thing is that, you don’t need to write validation codes in every single page of your application. Simply one validation page is totally concern with all the validations. It’s really impressive one. Really impressive which take your lesser amount of time to develop an application.

Using PHP version >5 CI 1.7 is now more effective and rich framework then last time. In addition, Code Igniter always made user guide builtin in their package. To know the latest updates and how to utilize the power read user guide with full of your concentration. Why not have a look ?
Visit: http://www.codeigniter.com 
Download code igniter 1.7 and extract it. Now you have powerful framework with user guide. So no way to prevent you :)

Best of luck with CI 1.7



Hello world in Code Igniter !!
October 16, 2008, 1:10 am
Filed under: Code Igniter

Welcome to code igniter. The easiest PHP framework of PHP(although it take more time to learn for me ;) ). Whatever its’ a new language or framework developer dont’ care if they able to run  successfully ”hello world”. So let’s try quick to run our hello world application using CI(Code Igniter).

Well first you need a fresh copy of codeigniter what you can downlaod from codeigniter site. For instance click here to donwlad a fresh copy of Code Igniter. click me

Huh!! You have a zip copy of CI. Please extract it and put it into your root (www/htcdocs) directory. Rename extracted folder as ci_power (for example) or whatever you like according to your project name.

Nice!! first try to run your application. GIve it a try http://localhost/ci_power or simply  http://localhost/ci_power/index.php

Okay!! You are watching Welcome to code igniter. Lets thing where from it came and what’s going under the hood? CI have some configuration files where from you can set which controller will run first. By default CI set to welcome(file named system/application/controller/welcome.php) controller. Moreover, welcome controller loads welcome view file located in system/application/views/welcome_message.php

hhhmmm Now let us think, where shall we put our simple PRINT command hello world? It can be placed either in view file or controller. It’s very easy solution. We like to create our own controller and put our world inside. Lets do that. For simplycity copy welcome.php and paste it as mystuff.php. Now open the file and change Welcome by Mystuff (keep in mind first letter must must must be in uppercase).

Excellent!! You are almost successful. Write a single line of code echo “hello world”; inside index() function. Yep. You are totally ready. But where is the world? Here it is: http://localhost/ci_power/mystuff/

hello world!!

Ah!!! You are now an expert of CI like me ;)