Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 20 revisions

For all my fancy Web 2.0 stuff I use jQuery (www.jquery.com). It's lean, it's easy, it's fast and it's fun. Anyway, to speed up production I put some stuff into a jquery class. It's far from complete, probably has bugs, error handling broke but I'll post it anyway. Knowledge of jQuery is necessary to work with it, it might be helpful for some. Using [url]http://www.visualjquery.com/index.xml[/url] is very much recommended, the jquery API is on it.

Name the code below; jquery.php and put in the libraries folder. $this->load->library('jquery'); loads it. [code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Jquery {

var $externalJquery='jquery.js'; var $jqueryScript='';

var $jqDocumentReady=false; //whether to automatically output $(document).ready()stuff

var $externalScripts=array(); var $errors=array();

//php4 constructor points to php5 constructor function Jquery(){ $this->__constructor(); } //php5 constructor function __constructor(){

//from php.net //in php4 http_build_query is not available, this is a handwritten version //php5 has such a function //is used for POST and GET stuff if (!function_exists('http_build_query')) { function http_build_query($data, $prefix='', $sep='', $key='') { $ret = array(); foreach ((array)$data as $k => $v) { if (is_int($k) && $prefix != null) $k = urlencode($prefix . $k); if (!empty($key)) $k = $key.'['.urlencode($k).']';

           if (is_array($v) || is_object($v))
               array_push($ret, http_build_query($v, '', $sep, $k));
           else    array_push($ret, $k.'='.urlencode($v));
       }
    
       if (empty($sep)) $sep = ini_get('arg_separator.output');
       return implode($sep, $ret);
    }
}

} //set the url of jquery.js //defaults to jquery.js but if you want to alter name/path function setExternalJquery($url){

$this->externalJquery=$url; } //add external script like , loads into an array to be looped in the final jqeury process function addExternalScript($url,$type='text/javascript'){ $this->externalScripts[]='</script>'; return true; } //sets $this->jqueryScript function setJqueryScript($script=''){

$this->jqueryScript=$script; return true;

} //appends $script to $this->jqueryScript function addJqueryScript($script=''){

$this->setJqueryScript($this->jqueryScript . $script); return true;
} //outputs the variable $this->jqueryScript (no stuff, handy if you want to load some jquery via ajax) function printJqueryScript(){ echo $this-&gt;jqueryScript; } //If set to true $(document).ready(function(){ $this-&gt;jqueryScript }); will be outputted, instead of without the $(document).ready stuff //saves a line of code that is often used. Defaults to false function setJqDocumentReady($int){ $this->jqDocumentReady=$int; return true; } function jQueryError($error, $description=false){ $this->errors[]=$error . ' - '. $description; return true; } ///outputs the entire Jquery Javascript stuff, including the externally loaded scripts. function processJquery(){ $result= 'externalJquery.'"></script>';

foreach($this->externalScripts as $externalScript){ $result.=$externalScript; } if(!empty($this->errors)) { $result.=''; $result.='$(document).ready(function(){'; $errors=''; foreach($this->errors as $error) $errors.=$error; $result.='alert("'.trim($errors).'")'; $result.='});'; $result.='</script>'; return $result; }

$result.='';

if($this->jqDocumentReady==true) $this-&gt;jqueryScript='$(document).ready(function(){'. $this->jqueryScript . '});';

$result.=$this->jqueryScript;

$result.='</script>'; return $result; } //outputs the script, include stuff function printJquery(){ echo $this->processJquery(); }

function JqueryObject($instanceName,$type){
$type=strtolower($type);

if($type=='ajax') $oName='JqueryAjax'; elseif($type=='assign') $oName='JqueryAssign'; elseif($type=='event') $oName='JqueryEvent';

/* $arg_list = func_get_args(); // print_r($arg_list); $args=''; foreach ($arg_list as $key=>$arg) { //echo $arg; if($key!=0){ $args.= $arg. ',';

 }

 //  echo "Argument  is: ";// . //$arg_list[$i] . "<br />\n";

} $args=substr($args,0,-1); //echo $args; */ $this->$instanceName=new $oName();

} function addJqueryObject($instanceName){ $result=$this->$instanceName->getJquery(); $this->addJqueryScript($result); } function getJqueryObject($instanceName){ $result=$this->$instanceName->getJquery(); return $result;//$this->getJqueryScript($result); }

}

class JqueryEvent{

var $script; var $eventType='bind'; var $value='ddddd'; var $elementName; var $objectName; var $event='click'; function JqueryEvent(){}

function assignEventTo($elementName){ $this->elementName=$elementName; } function assignJavascript($script){ $this->script=$script; } function assignType($eventType){ //Bind, Unbind that list $this->eventType=$eventType; } function assignEvent($event){ $this->event=$event; } function getJquery(){ return '$("'.$this->elementName.'").'.$this->eventType.'("'.$this->event.'",'.$this->script.');'; //$("p").bind( "click", function() { alert( $(this).text() ); } ) } } class JqueryAjax{

var $ajaxTypes=array(); var $ajaxDataTypes=array();

var $requestType='POST'; var $datatype='html'; var $data=array(); var $url=false; var $success_function=false; var $errors=array(); function JqueryAjax(){

$this->__constructor(); } function __constructor($dd='',$ss='',$sa=''){ $this->ajaxTypes=array('POST','GET'); $this->ajaxDataTypes=array('html','script','xml','json'); $this->requestType='POST';

// $arg_list = func_get_args();

// print_r($arg_list); }

function jQueryAjaxError($error, $description=false){ $this->errors[]=$error . ' - '. $description; print_r($this->errors); die(); } function setDataType($datatype){

$this->datatype=strtolower($datatype); if(!in_array($this->datatype,$this->ajaxDataTypes)) $this->jQueryAjaxError('Wrong datatype given','$datatype = '.$this->datatype); } function setRequestType($requestType){ $this->requestType=strtoupper($requestType); if(!in_array($this->requestType,$this->ajaxTypes)) $this->jQueryAjaxError('Wrong type given','$type = '.$this->requestType); } function setRequestUrl($url){ $this->url=$url; } function setSuccessFunction($script){ $this->success_function=$script; } function setDataRaw($data){ $this->data=$data;

} function setData($data){

if(!$this->data=http_build_query($data))  $this->jQueryAjaxError('Wrong data given','$data'.$this->data);

} function getJquery(){

//$ajaxType='type: "'.$this->requestType .'"';

$ajaxCall='$.ajax({'; $ajaxCall.=' type: "'.$this->requestType .'"'; $ajaxCall.=', url: "'.$this->url.'"'; $ajaxCall.=', data: "'.$this->data.'"' ; $ajaxCall.=', dataType: "'.$this->datatype.'"' ; if($this->success_function!=false) $ajaxCall.=',success: function(data) {' .$this->success_function .'}'; //' function(msg){ alert( "Data Saved: " + msg ); }'; $ajaxCall.='});'; //$.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } }); return $ajaxCall; }

}

class JqueryAssign{ var $script; var $location='after'; var $value='ddddd'; var $elementName; var $objectName; // var $objectType;

function JqueryAssign(){ //[o] see if a shorthand sth is possible //,$elementName=false,$value=false,$location=false){ //$this->objectName=$objectName; // $this->objectType=$objectType; /* see note above if($elementName!=false) $this->elementName=$elementName; if($location!=false) $this->location=$location; if($value!=false) $this->value=$value;*/

//$this->elementName=$elementName;
//if($getJquery==true) return $this->getJquery();

} function assignValueTo($elementName){ $this->elementName=$elementName; } function assignValue($value){ //$value=addslashes($value); $this->value=$value; } function assignLocation($location){ $this->location=$location; } function getJquery(){

return '$("'.$this->elementName.'").'.$this->location.'(" '.$this->value.' " );'; } } ?> [/code] [h2]How it works[/h2] Better do this on another page

Clone this wiki locally