Monday 17 September 2012

php array append

PHP: append an element to the end of the array


NoteIf you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
---- http://php.net/manual/en/function.array-push.php

Example:

$elements = array(1,2,3,4,5,6);

foreach ($elements as $elem)
    $newArray[] = $elem;

print_r($newArray);
//sample output: Array ( [0] => 1[1] => 2[2] => 3[3] => 4...)

Yii findbyattributes using IN Clause

How to use SQL IN clause based on Yii findbyattributes?

Solution:


put an array as an value for an specific attribute:
$model=new Model();
$result=$model->findAllByAttributes(array('Id'=>array(6,7,8,9)));

Sunday 16 September 2012

Yii clientScript registerScript (example)

Function : Generate a piece of specific js script.

Example:

HTML: 
we have a button: 

<!--  {'func':'abc','par':'123'} is JSON format (key:value)! -->

<input type="button" onclick="myFunction({'func':'abc','par':'123'})" value='click me'/>

PHP ajax:


<?php 
//Specify an ajax handler: 
//Controller : Ajax, Action: handler $ajaxUrl = $this->createUrl('ajax/handler'); 
$script = "function  myFunction (param){  
           $.ajax({  
                    'type': 'GET',  
                    'url': '$ajaxUrl',  
                    'data': param,
//!!!!!if returned results are not json formatted, please delete dataType !!!!!!.
                    'dataType', 'json'  
                    'success': function(msg){
                               //success code put here!  
                               }";

//register the script(generate the js).
// CClientScript::POS_END: gen js code after the html page is fully loaded. 
//the position of the JavaScript code. Valid values include the following:
      • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
      • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
      • CClientScript::POS_END : the script is inserted at the end of the body section.
      • CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
      • CClientScript::POS_READY : the script is inserted in the jQuery's ready function.


Yii::app()->clientScript->registerScript('updates', $script,        CClientScript::POS_END);
?>

PHP controller:

<?php 
class AjaxController extends Controller
{
public $defaultAction = 'Handler';
public function actionHandler()
{
    $func = $_GET['func'];
    $param = $_GET['par'];
}
}
?>

For more info, about CClientScript : http://www.yiiframework.com/doc/api/1.1/CClientScript

http://daxue.menggy.com