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: