Sunday, 29 May 2011

PHP Multilanguage Site Using Arrays

PHP Multilanguage

Here we can develop multilanguage site with the use of arrays

So, here we go:

First of all we need a configuration setting inside our script’s config.php file. For the sake of our example, we’ll add a manual variable change:

// language settings
$lng = 'en';

Next, our header file, dubbed header.php (could be head.php or top.php in your script) will contain these lines:

include('includes/config.php');
include('languages/'.$lng.'.php');

You now, obviously, have to create a languages directory and create a new file called en.php. This file will hold our array of words and expressions:

/*
@Package: My Multilanguage Script
@Language: English (English)
*/

$lang = array();

$lang['REGISTER'] = 'Register';
$lang['USERNAME'] = 'Username';
$lang['PASSWORD'] = 'Password';
$lang['LOGIN'] = 'Log in';
$lang['LOGOUT'] = 'Log out';
$lang['DASHBOARD'] = 'Dashboard';

Notice how I tried to keep the array index name as close to translation as possible. For example, you’ll have the string “Separate tags with commas” as $lang['SEPARATE_TAGS_COMMAS']. It’s easier after a couple of months when you’ll make changes.

Also, try to keep consistent naming of your language files, such as fr.php, de.php, ru.php, cn.php.

Now, call in you script . It will display “Register”, just as you translated it in your language file.

Scrolling div with Javascript

You can scroll div with javascript. You need to have one mainContainer which will contain the child for it. eg.

I will have mainArea div with child div myReport.







You need to fixed the height for the mainArea div with style







Now your div can scroll, You now need to write one line code in any javascript function

function scrollMainArea()
{
document.getElementById(“mainArea”).scrollTop = 0;
}

This will move your myReport div to show the top portion of your division.

Thursday, 5 May 2011

Zend Framework: Storing session data in database

The reason behind saving session data in database is that data become more secure and can be easily retrieved later on.
Let’s look how simple and easy it is to save session data in database using Zend_Session_SaveHandler_DbTable in Zend Framework.
1. Creating database table for holding session data
Before writing code you will need to create a table in the database for holding session data. Execute the following query for this purpose.
CREATE TABLE `session` (
`id` char(32),
`modified` int,
`lifetime` int,
`data` text,
PRIMARY KEY (`id`)
);

2.Making necessary configuration in your bootstrap file.
You will only need only few lines of code in your bootstrap file.
After making database configuration in your bootstrap file, write the following code.
$config = array(
'name' => 'session',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);
Zend_Session::setSaveHandler(new
Zend_Session_SaveHandler_DbTable($config));
Zend_Session::start();

In the $config array we tell the name of the table that will store the session information, primary key of the table, column that will store the session modification timestamp(*nix timestamp), column that will store the session data, and the column that save the information about how long the session will last.

After defining this configuration array we call setSaveHandler() method- static method, of the Zend_Session. This setSaveHandler() method accept Zend_Session_SaveHandler_DbTable object. Zend_Session_SaveHandler_DbTable take configuration array as an argument.

We have now done all the necessary configuration. The only thing we will need to do now is to start our session. Session can be started in the bootstrap file, however it’s not necessary for the session to be started here. You can start it in any controller you needed.

Anywhere in your controller, write
$namespace = new Zend_Session_Namespace();
$namespace->name = "Viral";

and check your session table. The table will not only hold session id, but also the modified timestamp of when session was modified, life time of the session and the session data in special format.
Try it. Its really cool.

Zend Framework SQL Joins Examples

You may have custom of using advanced queries. It often requires writing complex queries if you are working on enterprise, large scale web application(s).
The use of joins can never be ignored.
Zend Framework developers have done tremendous job by providing simple method for implementing joins.
Lets look some examples of different type of joins.
Before discussing joins lets consider we have two tables, “authors” and “books”.
These are associated with author_id.

1. Inner Join

The simplest query will be
$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinInner('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->order('column name ASC/DESC');

2. Left Join

$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinLeft('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');

3. Right Join

$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinRight('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');

4. Full Join

$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');

5. Cross Join

$select = $this->_db->select()
->from('books',array('col1','col2'…..))
->joinFull('authors','books.id=authors.bks_id',array('col1','col3'…))
->where('where condition here')
->group('group by column name here')
->order('column name ASC/DESC');

Once you write these queries, you can fetch a single row or multiple rows as

Wednesday, 4 May 2011

How to Charm Your Way Through Life

Charismatic people are those who have the ability to inspire and influence others. Such people often have an aura of magnetism surrounding them.

However, Charisma is not a magical power and can be acquired by anyone who wishes to. Following are the suggestions which can help you build up the charismatic factor in you.

Find Ease in Yourself
In order to feel charismatic, you need not incorporate any artificial swagger or machismo. You just have to feel comfortable in your own skin and emphasize your real self with others as well.

No Bargain
While trying to maintain their self-assertion, people often come across as aggressive. This, however, does not mean that you hide your individuality. You should find the ability to draw a line between assertion and aggression.

Accept Your Flaws
Charismatic people never fail to compliment someone or embrace their own weaknesses. Similarly, you should never shy away from admitting your mistakes. It not only increases your charm quotient but also makes you a better person.

Be Full of Life
Contrary to the pre-conceived notion that charming people have to be intelligent or witty, it is the good-humouredness of a person that can more hearts. So, don't try too hard to be too scholarly; just go ahead and have fun with the people around you.

Be Aware of Yourself
The key to becoming a charming person is to know yourself and your beliefs as it reflects in personality. A person who is unsure of himself fails to strike a positive impact on others. Discover yourself and let the light of your self-awareness shine upon others too.

Practice Makes Perfect
Being charismatic is a skill which which everyone can learn. You cannot expect to wake up one morning as the most charismatic individual on the planet just because you desired so. It has to be built over time and with precision. After all, nothing comes easy in life.