July 20 2013

Protect PHP class file that must be include

Tagged Under : ,

php
Here we describe how we can protect our class file that calling with include function, but it was not secure and its have big risk. For the solution if we can make the file cannot execute or calling when it not include by PHP file.

Why we need it? because when someone try calling http://localhost/module/foo.class.php it will be successful and maybe some accident will happen here.

if( basename( __FILE__ ) == basename( $_SERVER['PHP_SELF'] ) ) exit();

June 25 2013

MySQL sql duplicate insert update

Tagged Under : ,

MySQL
Duplicate row found and then update the row by one sql statement. This can be done by SQL, and not need any others server-side scripting.

Now let us see how it work. First, create table and insert some example data.
CREATE TABLE IF NOT EXISTS `foo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
);

INSERT INTO `foo` (`id`, `name`) VALUES
(1, 'foo'),
(2, 'foo2');

June 23 2013

Yii CGridView auto increase row number

Tagged Under :

Yii
Below example showing you how to add the row number in cgridview widgets.

You just need add the following script inside the array columns.
array(
	'header' => 'No.',
	'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),

The completed example as below:

June 23 2013

Yii include/preload yiigridview javascript

Tagged Under :

Yii
Normally we include Javascript in our application in Yii is below script
Yii::app()->getClientScript()->registerScriptFile('jquery.js');	
But, now we need include yiigridview.js script file in our application. Because it was generate by Yii and it located at assets folder. So above example cannot used.

In here, we need to call getAssetManager() to get the assets folder url and folder name generated by Yii.
Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('zii.widgets.assets'));
The above example will get the gridview path from the assets folder.

June 21 2013

PHP shorthand If / Else

Tagged Under :

php
PHP shorthand is something that even if you don’t use it yourself, you’re sure to come across it elsewhere.
For Example:
if ($age > 12) {
   $result = 'Young';
} else {
   $result = 'Adult';
}
You can write it as:
$result = $age > 12 ? 'Young' : 'Adult';

And I also give some example at below:

May 13 2013

Yii CGridView listdata with CArrayDataProvider and Pagination

Tagged Under :

Yii
Following last post CGridview listdata with CArrayDataProvider to list down the data with cgridview.

But it just can listing the records without the pagination. Use back last post example script and then modify the CArrayDataProvider as below example.

May 13 2013

Yii CGridView listdata with CArrayDataProvider

Tagged Under :

Yii
Yii, use CArrayDataProvider to listing out the array with CGridView widgets.

Below example are showing how to use it.
Data array:
$data = array();
$data[] = array('id' => 1, 'name' => 'name 1', 'age' => 22, 'date' => '1-Jan-2013');
$data[] = array('id' => 2, 'name' => 'name 2', 'age' => 25, 'date' => '1-Feb-2013');
$data[] = array('id' => 3, 'name' => 'name 3', 'age' => 22, 'date' => '1-Mac-2013');
$data[] = array('id' => 4, 'name' => 'name 4', 'age' => 28, 'date' => '1-Jan-2013');
$data[] = array('id' => 5, 'name' => 'name 5', 'age' => 23, 'date' => '1-Apr-2013');
$data[] = array('id' => 6, 'name' => 'name 6', 'age' => 29, 'date' => '1-May-2013');
$data[] = array('id' => 7, 'name' => 'name 7', 'age' => 21, 'date' => '1-Jan-2013');
$data[] = array('id' => 8, 'name' => 'name 8', 'age' => 20, 'date' => '1-Feb-2013');

May 11 2013

Mysql better way to select random records

Tagged Under : ,

MySQL
The easiest way to generate random records in Mysql is to use the ORDER BY RAND().
SELECT col1 FROM tbl ORDER BY RAND() LIMIT 1;
This can work fine for small tables. However, for big table, it will have a serious performance problem as in order to generate the list of random rows, because Mysql need to assign random number to each row and then sort them.

Even if you only want random 1 records from 200k+ rows, Mysql need to sort all the records and then extract it.

March 10 2013

PHP is_serialized function

Tagged Under :

php
PHP unserialize function is use to convert an array string to an array.

But if the string not in proper array string it will return Notice error like below:
Notice: unserialize() [function.unserialize]: Error at offset 0 of 6 bytes

The easy way to fix this problem is write a function and call it “is_serialized”, use to check the string before unserialize it.

March 10 2013

Yii row number in CGridView

Tagged Under :

Yii
In Yii CGridView it doesn’t provide absolute row number in all the pages.

The easy way is write the a calculation method inside the column to show the column number.