Location: PHPKode > scripts > navigInTable > navigintable/navigInTable.class.php
<?php
/*
 * Classe de circulation dans une table de base de données
 * sur le critère d'un champ auto_increment.
 *
 * Cette classe nécessite l'utilisationde Db_Sql.php
 */
if ( !defined( 'NAVIGINTABLE_INCLUDED' ) ) {
    define('NAVIGINTABLE_INCLUDED', 1 );    //debut de l'include

class navigInTable {

	// Le constructeur
    function navigInTable ( $host="", $base="", $user="", $pswd="", $table="", $rang="", $pCour, $clWhere="", $tbWhere="" ) {
		$this->OK = true;
        if ( $host == "" || $base == "" || $user == "" || $pswd == "" || $table == "" || $rang == "") {
            $this->OK = false;
        } else {
            $this->Host = $host;
            $this->Database = $base;
            $this->User = $user;
            $this->Password = $pswd;
            $this->Table = $table;
			$this->champRang = $rang;
			$this->champRangTable = $table.".".$rang;				
			
            $q = new aj_Db( $this->Host, $this->Database, $this->User, $this->Password);
            $this->dbConn = $q;
			//$this->dbConn->Debug =1;
            // $wher permet de ne naviguer que parmi certains champs
            if ( !empty($clWhere)) {
                $this->clWhere0 = "WHERE ($clWhere) ";
                $this->clWhere1 = "AND ($clWhere) ";
            } else {
                $this->clWhere0 = "";
                $this->clWhere1 = "";
            }
			
			// Si la clause where fait intervenir d'autres tables, il faut le préciser
            if ( !empty( $tbWhere))
                $this->tbWhere = ", $tbWhere ";
            else
                $this->tbWhere = "";
				
            // Vérification du flag du champ : auto_increment
            $tabMetaData = $q->metadata( $this->Table, true);
            $ir = $tabMetaData["meta"][$this->champRang];
            if ( ereg( "auto_increment", $tabMetaData[$ir]["flags"])) {
				$rgp = $this->rangFirst(); $rgd = $this->rangLast();
    			if ( ( settype( $pCour, "integer")) && ($pCour>=$rgp) && ($pCour<=$rgd)) {
					$this->dbConn->query( "SELECT $this->champRang FROM $this->Table WHERE $this->champRang = $pCour $this->clWhere1");
					if ($this->dbConn->nf()) $this->setRangCourant( $pCour);
						else $this->rangPrem();
				}
				else $this->setRangCourant(  $rgp);
            } else {
                $this->OK = false;
            }
        }
    }    // fin du constructeur

/////////////////////////////////////////////////////////////////////////////
///////                   Fonctions et variables privées              ///////
/////////////////////////////////////////////////////////////////////////////
    function setRangCourant( $rc) {
        $this->rangCour = $rc;
    }   // fin de getRangCourant
	function rangFirst() {	// function privée
        if ( $this->nbElts()) {
            $this->dbConn->query( "SELECT MIN($this->champRangTable) AS rangPrem FROM $this->Table $this->tbWhere WHERE ($this->champRangTable > 0) $this->clWhere1 ORDER BY $this->champRangTable");
            $this->dbConn->next_record();
			$this->OK = true; 
			return $this->dbConn->Record['rangPrem'];
        } else {
            $this->OK = false;
			return 0;
        }
    }    // fin de rangPrem
    function rangLast() {	// function privée
        if ( $n = $this->nbElts()) {
            if ( $n == 1) {
                return 1;
            } else {
                $r = $n-1;
                $this->dbConn->query( "SELECT $this->champRangTable AS rang FROM $this->Table $this->tbWhere $this->clWhere0 ORDER BY $this->champRangTable LIMIT $r,1");
                $this->dbConn->next_record();
				$this->OK = true; 
				return $this->dbConn->Record['rang'];
            }
        } else {
            $this->OK = false;
			return 0;
        }
    }    // fin de rangDern
    function rangNext( $rc) { // fonction privée
        $this->dbConn->query( "SELECT MIN($this->champRangTable) AS numNext FROM $this->Table $this->tbWhere WHERE ($this->champRangTable > $rc) $this->clWhere1 ORDER BY $this->champRangTable");
        $this->dbConn->next_record();
        return $this->dbConn->Record['numNext'];
    }   // fin de rangNext
    function rangPrev( $rc) { // fonction privée
        $this->dbConn->query( "SELECT MAX($this->champRangTable) AS numPrev FROM $this->Table $this->tbWhere WHERE ($this->champRangTable < $rc) $this->clWhere1 ORDER BY $this->champRangTable");
        $this->dbConn->next_record();
        return $this->dbConn->Record['numPrev'];
    }  // fin de rangPrev
/////////////////////////////////////////////////////////////////////////////
///////                   Fonctions publiques                         ///////
/////////////////////////////////////////////////////////////////////////////
	function nbElts() {
        $this->dbConn->query( "SELECT COUNT(*) AS nbElts FROM $this->Table $this->tbWhere $this->clWhere0");
        $this->dbConn->next_record();
        return $this->dbConn->Record['nbElts'];
    }    // fin de nbElts

    function getRangCourant() {
        return $this->rangCour;
    }   // fin de getRangCourant

	function isExist( $rc) {
		$this->dbConn->query( "SELECT $this->champRang FROM $this->Table WHERE $this->champRang = $rc");
		return $this->dbConn->nf();
	}
	
    function isPrem( $rc) {
        return ( $rc == $this->rangFirst());
    }   // fin de isPrem
	
    function isDern( $rc) {
        return ( $rc == $this->rangLast());
    }   // fin de isDern

    function rangPrem () {
		$r =  $this->rangFirst();
		if ( $this->OK) 
			return $this->setRangCourant( $r);
	}

    function rangDern () {
		$r = $this->rangLast();
		if ( $this->OK) 
			return $this->setRangCourant( $r);
	}

    function rangSuiv() {
		if ( $this->isExist( $rc = $this->getRangCourant())) {
	        if ( !$this->isDern( $rc))
	    	    $this->setRangCourant( $this->rangNext( $rc));
		} else $this->rangDern(); 
		return $this->getRangCourant();
    }    // fin de rangSuiv

    function rangPrec() {
		if ( $this->isExist( $rc = $this->getRangCourant())) {
			if ( !$this->isPrem( $rc))
		        $this->setRangCourant( $this->rangPrev( $rc));
		} else $this->rangprem();
		return $this->getRangCourant();
    }  // fin de rangPrec

    function rangSuivPlus( $n=1) {
        for ( $i=0; $i<$n; $i++) $ret = $this->rangSuiv();
        return $ret;
    }

    function rangPrecPlus( $n=1) {
        for ( $i=0; $i<$n; $i++) $ret = $this->rangPrec();
        return $ret;
    }
}   // fin de la classe navigInTable
}   // Fin de l'include
?>
Return current item: navigInTable