WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : [PHP5] NOTIMEOUT PACKAGE
Categories : PHP, PHP Classes, AJAX Click here to Update Your Picture
Johan Barbier
Date : Oct 09th 2006
Grade : 5 of 5 (graded 1 times)
Viewed : 3185
File : 4512.zip
Images : No Images for this code example.
Search : More code by Johan Barbier
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

noTimeOut package
@author : Johan Barbier <johan.barbier@gmail.com>
@Version : 2006/10/09

Needed : PHP 5, short_open_tags to Off

DESCRIPTION:

This package is meant to retrieve a huge amount of data, or process a big php script, and not being annoyed by PHP time limit (ie : set_time_limit, and max_execution_time).
Sometimes, you do not want, or can, modify this timeout. But it can be pretty annoying if you have a big file to read, or a huge query to process.
Well, this package can help you!

Basically, it uses xmlhttp requests to ask the server to process a part of the main process.
For exemple, for a file, it will read lines 0 to 10, then 11 to 20, and so on.
That way, you will never hit PHP time limit, but your 50000 lines file will be read and displayed anyway.
The file (or query results, or whatever) will be flushed step by step.
Just have a look at the indexX.php exemple pages.
In most of them, PHP time limit has been set to 2s.

Addition from the 20th of October 2006 : now, stacks are supported. You can declare several stacks and have them work asynchronously.

The package consists of 2 classes:
class/class.noTimeOut.js
=> used to initialize the xmlhttp object and the required methods.
noTimeOut::initialize() method shows the properties that can/must be set
NOTA : the ARGS property is an array. It can be used to send other variables to the PHP script (useful for the new oneShot method : a basic Ajax one shot request)
noTimeOut::getData() method shows you the types of processes that can be used.
Basically :
- instanciate your object :
var oBuffer = new noTimeOut ();
- declare a stack :
oBuffer.declareStack (Stack_Name);
- initialize your properties :
oBuffer.initialize (Stack_Name, Prop_Name, Prop_Value);
- start it!
oBuffer.startWork (Stack_Name);

class/class.noTimeOut.php
=> used to process the scripts.
noTimeOut::aProps shows all the properties that can/must be initialized (it depends on the type of process chosen).
noTimeOut::aTypes shows the possible types of process (up to now)
noTimeOut::aDbServers shows the sql servers supported up to now, in a DB type of process
noTimeOut::flushMe () is the method used to flush the process
In the scripts/ folder, you will find the scripts using this class.
Basically : initialize your properties, and use flushMe() to process.


Have a look at the indexX.php exemple pages to see how to use the classes, and the different types of processes.
If you want to use the index2.php you first have to create a 'tests' database, and use the test.sql file to insert the data.


class.noTimeOut.js
/**
@author : Johan Barbier <johan.barbier@gmail.com>
@Version : 2006/10/20
*/
function noTimeOut () {

    var aStack = new Array;
    var aStacks = new Array;

    function getObject () {
        if (window.XMLHttpRequest) {
            var oXmlhttp = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            var oXmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return oXmlhttp;
    }

    function addToData (sStack) {
        var sData = '';
        var iArgs = aStack[sStack]['ARGS'].length;
        if ( iArgs > 0) {
            for (var iCpt = 0; iCpt < iArgs; iCpt ++) {
                sData += '&arg_'+iCpt+'='+aStack[sStack]['ARGS'][iCpt];
            }
        }
        return sData;
    }

    function getDefault (sStack, iStart) {
        var j = iStart + aStack[sStack]['STEP'];
        aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
        aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
            if (aStack[sStack]['OXMLHTTP'].readyState==1) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==2) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
                }
                /**
                * DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
                *
                */
                if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
                    var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
                    parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
                } else {
                    parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
                }
                /*
                parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
                */
                if (j < aStack[sStack]['LIMIT']) {
                        aStacks.unshift (sStack);
                        aStack[sStack]['START'] = j;
                        checkStack ();
                }
            }
        }
        aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var data = 'sType=DEFAULT&iStart='+iStart+'&iStep='+STEP;
        var iArgs = aStack[sStack]['ARGS'].length;
        data += addToData (sStack);
        aStack[sStack]['OXMLHTTP'].send (data);
    }

    function getDB (sStack, iStart) {
        var j = iStart + aStack[sStack]['STEP'];
        aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
        aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
            if (aStack[sStack]['OXMLHTTP'].readyState==1) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];

                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==2) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];

                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];

                }
                /**
                * DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
                *
                */
                if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
                    var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
                    parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
                } else {
                    parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
                }
                //parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
                if (j < aStack[sStack]['LIMIT']) {
                        aStacks.unshift (sStack);
                        aStack[sStack]['START'] = j;
                        checkStack ();
                }
            }
        }
        aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var data = 'sType=DB&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sQuery='+aStack[sStack]['QUERY'];
        data += addToData (sStack);
        aStack[sStack]['OXMLHTTP'].send (data);
    }

    function getFile (sStack, iStart) {
        var j = iStart + aStack[sStack]['STEP'];
        aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
        aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
            if (aStack[sStack]['OXMLHTTP'].readyState==1) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==2) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
                }
                /**
                * DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
                *
                */
                if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
                    var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
                    parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
                } else {
                    parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
                }
                /*
                parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
                */
                if (j < aStack[sStack]['LIMIT']) {
                        aStacks.unshift (sStack);
                        aStack[sStack]['START'] = j;
                        checkStack ();
                }
            }
        }
        aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var data = 'sType=FILE_LINE&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
        data += addToData (sStack);
        aStack[sStack]['OXMLHTTP'].send (data);
    }

    function getFileLine (sStack, iStart) {
        var j = iStart;
        aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
        aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
            if (aStack[sStack]['OXMLHTTP'].readyState==1) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==2) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
                }
                /**
                * DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
                *
                */
                if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
                    var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
                    parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
                } else {
                    parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
                }
                /*
                parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
                */
                j += aStack[sStack]['OXMLHTTP'].responseText.length;
                if (aStack[sStack]['SUBSTR'] != '') {
                    j -= aStack[sStack]['SUBSTR'];
                }
                if (j < aStack[sStack]['LIMIT']) {
                        aStacks.unshift (sStack);
                        aStack[sStack]['START'] = j;
                        checkStack ();
                }
            }
        }
        aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var data = 'sType=FILE_LINE&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
        data += addToData (sStack);
        aStack[sStack]['OXMLHTTP'].send (data);
    }

    function getFilePat (sStack, iStart) {
        var j = iStart;
        aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
        aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
            if (aStack[sStack]['OXMLHTTP'].readyState==1) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==2) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
                }
                /**
                * DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
                *
                */
                if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
                    var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
                    parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
                } else {
                    parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
                }
                /*
                parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML +=  aStack[sStack]['OXMLHTTP'].responseText;
                */
                j += aStack[sStack]['OXMLHTTP'].responseText.length;
                if (aStack[sStack]['SUBSTR'] != '') {
                    j -= aStack[sStack]['SUBSTR'];
                }
                if (j < aStack[sStack]['LIMIT']) {
                        aStacks.unshift (sStack);
                        aStack[sStack]['START'] = j;
                        checkStack ();
                }
            }
        }
        aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        var data = 'sType=FILE_PATTERN&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
        data += addToData (sStack);
        aStack[sStack]['OXMLHTTP'].send (data);
    }

    function oneShot (sStack) {
        aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
        aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
            if (aStack[sStack]['OXMLHTTP'].readyState==1) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];

                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==2) {
                if (aStack[sStack]['MSG'] != '') {
                    parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];

                }
            }
            if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
                /**
                * DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
                *
                */
                /*
                if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
                    var oNode =  parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
                    parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
                } else {
                    parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
                }
                */
                parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML = aStack[sStack]['OXMLHTTP'].responseText;
                checkStack ();
            }
        }
        aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        data = addToData (sStack);
        aStack[sStack]['OXMLHTTP'].send (data);
        checkStack ();
    }

    function getData (sStack, sType) {
        var bGo = false;
        switch (sType) {
            case 'DEFAULT':
                getDefault (sStack, aStack[sStack]['START']);
                break;
            case 'FILE_OCTET':
                getFile (sStack, aStack[sStack]['START']);
                break;
            case 'FILE_LINE':
                getFileLine (sStack, aStack[sStack]['START']);
                break;
            case 'FILE_PATTERN':
                getFilePat (sStack, aStack[sStack]['START']);
                break;
            case 'DB':
                getDB (sStack, aStack[sStack]['START']);
                break;
            case 'ONE_SHOT':
                oneShot (sStack);
                break;
        }
    }

    function checkStack () {
        var iLen = aStacks.length;
        if (iLen > 0) {
            var sStack = aStacks[iLen - 1];
            var sType = aStack[aStacks[iLen - 1]]['TYPE'];
            aStacks.pop ();
            getData (sStack, sType);
        }
    }

    this.declareStack = function (sStack) {
        aStack[sStack] = new Array;
        aStack[sStack]['START'] = aStack[sStack]['LIMIT'] = aStack[sStack]['STEP'] = aStack[sStack]['FILE'] = aStack[sStack]['QUERY'] = aStack[sStack]['TARGET'] = aStack[sStack]['SCRIPT'] = aStack[sStack]['MSG'] = aStack[sStack]['SUBSTR'] = '';
        aStack[sStack]['METHOD'] = 'POST';
        aStack[sStack]['ARGS']  = new Array;
        aStack[sStack]['MSG_WAITING'] = 'Loading';
        aStack[sStack]['MSG_LOADED'] = 'Loaded';
        aStack[sStack]['MSG_READY'] = 'OK';
    }

    this.startWork = function (sStack) {
        aStack[sStack]['OXMLHTTP'] = getObject ();
        var iLen = aStacks.length;
        aStacks[iLen] = sStack;
        checkStack ();
    }

    this.initialize = function (sStack, sType, mValue) {
        if (!aStack[sStack]) {
            return false;
        }
        switch (sType) {
            case 'START' :
                aStack[sStack]['START'] = mValue;
                return true;
                break;
            case 'LIMIT' :
                aStack[sStack]['LIMIT'] = mValue;
                return true;
                break;
            case 'STEP' :
                aStack[sStack]['STEP'] = mValue;
                return true;
                break;
            case 'SUBSTR' :
                aStack[sStack]['SUBSTR'] = mValue;
                return true;
                break;
            case 'FILE' :
                aStack[sStack]['FILE'] = mValue;
                return true;
                break;
            case 'QUERY' :
                aStack[sStack]['QUERY'] = mValue;
                return true;
                break;
            case 'TARGET' :
                aStack[sStack]['TARGET'] = mValue;
                return true;
                break;
            case 'SCRIPT' :
                aStack[sStack]['SCRIPT'] = mValue;
                return true;
                break;
            case 'MSG' :
                aStack[sStack]['MSG'] = mValue;
                return true;
                break;
            case 'MSG_WAITING' :
                aStack[sStack]['MSG_WAITING'] = mValue;
                return true;
                break;
            case 'MSG_LOADED' :
                aStack[sStack]['MSG_LOADED'] = mValue;
                return true;
                break;
            case 'MSG_READY' :
                aStack[sStack]['MSG_READY'] = mValue;
                return true;
                break;
            case 'METHOD' :
                if (mValue != 'POST' || mValue != 'GET') {
                    return false;
                }
                aStack[sStack]['METHOD'] = mValue;
                return true;
                break;
            case 'TYPE':
                aStack[sStack]['TYPE'] = mValue;
                break;
            case 'ARGS':
                aStack[sStack]['ARGS'] = mValue;
                break;
            default:
                return false;
                break;
        }
    }
}



class.noTimeOut.php
<?php
/**
@author : Johan Barbier <johan.barbier@gmail.com>
@Version : 2006/10/09
*/

class noTimeOut {
    private
$aProps = array (
       
'TYPE' => null,
       
'DB' => null,
       
'HOST' => null,
       
'LOGIN' => null,
       
'PWD' => null,
       
'QUERY' => null,
       
'DBSERVER' => null,
       
'FILE' => null,
       
'START' => null,
       
'LIMIT' => null,
       
'STEP' => null
       
);

    private
$aDbServers = array (
       
'MYSQL', 'MSSQL'
       
);

    private
$aTypes = array (
       
'DEFAULT', 'FILE_OCTET', 'FILE_PATTERN', 'FILE_LINE', 'DB'
       
);

    private
$aErrors = array ();

    public function
__construct () {
       
// might be useful later
   
}

    public function
__set ($sType, $sVal) {
        try {
            if (!
array_key_exists ($sType, $this -> aProps)) {
                throw new
Exception ($sType.' is not a valid property');
            }
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
        try {
            switch (
$sType) {
                case
'TYPE':
                    if (!
in_array ($sVal, $this -> aTypes)) {
                        throw new
Exception ($sVal.' is not a valid TYPE value');
                    }
                    break;
                case
'FILE':
                    if (!
file_exists ($sVal)) {
                        throw new
Exception ('File '.$sVal.' has not been found');
                    }
                    break;
                case
'DBSERVER':
                    if (!
in_array ($sVal, $this -> aDbServers)) {
                        throw new
Exception ('DB SERVER '.$sVal.' is not supported');
                    }
                    break;
                default :
                    break;
            }
           
$this -> aProps[$sType] = $sVal;
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
    }

    public function
__get ($sType) {
        try {
            if (!
array_key_exists ($sType, $this -> aProps) && $sType !== 'ERRORS') {
                throw new
Exception ($sType.' is not a valid property');
            }
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
        if (
$sType === 'ERRORS') {
            return
$this -> aErrors;
        } else {
            return
$this -> $sType;
        }
    }

    private static function
isNull () {
        foreach (
func_get_args() as $sArg) {
            if (
is_null ($sArg)) {
                return
false;
            }
        }
        return
true;
    }

    public function
flushMe ($aWork = null) {
        try {
            if (
is_null ($this -> aProps['TYPE'])) {
                throw new
Exception ('TYPE has not been defined');
            }
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
        try {
            switch (
$this -> aProps['TYPE']) {
                case
'DB':
                    if (
false === self::isNull ($this -> aProps['DB'], $this -> aProps['HOST'], $this -> aProps['LOGIN'], $this -> aProps['PWD'], $this -> aProps['QUERY'], $this -> aProps['DBSERVER'], $this -> aProps['START'], $this -> aProps['STEP'])) {
                        throw new
Exception ('DB properties have not been fully defined');
                    }
                   
$mTmp = $this -> getDB ();
                    break;
                case
'FILE_OCTET':
                    if (
false === self::isNull ($this -> aProps['FILE'], $this -> aProps['START'], $this -> aProps['STEP'])) {
                        throw new
Exception ('FILE properties have not been fully defined');
                    }
                   
$mTmp = $this -> getFileOctet ();
                    break;
                case
'DEFAULT':
                    if (
false === self::isNull ($this -> aProps['START'], $this -> aProps['STEP'], $aWork)) {
                        throw new
Exception ('DEFAULT properties have not been fully defined');
                    }
                   
$mTmp = $this -> getDefault ($aWork);
                    break;
                case
'FILE_PATTERN':
                    if (
false === self::isNull ($this -> aProps['FILE'], $this -> aProps['START'], $this -> aProps['STEP'])) {
                        throw new
Exception ('FILE properties have not been fully defined');
                    }
                   
$mTmp = $this -> getFilePat ();
                    break;
                case
'FILE_LINE':
                    if (
false === self::isNull ($this -> aProps['FILE'], $this -> aProps['START'], $this -> aProps['STEP'])) {
                        throw new
Exception ('FILE properties have not been fully defined');
                    }
                   
$mTmp = $this -> getFileLine ();
                    break;
            }
            return
$mTmp;
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
    }

    private function
getFilePat () {
       
$sTmp = '';
        try {
            if (
false === ($fp = fopen ($this -> aProps['FILE'], 'r'))) {
                throw new
Exception ('Failed to open file : '.$this -> aProps['FILE']);
            }
            if ( -
1 === (fseek ($fp, $this -> aProps['START'], SEEK_SET))) {
                throw new
Exception ('Failed to  modify cursor on : '.$this -> aProps['FILE']);
            }
            while (
false === ($iEnd = strpos ($sTmp, $this -> aProps['STEP'])) && !feof ($fp)) {
               
$sTmp .= @fgets ($fp, 1024);
            }
           
$sTmp = substr ($sTmp, 0, $iEnd + strlen ($this -> aProps['STEP']));
            @
fclose ($fp);
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
        return
$sTmp;
    }

    private function
getDefault ($aWork) {
        try {
            if (!
is_array ($aWork)) {
                throw new
Exception ('Parameter must be an array');
            }
           
$aTmp = array ();
            for (
$i = $this -> aProps['START']; $i < $this -> aProps['START'] + $this -> aProps['STEP']; $i ++) {
                if (isset (
$aWork[$i])) {
                   
$aTmp[] = $aWork[$i];
                }
            }
            return
$aTmp;
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
    }

    private function
getFileOctet () {
        try {
           
$sTmp = '';
            if (
false === ($fp = fopen ($this -> aProps['FILE'], 'r'))) {
                throw new
Exception ('Failed to open file : '.$this -> aProps['FILE']);
            }
            if ( -
1 === (@fseek ($fp, $this -> aProps['START'], SEEK_SET))) {
                throw new
Exception ('Failed to  modify cursor on : '.$this -> aProps['FILE']);
            }
            if (
false === ($sTmp .= @fread ($fp, $this -> aProps['STEP']))) {
                throw new
Exception ('Failed to  read file : '.$this -> aProps['FILE']);
            }
            @
fclose ($fp);
            return
$sTmp;
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
    }

    private function
getFileLine () {
       
$sTmp = '';
        try {
            if (
false === ($fp = fopen ($this -> aProps['FILE'], 'r'))) {
                throw new
Exception ('Failed to open file : '.$this -> aProps['FILE']);
            }
            if ( -
1 === (fseek ($fp, $this -> aProps['START'], SEEK_SET))) {
                throw new
Exception ('Failed to  modify cursor on : '.$this -> aProps['FILE']);
            }
           
$iEnd = 0;
            while (
$iEnd < $this -> aProps['STEP']) {
               
$sTmp .= @fgets ($fp);
               
$iEnd ++;
            }
            @
fclose ($fp);
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
        return
$sTmp;
    }

    private function
getDB () {
       
$sDb = strtolower ($this -> aProps['DBSERVER']);
        try {
           
$rLink = @call_user_func ($sDb.'_connect', $this -> aProps['HOST'], $this -> aProps['LOGIN'], $this -> aProps['PWD']);
            if (
false === $rLink) {
                throw new
Exception ('Failed to connect to host : '.$this -> aProps['HOST']);
            }
            if (
false === (@call_user_func ($sDb.'_select_db', $this -> aProps['DB'], $rLink))) {
                throw new
Exception ('Failed to select database : '.$this -> aProps['DB']);
            }
            if (
false === ($rRes = @call_user_func ($sDb.'_query', $this -> aProps['QUERY'], $rLink))) {
                throw new
Exception ('Query failed : '.$this -> aProps['QUERY']);
            }
            if (
false === (@call_user_func ($sDb.'_data_seek', $rRes, $this -> aProps['START']))) {
                throw new
Exception ('Query failed : '.$this -> aProps['QUERY']);
            }
           
$iCpt = 0;
           
$aTmp = array ();
            while ((
$aRes = call_user_func ($sDb.'_fetch_assoc', $rRes)) && $iCpt < $this -> aProps['STEP']) {
               
$aTmp[] = $aRes;
               
$iCpt ++;
            }
            @
call_user_func ($sDb.'_close', $rLink);
            return
$aTmp;
        } catch (
Exception $e) {
           
$this -> aErrors[] = $e -> getMessage ();
        }
    }
}
?>






The Ajax Tree view class fetches data from a db for the requested parent category id. The data is then stored in an array and converted into JSON (Javascript Object Notation) format. This format is then used by JavaScript for populating tree view.
Categories : PHP, PHP Classes, Java Script, AJAX, Databases
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
very simple ftp class
Categories : PHP, PHP Classes, FTP
PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
crop and resize image class using gd library function
Categories : PHP,