PHP YII Change the Row Color Based on the Column value in CGridView [rowCssClassExpression]

Belows is how you can use CGridView ‘rowCssClassExpression’ to change row color based on a column’s value: $this->widget(‘zii.widgets.grid.CGridView’, array( ‘dataProvider’=>$dataProvider, ‘rowCssClassExpression’=>'($data->column_name==0)?”new”:”old”‘, ‘columns’=>array( … ),)); You can also call a custom php function, and pass the $data variable to it. That function should return the class name for the given row 🙂

PHP, Magic_Quotes. Getting rid of magic quotes issues

This is how i get rid of magic quotes issues on my PHP projects. function array_map_r( $func, $arr ) { $newArr = array(); foreach( $arr as $key => $value ) { $newArr[ $key ] = ( is_array( $value ) ? array_map_r( $func, $value ) : ( is_array($func) ? call_user_func_array($func, $value) : $func( $value ) )… Continue reading PHP, Magic_Quotes. Getting rid of magic quotes issues

PHP, Simple export to csv tutorial for magento

Here is a very simple code you can use to generate your csv file : unlink(“upload.csv”); $handle_out = fopen(“upload.csv”, “a”); $fields_put=array(‘sku’, ‘name’, ‘price’, ‘short_description’, ‘description’, ‘attribute_set’, ‘type’, ‘status’, ‘visibility’, ‘tax_class_id’, ‘category_ids’, ‘weight’); fputcsv($handle_out, $fields_put, ‘|’ ,'”‘); while ($row = mysql_fetch_array($result)) { $description=ereg_replace( “rn”, “”, $row[‘short_description’]); $description=ereg_replace( “rt”, “”, $description); $description=ereg_replace( “r”, “”, $description); $description=ereg_replace( “t”,… Continue reading PHP, Simple export to csv tutorial for magento

PHP remove hidden characters from text

PHP remove hidden character like line breaks and tabs and hidden character like following: $description=ereg_replace( “rn”, “”, $row[‘short_description’]);$description=ereg_replace( “rt”, “”, $description);$description=ereg_replace( “r”, “”, $description);$description=ereg_replace( “t”, “”, $description);$description=ereg_replace( “n”, “”, $description);$description=ereg_replace( “xA0”, “”, $description);$description=ereg_replace( “x0B”, “”, $description);$description=ereg_replace( ‘”‘, ‘””‘, $description);  This really helps when you are creating a CSV file which has line breaks. Some softwares… Continue reading PHP remove hidden characters from text

Yii, how to add current date or time or timestamp in DB field automatically

To add current date or time or timestamp in DB field automatically Using Rules in model use it like below : <?php/** * @return array validation rules for model attributes. */public function rules(){ return array( array(‘title’,’length’,’max’=>255), array(‘title, created, modified’, ‘required’), array(‘modified’,’default’, ‘value’=>new CDbExpression(‘NOW()’), ‘setOnEmpty’=>false,’on’=>’update’), array(‘created,modified’,’default’, ‘value’=>new CDbExpression(‘NOW()’), ‘setOnEmpty’=>false,’on’=>’insert’) );}?>

YII, How to get current controller name and action name

To get current controller name use this code : <?php$controllerId = Yii::app()->controller->id;//or$controllerId = $this->getId();?> To get current action name/id being executed, if you are inside beforeAction() or afterAction(), use the received CAction argument <?php//inside beforeAction or afterActionpublic function beforeAction($action){ $actionId = $action->id;…?> or just elsewhere inside your controller <?php$actionId = $this->getAction()->getId();?> To get name of… Continue reading YII, How to get current controller name and action name

yii, handling image uploads

In Model file function rules() : <?phparray(‘product_image_1, product_image_2, product_image_3’, ‘file’, ‘types’=>’jpg, gif, png’, ‘allowEmpty’=>true),?> In View file set form like this : <div class=”form”><?php $form=$this->beginWidget(‘CActiveForm’, array( ‘id’=>’products-form’, ‘enableAjaxValidation’=>false, ‘htmlOptions’=>array(‘enctype’ => ‘multipart/form-data’))); ?> <p class=”note”>Fields with <span class=”required”>*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class=”row”> <?php echo $form->labelEx($model,’product_name’); ?> <?php echo $form->textField($model,’product_name’,array(‘size’=>60,’maxlength’=>255)); ?> <?php echo… Continue reading yii, handling image uploads