Tuesday 14 June 2016

algo write a bulk of data into a file faster and more efficient

//we only call fwrite() when the num of lines reach CHUNK.
/call fwrite() every time may largely decrease the writing performance

$totalCount = TOTAL;
$CHUNK = 10000; //No, lines write each time
$counter = 1;


while (false !== $row = mysql_fetch_row($rs)){
$data .= implode("\t", $row)."\n";
if ($counter%$CHUNK == 0||$counter==$totalCount){
echo "proc ".$counter.' ...'."\n";
fwrite($fh_save, $data);
$data = '';
}
$counter++;
}

Saturday 9 March 2013

mysql two datetimes substraction (date difference)

Assume var1, var2 are type of datetime in MySQL:

var1 = 2010-11-09, var2 = 2010-10-15;

select var1 - var2
from tableX
//return 94
//???


select DATEDIFF(var1,var2)
from tableX
//return 25
//25 days between var1 and var2

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






Friday 8 June 2012

Yii Three ways to update counters

Using Yii, there are probably three ways to update counters:

  1. Model->save();
  2. Model->saveCounters();
  3. Model->updateCounters();
For all these methods, we have to get the object before performing the updating process. i.e.,  

$obj = YourObject->model()->findByPk($id);

Difference among them:

1.$obj->visits += 1; 
  $obj->save();

2.$obj->saveCounters(array('visits'=>'1'));

3.$obj->updateCounters(array('visits'=>'1', 'id'=>$id));

The Trick is:
  1. Better to use saveCounters()!
  2. If you use updateCounters(), pls make sure you have put id in a condition as highlighted in the code. Otherwise, the 'visits' field for all records +1.

Monday 21 May 2012

jQuery Insert text into textarea

jQuery Insert text into textarea:



jQuery.fn.extend({
insertAtCaret: function(myValue){
 return this.each(function(i) {
   if (document.selection) {
     //For browsers like Internet Explorer
     this.focus();
     sel = document.selection.createRange();
     sel.text = myValue;
     this.focus();
   }
   else if (this.selectionStart || this.selectionStart == '0') {
     //For browsers like Firefox and Webkit based
     var startPos = this.selectionStart;
     var endPos = this.selectionEnd;
     var scrollTop = this.scrollTop;
     this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
     this.focus();
     this.selectionStart = startPos + myValue.length;
     this.selectionEnd = startPos + myValue.length;
     this.scrollTop = scrollTop;
   } else {
     this.value += myValue;
     this.focus();
   }
 })
}
});
Usage:


$("#elem_id").insertAtCaret('text');

Tuesday 1 May 2012

Axis Webserivce how to solve: java.lang.reflect.InvocationTargetException

Axis Webserivce how to solve:  java.lang.reflect.InvocationTargetException


Config: Tomcat 5.5 + Axis 1.4 , called by PHP SoapClient, 


Status: While visiting localhost:xxx?wsdl the dom tree can be seen. But called from PHP, it raises the  java.lang.reflect.InvocationTargetException.


Solution:
    1. copy all .jar files from tomcat/common/lib to Tomcat\webapps\ROOT\axis\WEB-INF\lib.
    2. Make sure there is no enum in your web service class!!!! Very important!!!
         E.g.:


        public class Search {


       // remove this !!!!!! it will raise java.lang.reflect.InvocationTargetException
         public enum Color { 
            RED , BLUE,GREEN,BLACK ;
         }
public XXX search(){


        }
    }