Wednesday 24 September 2014

Date Compare in YII model search function


View : Search form

label($model,'fromdate'); ?> widget('zii.widgets.jui.CJuiDatePicker',array( 'id'=>'HolidayHolidayrange_fromdate', 'name'=>'HolidayHolidayrange[fromdate]', 'value'=>$model->fromdate==''?'':date('d-m-Y',strtotime($model->fromdate)), 'options'=>array( 'dateFormat' => 'dd-mm-yy', 'showAnim'=>'slide', 'showButtonPanel'=>true, 'changeMonth'=>true, 'changeYear'=>true, 'yearRange'=>'1900:'.date('Y'), ), 'htmlOptions'=>array( 'style'=>'', 'placeHolder'=>'End Date' ), )); ?>
label($model,'todate'); ?> widget('zii.widgets.jui.CJuiDatePicker',array( 'id'=>'HolidayHolidayrange_todate', 'name'=>'HolidayHolidayrange[todate]', 'value'=>$model->todate==''?'':date('d-m-Y',strtotime($model->todate)), 'options'=>array( 'dateFormat' => 'dd-mm-yy', 'showAnim'=>'slide', 'showButtonPanel'=>true, 'changeMonth'=>true, 'changeYear'=>true, 'yearRange'=>'1900:'.date('Y'), ), 'htmlOptions'=>array( 'style'=>'', 'placeHolder'=>'End Date' ), )); ?>
Model's Search function
public function search()
 {
           ....................
        if(!empty($this->fromdate))  
        $criteria->addCondition('fromdate > "'.date("Y-m-d",strtotime($this->fromdate)).'" ');
   
        if(!empty($this->todate))
        $criteria->addCondition('todate < "'.date("Y-m-d",strtotime($this->todate)).'" ');
         .......................
       }

Tuesday 23 September 2014

Dropdown Filter in CGridView

$month_list=array('1'=>'JAN','2'=>'FEB');
$this->widget('zii.widgets.grid.CGridView', array(
 'id'=>'form-grid',
 'dataProvider'=>$model->search(),
 'filter'=>$model,
 'columns'=>array(
        array(
            'name'=>'festivals.festival_name',
            'filter' => CHtml::activeTextField($model, 'festival_name'),
            ),      
  'day',
        array(
            'name'=>'month',
             'value'=>function($data,$row) use ($month_list){
                            return $month_list[$data->month];
                        },
             'filter'=>CHtml::activeDropDownList($model,'month',$month_list,array('empty'=>'Select Month')),
            ),
...........

Thursday 29 May 2014

Multiple Row Updates in MySQL

CREATE TABLE  `testimonial_setting` (
  `testimonial_setting_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `testimonial_label` varchar(255) NOT NULL,
  `testimonial_key` varchar(45) NOT NULL,
  `testimonial_value` varchar(45) NOT NULL,
  PRIMARY KEY (`testimonial_setting_id`)
);

Table :-  testimonial_setting
testimonial_setting_id, testimonial_label,    testimonial_key, testimonial_value
            1,                       'Background Color',    'bgcolor',            ''
            2,                        'Text Color',               'textcolor',          ''
            3,                      'Heding Text Color',    'headtextcolor',    ''
            4,               'Heding Background Color', 'headbgcolor',    ''

Query :-

UPDATE `testimonial_setting` SET
`testimonial_value` = IF(`testimonial_key`="bgcolor","#ff00ff",`testimonial_value`),
`testimonial_value` = IF(`testimonial_key`="textcolor","#ffffff",`testimonial_value`),
`testimonial_value` = IF(`testimonial_key`="headtextcolor","#ff0000",`testimonial_value`),
`testimonial_value` = IF(`testimonial_key`="headbgcolor","#ff0fff",`testimonial_value`);

After update table :-  testimonial_setting


testimonial_setting_id, testimonial_label,    testimonial_key, testimonial_value
            1,                       'Background Color',    'bgcolor',            '#ff00ff'
            2,                        'Text Color',               'textcolor',          '#ffffff'
            3,                      'Heding Text Color',    'headtextcolor',    '#ff0000'
            4,               'Heding Background Color', 'headbgcolor',    '#ff0fff'

Monday 24 February 2014

Import a SQL file using the command line in MySQL

A common use of mysqldump is for making a backup of an entire database:

  shell> mysqldump db_name > backup-file.sql

 You can load the dump file back into the server like this:

  UNIX
shell> mysql db_name < backup-file.sql

  Same in Windows comand prompt
mysql -p -u[user] [database] < backup-file.sql

  PowerShell
C:\> cmd.exe /c "mysql -u root -p db_name < backup-file.sql"

  MySQL command line
mysql> use db_name; mysql> source backup-file.sql;

Saturday 1 February 2014

PHP find value by index in multidimensional/associative array

Following function has easily to find and retrieve value by index from multidimensional/associative array
function array_key_exists_value($searchkey, $assocarray)
{
 $output='';
    $result = array_key_exists($searchkey, $assocarray);
    if ($result)
 {
  $output =$assocarray[$searchkey];
 }
else
{ 
    foreach ($assocarray as $val) {
        if (is_array($val)) {
            $output = array_key_exists_value($searchkey, $val);
        }
    }
} 
    return $output;
}

Convert a PHP Object to an Array

Following function has easily to convert an associative array from object data


function object_to_array($result) 
{ 
    $arraydata = array(); 
    foreach ($result as $key=>$value) 
    { 
        if (is_object($value)) 
        { 
            $arraydata [$key]=object_to_array($value); 
        } 
        elseif (is_array($value)) 
        { 
            $arraydata [$key]=object_to_array($value); 
        } 
        else 
        { 
            $arraydata [$key]=$value; 
        } 
    } 
    return $arraydata ; 
}  

Wednesday 8 January 2014

Uncaught TypeError: Object [object Object] has no method 'live'

Uncaught TypeError: Object [object Object] has no method 'live'
.live() is a deprecated function (from 1.7+) and removed completely from jQuery 1.9+.

You can instead use .on() or .bind() methods:

http://api.jquery.com/on/
http://api.jquery.com/bind/

Tuesday 7 January 2014

Parent child relationship mysql query

Create table like this :-

CREATE TABLE `nodeslist` (
  `NodeID` mediumint(9) NOT NULL AUTO_INCREMENT,
  `NodeTypeID` tinyint(3) unsigned NOT NULL,
  `NodeCode` char(20) NOT NULL,
  `NodeLocationName` char(75) NOT NULL,
  PRIMARY KEY (`NodeID`),
) ;

Here this query to select tree struture data :-

 SELECT
    p.NodeID as parent_id,
    c1.NodeID as child_id_1,
    c1.ParentNode as parent_id_1,
    c2.NodeID as child_id_2,
    c2.ParentNode as parent_id_2,
    c3.NodeID as child_id_3,
    c3.ParentNode as parent_id_3
FROM
    nodeslist p
LEFT JOIN nodeslist c1
    ON c1.ParentNode = p.NodeID
LEFT JOIN nodeslist c2
    ON c2.ParentNode = c1.NodeID
LEFT JOIN nodeslist c3
    ON c3.ParentNode = c2.NodeID
WHERE
    p.ParentNode=0;

Select all child and subchild for particular Parent ID:-

SELECT NodeID,ParentNode,NodeTypeID
FROM nodeslist
WHERE NodeID = 1
OR ParentNode = 1
OR ParentNode IN (SELECT NodeID
    FROM nodeslist
    WHERE ParentNode = 1 OR
    ParentNode IN (SELECT NodeID
    FROM nodeslist
    WHERE ParentNode = 1)

  );