-->

Yii Code Snippet Collection (Yii 1 Only) part 3


Lanjutan dari yii code snippet seri ke 1 dan ke dua ini adalah seri yang ketiga semoga bermanfaat ya teman - temen langsung aja ni YiiFramework Snippet Collection part 3


Cara menggunakan Form Control di YiiFramework


  1. Cara menggunakan Dropdown list di yiiframework
    //Inside Form (View)
    <?php echo $form->dropDownList($model,'id_level_FK',CHtml::listData(MLevel::model()->findAll(), 'id_level', 'name'),array('empty'=>'Select level','style'=>'width:200px'))?>
    //Outside Form (View)
    <?php echo CHtml::dropDownList('Users[id_level_FK]','', CHtml::listData(MLevel::model()->findAll(),'id_level','name'),array('empty'=>'Select Level'))?>
  2. Cara menggunakan LinkButton di yiiframework
    <?php echo CHtml::linkButton('Approve', array(
     'submit'=>array('comment/approve','id'=>$data->id),
    )); ?> 
    <?php echo CHtml::linkButton('Delete', array(
     'submit'=>array('comment/delete','id'=>$data->id),
     'confirm'=>"Are you sure to delete comment #{$data->id}?",
    )); ?>
  3. Cara menggunakan ActiveField di YiiFramework
    <?php
    echo CHtml::activeField('User[name]',array(
     'style'=>'margin-left:30px',
     'placeholder'=>'search',
     'visible'=>Yii::app()->user->isGuest()
    );
    ?>
  4. Cara menggunakan Link di YiiFramework
    <?php echo CHtml::link('Link Text',array('controller/action')); ?> 
    <!-- <a href="index.php?r=controller/action">Link Text</a> -->
    
    
    <?php echo CHtml::link('Link Text',array('controller/action', 'param1'=>'value1')); ?>
    <!--  <a href="index.php?r=controller/action&param1=value1">Link Text</a> -->
    
    
    <?php echo CHtml::link('Link Text',array('controller/action',
           'param1'=>'value1',
           'param2'=>'value2',
           'param3'=>'value3')); ?>
    <!-- <a href="index.php?r=controller/action&param1=value1&param2=value2&param3=value3">Link Text</a> -->
    
    
    <?php echo CHtml::link('Link Text',array('controller/action',
       'param1'=>'value1'), array('target'=>'_blank'); ?>
    <!--  <a target="_blank" href="index.php?r=controller/action&param1=value1">Link Text</a> -->
    
    
    <?php echo CHtml::link('Link Text',
       array('action')
      ); ?>
    
    
    
    <?php echo CHtml::link('Link Text',
      array('/controller/action')
    ); ?>
    
    
    <?php echo CHtml::link('Link Text',
     array('/module-id/controller/action')
    ); ?>
    
    
    <?php echo CHtml::link('Link Text',
     array('/{$this->module->id}/controller/action')
    ); ?>
    
    
    <?php echo CHtml::link('Delete',"#", array(
     "submit"=>array('delete', 'id'=>$data->ID), 
     'confirm' => 'Are you sure?')
     ); ?>
    
    
    <?php echo CHtml::link('Delete',"#", array(
     "submit"=>array('delete', 'id'=>$data->ID),
     'confirm' => 'Are you sure?', 
     'csrf'=>true)
    ); ?>
    
    
    
    <?php echo CHtml::link('Delete blog post', '#', array(
     'submit'=>array('blog/deletePost', 'param'=>100),
     'params'=>array('id'=>$post->id, 'status'=>Post::STATUS_DELETED_BY_OWNER),
     'csrf'=>true,
    )); ?>
    
    
    <?php 
    CHtml::link(t("Delete"), "#", array(
    "submit"=>array("delete","id"=>"$model->id" ),
    "confirm"=>"Are you sure?")
    ); ?>
    
  5. Cara menggunakan Button di YiiFramework
    <?php echo CHtml::button('Button Text', array('submit' => array('controller/action'))); ?>
    <?php echo CHtml::button('Delete', 
    array('submit' => array('user/delete','id'=>$model->usId),
     'name'=>'btnDelete',
     'confirm'=>'Are you sure you want to delete?',
     'class'=>'btn btn-large btn-danger',
     'style'=>'width:160px;'
    ));
    ?>
  6. Cara menggunakan TextField di YiiFramework
    1. <?php echo CHtml::textField('Text'); ?>
    2. <?php echo CHtml::textField('Text', 'some value'); ?>
    3. <?php echo CHtml::textField('Text', 'some value',
      array('id'=>'idTextField', 
        'width'=>100, 
        'maxlength'=>100); ?>
    4. <?php echo CHtml::textField('Text', 'some value', 
      array('disabled'=>'disabled'); ?>
  7. Cara menggunakan ListData di YiiFramework
    1. <?php 
      /*you can use here any find method you think 
      proper to return your data from db*/
      $models = categories::model()->findAll();
     
      // format models resulting using listData     
      $list = CHtml::listData($models, 
        'category_id', 'category_name');    //array("1" => "Arts", "2" => "Science", "3" => "Culture");
     
      print_r($list);
    ?>
    2. <?php 
      $models = categories::model()->findAll(
         array('order' => 'category_name'));
     
      $list = CHtml::listData($models, 
        'category_id', 'category_name');    
     
      print_r($list);// array("1" => "Arts", "3" => "Culture", "2" => "Science");
    ?>
  8. Cara menggunakan DropdownList di YiiFramework
    1. <?php echo CHtml::dropDownList('listname', $select, 
      array('M' => 'Male', 'F' => 'Female'));?>
    2. <?php echo CHtml::dropDownList('listname', $select, 
      array('M' => 'Male', 'F' => 'Female'),
      array('empty' => '(Select a gender)'));?>
    3. <?php 
    public function getGenderOptions(){
     return array('M' => 'Male', 'F' => 'Female');
    }
    
    echo CHtml::dropDownList('listname', $select, 
       $model->genderOptions,
       array('empty' => '(Select a gender'));?>
    4. <?php // retrieve the models from db
     $models = categories::model()->findAll(
          array('order' => 'category_name'));
      
     // format models as $key=>$value with listData
     $list = CHtml::listData($models, 
         'category_id', 'category_name');
        
    // to Use
      echo CHtml::dropDownList('categories', $category, 
           $list,
           array('empty' => '(Select a category'));
    ?>
    5.  <div class="cars-select">
     <?php echo CHtml::dropDownList('Cars', 'car_id', array(
      'Mazda'=>array(
       'mazda-rx7'=>'RX7',
       'mazda-rx5'=>'RX5',
      ),
      'Volvo'=>array(
       'volvo-b9tl'=>'B9TL',
       'volvo-l90e-radlader'=>'L90E Radlader',
      ),
     )); ?>
    </div>
  9. Cara menggunakan Upload di YiiFramework
    a. di form <?php 'htmlOptions'=>array('enctype'=>'multipart/form-data'), ?>
    b. <?php echo $form->fileField($model,'profile_picture',array('size'=>60,'maxlength'=>200)); ?>
    c. <?php $model->attributes=$_POST['TUser'];
        $model->profile_picture=CUploadedFile::getInstance($model, 'profile_picture');
        if($model->save()){
         if(strlen($model->profile_picture)>0)
          $model->profile_picture->saveAs(Yii::app()->basePath.'/../upload/'.$model->profile_picture);
         $this->redirect(array('view','id'=>$model->id_user));
        } ?>
  10. Cara menggunakan Captcha di YiiFramework
    a. 
     <?php echo $form->labelEx($model,'verifyCode'); ?>
     <div>
     <?php $this->widget('CCaptcha'); ?>
     <?php echo $form->textField($model,'verifyCode'); ?>
     
    b. <?php 
     public function actions()
     {
      return array(
       // captcha action renders the CAPTCHA image displayed on the contact page
       'captcha'=>array(
        'class'=>'CCaptchaAction',
        'backColor'=>0xFFFFFF,
       ),
       // page action renders "static" pages stored under 'protected/views/site/pages'
       // They can be accessed via: index.php?r=site/page&view=FileName
       'page'=>array(
        'class'=>'CViewAction',
       ),
      );
     }
    ?> 
    c.controller: accessRules <?php  array('captcha');?>
       model : rules      <?php array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),?>
    
    

Facebook Comments

0 komentar