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



One shot symfony install
April 11, 2009, 10:53 pm
Filed under: Symfony | Tags: ,

First install WAMP. you can also choose XAMP but for me it’s WAMP.

to install symfony you need to enable or install pear. follow this link in order to do it:

http://trac.symfony-project.org/wiki/HowToInstallPearOnWindowsWithWamp

REBOOT PC

second step is to install symfony
http://trac.symfony-project.org/wiki/HowToInstallSymfonyOnWindowsWithWamp

REBOOT PC

Dont worry if your C:\>symfony does not show anything. it’s important to make sure that, there are no error.

Now you are ready to start symfony. First download symfony package from http://symfony-project.org/ or theiir SVN. I prefer svn one.

Enjoy Symfony!!!!



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



Build a small project
January 13, 2009, 11:52 pm
Filed under: Symfony | Tags: , , ,

well here we are for build second symfony project. previously we have seen, how difficult first project was without a single line of php code. here is the second one similar to first project. but this time not only signp form. instead we implement a small project. lets start
propel:
article:
id: ~
title: { type: varchar(255), required: true }
slug: { type: varchar(255), required: true }
content: longvarchar
status: varchar(255)
author_id: { type: integer, required: true, foreignTable: author, foreignReference: id, OnDelete: cascade }
category_id: { type: integer, required: false, foreignTable: category, foreignReference: id, onDelete: setnull }
published_at: timestamp
created_at: ~
updated_at: ~
_uniques:
unique_slug: [slug]

author:
id: ~
first_name: varchar(20)
last_name: varchar(20)
email: { type: varchar(255), required: true }
active: boolean

category:
id: ~
name: { type: varchar(255), required: true }

tag:
id: ~
name: { type: varchar(255), required: true }

article_tag:
article_id: { type: integer, foreignTable: article, foreignReference: id, primaryKey: true, onDelete: cascade }
tag_id: { type: integer, foreignTable: tag, foreignReference: id, primaryKey: true, onDelete: cascade }

now goto command prompt and run few commands we already practice yesterday:

  • from DOS prompt d:\dev\small_project> php symfony propel:build-all
  • from DOS prompt d:\dev\small_project> php symfony propel:generate-crud frontend article Article
  • from DOS prompt d:\dev\small_project> php symfony propel:generate-crud frontend author Author
  • from DOS prompt d:\dev\small_project> php symfony propel:generate-crud frontend category Category
  • that’s it. readymade module for you. browse your project using:
    http://loclhost:420/author

    Hey!! i have no idea which client like this project ;) but yes developer might like it for customization and build a genuine one :)



Build your first symfony project
January 11, 2009, 9:41 am
Filed under: Symfony | Tags: , , , ,

Good time to build first simple symfony project. The project is very simple one. Build a Signup form. Don’t blame me, how a signup form can be a project !!!. Yes it is :) Lets Start.

Before start follow the instructions make sure you have successfully configured symfony in your local PC. which mean, your PHP, MySql and Apache are perfectly installed. Moreover, PEAR library is also installed and you have enabled PHP CLI (Command Line Instruction). Fair enough :) you have all these installed and support. So lets get start!!!

First create a folder with your project name. for example, for this tutorial we name the project signup. Say we create a folder at d:\dev\signup and we create two sub folder named lib and vendor. which mean the directory structure is exactly d:\dev\signup\lib\vendor. okay !! now donwload symfony latest stable package from here and extract it inside d:\dev\signup\lib\vendor. rename the extract folder to symfony. which mean our structure is like D:\dev\signup\lib\vendor\symfony\ and at this location we get some folders (data, doc, lib, etc etc).

Hang ON!! Don’t dive!! Before that, we need to analyze what exactly we are going to do. we have to build a signup page and a success page. That’s all !!!! Signup Form: only 3 fields: name, email and sex(male or female). as though you all are quite mature lets define specific. we use html text box for name and email and radio button for sex. Success Form: simply a message. Operation Successful !! Very easy requirement for us. if we start write raw coding then it must not take more then 20 minutes of us. Right? Let’s see how long symfony take for this tiny requirement.

LETS DIVE !! DIVE !! DIVE !! DIVE !! DIVE !! DIVE !!

  • Goto Start->Run->cmd press enter
  • navigate to d:\dev\signup
  • Setup your PROJECT by d:\dev\signup>php lib\vendor\symfony\data\bin\symfony generate:project signup =) lot of files automatically generated for you :)
  • Create frontend application using d:\dev\signup>php symfony generate:app –escaping-strategy=on –csrf-secret=Unique$ecret frontend =) lot of file again automatically generated for you at d:\dev\signup\app\frontend :)

Upto this step these are common for any types of projects like facebook.com or ebay.com. :) Lets dive into symfony to build our HUGE signup project. Till now we have not write a single line of code and it’s time for us to do that. Let’s remember our requirement. it was a signup form with only 3 fields. This is the most most most tricky one. Lot’s of class, form, validation etc etc will create automatically based on our object signup. So lets build our object first. How? very easy


// D:\dev\signup\config\schema.yml
propel:
member: id: ~ name: { type: varchar(255), required: true }
email: { type: varchar(255), required: true }
sex: { type: varchar(255), required: true }

and customize your database file

// D:\dev\signup\config\databases.yml
dev:
propel:
param:
classname: DebugPDO
test:
propel:
param:
classname: DebugPDO
param:
classname: DebugPDO
dsn: 'mysql:host=localhost;dbname=signup_test'
username: root
password: null
all:
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
dsn: mysql:dbname=signup_dev;host=localhost
username: root
password:
encoding: utf8
persistent: true
pooling: true

Is the class, form etc etc created by writing only these above line of code? It’s impossible. Yes it’s really impossible until you run few lines of code at command prompt. Lets do that.

  • first create create two database named signup_dev and signup_test from your browser http://localhost/phpmyadmin
  • now from command promt: d:\dev\signup>php symfony propel:build-all automatically generate lot of classes for you inside d:\dev\signup\lib\. don’t get any file? don’t worry look inside model, form, filter folder :D

i hope you have fair experience of PORT BASED SETUP. if No then you are most welcome :) . simply add these lines at the end of your configuration file apache\conf\httpd.conf


Listen 127.0.0.1:8081
DocumentRoot "D:\dev\signup\web" DirectoryIndex index.php
AllowOverride All Allow from All
Alias /sf D:\dev\signup\lib\vendor\symfony\data\web\sf
AllowOverride All Allow from All

Run http://localhost:8081/frontend_dev.php what comes in browser? “Symfony Project Created”. right? which mean’s you are Successful at first stage!! YEAAAAAAA !! Lets celebrate!!

Well let’s move forward.
From command promt: d:\dev\signup>php symfony generate:module frontend member
Have a look inside app\frontend\module. A ready made module named member for us :)
another one: d:\dev\signup>php symfony propel:generate-crud frontend member Member

That’s it!!! Member module is 100 % complete. which means you can add, edit, update, delete a member with proper VALIDATION. did you write a single line of php code? i don’t think so. browse

http://localhost:8081/member
default display a list of members and option for add new one, edit existing one, delete or update any one.

hope you have not enjoyed. because either you know symfony very well, then this article is waste of time for you :D in contrast, no knowledge of symfony means this article is not fair enough to understand. i am afraid but you need to http://www.symfony-project.org. which means waste of time with this article :D

BEST OF LUCK :)



build first symfony project
January 11, 2009, 8:09 am
Filed under: Symfony | Tags: , ,

Good time to build first simple symfony project. The project is very simple one. Build a Signup form. Don’t blame me, how a signup form can be a project !!!. Yes it is :) Lets Start.

Before start follow the instructions make sure you have successfully configured symfony in your local PC. which mean, your PHP, MySql and Apache are perfectly installed. Moreover, PEAR library is also installed and you have enabled PHP CLI (Command Line Instruction). Fair enough :) you have all these installed and support. So lets get start!!!

First create a folder with your project name. for example, for this tutorial we name the project signup. Say we create a folder at d:\dev\signup and we create two sub folder named lib and vendor. which mean the directory structure is exactly d:\dev\signup\lib\vendor. okay !! now donwload symfony latest stable package from here and extract it inside d:\dev\signup\lib\vendor. rename the extract folder to symfony. which mean our structure is like D:\dev\signup\lib\vendor\symfony\ and at this location we get some folders (data, doc, lib, etc etc).

Hang ON!! Don’t dive!! Before that, we need to analyze what exactly we are going to do. we have to build a signup page and a success page. That’s all !!!!

Signup Form: only 3 fields: name, email and sex(male or female). as though you all are quite mature lets define specific. we use html text box for name and email and radio button for sex.
Success Form: simply a message. Operation Successful !!

Very easy requirement for us. if we start write raw coding then it must not take more then 20 minutes of us. Right? Let’s see how long symfony take for this tiny requirement.

LETS DIVE !! DIVE !! DIVE !! DIVE !! DIVE !! DIVE !!

  • Goto Start->Run->cmd press enter
  • navigate to d:\dev\signup
  • Setup your PROJECT by d:\dev\signup>php lib\vendor\symfony\data\bin\symfony generate:project signup
    =) lot of files automatically generated for you :)
  • Create frontend application using d:\dev\signup>php symfony generate:app –escaping-strategy=on –csrf-secret=Unique$ecret frontend
    =) lot of file again automatically generated for you at d:\dev\signup\app\frontend :)

Upto this step these are common for any types of projects like facebook.com or ebay.com. :) Lets dive into symfony to build our HUGE signup project.

Till now we have not write a single line of code and it’s time for us to do that. Let’s remember our requirement. it was a signup form with only 3 fields. This is the most most most tricky one. Lot’s of class, form, validation etc etc will create automatically based on our object signup. So lets build our object first. How? very easy
// D:\dev\signup\config\schema.yml
propel:
member:
id: ~
name: { type: varchar(255), required: true }
email: { type: varchar(255), required: true }
sex: { type: varchar(255), required: true }

and customize your database file
// D:\dev\signup\config\databases.yml
dev:
propel:
param:
classname: DebugPDO

test:
propel:
param:
classname: DebugPDO
param:
classname: DebugPDO
dsn: 'mysql:host=localhost;dbname=signup_test'
username: root
password: null

all:
propel:
class: sfPropelDatabase
param:
classname: PropelPDO
dsn: mysql:dbname=signup_dev;host=localhost
username: root
password:
encoding: utf8
persistent: true
pooling: true

Is the class, form etc etc created by writing only these above line of code? It’s impossible. Yes it’s really impossible until you run few lines of code at command prompt. Lets do that.

  • first create create two database named signup_dev and signup_test from your browser http://localhost/phpmyadmin
  • now from command promt: d:\dev\signup>php symfony propel:build-all
    automatically generate lot of classes for you inside d:\dev\signup\lib\. don’t get any file? don’t worry look inside model, form, filter folder :D

i hope you have fair experience of PORT BASED SETUP. if No then you are most welcome :) . simply add these lines at the end of your configuration file apache\conf\httpd.conf

Listen 127.0.0.1:8081

DocumentRoot "D:\dev\signup\web"
DirectoryIndex index.php

AllowOverride All
Allow from All

Alias /sf D:\dev\signup\lib\vendor\symfony\data\web\sf

AllowOverride All
Allow from All


now try to run http://localhost:8081/frontend_dev.php what comes here? “Symfony Project Created”. right? which mean’s you are Successful at first stage!! YEAAAAAAA !! Lets celebrate!!