dbnested.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 969 行 · 第 1/3 页
PHP
969 行
$this->_getColName('left') ); if (DB::isError( $res = $this->dbh->getAll($query))) { return $this->_throwError( $res->getMessage() , __LINE__ ); } return $this->_prepareResults( $res ); } /** * gets the element to the left, the left visit * * @access public * @version 2002/03/07 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param * @return mixed either the data of the requested element or an Tree_Error */ function getLeft( $id ) { $element = $this->getElement( $id ); if( PEAR::isError($element) ) return $element; $query = sprintf( 'SELECT * FROM %s WHERE%s (%s=%s OR %s=%s)', $this->table, $this->_getWhereAddOn(), $this->_getColName('right'),$element['left']-1, $this->_getColName('left'),$element['left']-1 ); if( DB::isError( $res = $this->dbh->getRow($query) ) ) { return $this->_throwError( $res->getMessage() , __LINE__ ); } return $this->_prepareResult( $res ); } /** * gets the element to the right, the right visit * * @access public * @version 2002/03/07 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param * @return mixed either the data of the requested element or an Tree_Error */ function getRight( $id ) { $element = $this->getElement( $id ); if( PEAR::isError($element) ) return $element; $query = sprintf( 'SELECT * FROM %s WHERE%s (%s=%s OR %s=%s)', $this->table, $this->_getWhereAddOn(), $this->_getColName('left'),$element['right']+1, $this->_getColName('right'),$element['right']+1); if( DB::isError( $res = $this->dbh->getRow($query) ) ) { return $this->_throwError( $res->getMessage() , __LINE__ ); } return $this->_prepareResult( $res ); } /** * get the parent of the element with the given id * * @access public * @version 2002/04/15 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param * @return mixed the array with the data of the parent element * or false, if there is no parent, if the element is the root * or an Tree_Error */ function getParent( $id ) { $query = sprintf( 'SELECT p.* FROM %s p,%s e WHERE%s e.%s=p.%s AND e.%s=%s', $this->table,$this->table, $this->_getWhereAddOn( ' AND ' , 'p' ), $this->_getColName('parentId'), $this->_getColName('id'), $this->_getColName('id'), $id); if( DB::isError( $res = $this->dbh->getRow($query) ) ) { return $this->_throwError( $res->getMessage() , __LINE__ ); } return $this->_prepareResult( $res ); } /** * get the children of the given element * or if the parameter is an array, it gets the children of all * the elements given by their ids in the array * * @access public * @version 2002/04/15 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param mixed (1) int the id of one element * (2) array an array of ids for which * the children will be returned * @param integer the children of how many levels shall be returned * @return mixed the array with the data of all children * or false, if there are none */ function getChildren($ids,$levels=1) { $res = array(); for( $i=1 ; $i<$levels+1 ; $i++ ) { // if $ids is an array implode the values $getIds = is_array($ids) ? implode(',',$ids) : $ids; $query = sprintf( 'SELECT c.* FROM %s c,%s e WHERE%s e.%s=c.%s AND e.%s IN (%s) '. 'ORDER BY c.%s', $this->table,$this->table, $this->_getWhereAddOn( ' AND ' , 'c' ), $this->_getColName('id'), $this->_getColName('parentId'), $this->_getColName('id'), $getIds, // order by left, so we have it in the order as it is in the tree // if no 'order'-option is given $this->getOption('order') ? $this->getOption('order') : $this->_getColName('left') ); if (DB::isError($_res = $this->dbh->getAll($query))) { return $this->_throwError( $_res->getMessage() , __LINE__ ); } $_res = $this->_prepareResults( $_res ); // we use the id as the index, to make the use easier esp. for multiple return-values $tempRes = array(); foreach ($_res as $aRes) { $tempRes[$aRes[$this->_getColName('id')]] = $aRes; } $_res = $tempRes; // if ($levels>1) { $ids = array(); foreach( $_res as $aRes ) $ids[] = $aRes[$this->_getColName('id')]; } $res = array_merge($res,$_res); // quit the for-loop if there are no children in the current level if (!sizeof($ids)) { break; } } return $res; } /** * get the next element on the same level * if there is none return false * * @access public * @version 2002/04/15 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param * @return mixed the array with the data of the next element * or false, if there is no next * or Tree_Error */ function getNext( $id ) { $query = sprintf( 'SELECT n.* FROM %s n,%s e WHERE%s e.%s=n.%s-1 AND e.%s=n.%s AND e.%s=%s', $this->table,$this->table, $this->_getWhereAddOn( ' AND ' , 'n' ), $this->_getColName('right'), $this->_getColName('left'), $this->_getColName('parentId'), $this->_getColName('parentId'), $this->_getColName('id'), $id); if( DB::isError( $res = $this->dbh->getRow($query) ) ) { return $this->_throwError( $res->getMessage() , __LINE__ ); } if( !$res ) return false; return $this->_prepareResult( $res ); } /** * get the previous element on the same level * if there is none return false * * @access public * @version 2002/04/15 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param * @return mixed the array with the data of the previous element * or false, if there is no previous * or a Tree_Error */ function getPrevious( $id ) { $query = sprintf( 'SELECT p.* FROM %s p,%s e WHERE%s e.%s=p.%s+1 AND e.%s=p.%s AND e.%s=%s', $this->table,$this->table, $this->_getWhereAddOn( ' AND ' , 'p' ), $this->_getColName('left'), $this->_getColName('right'), $this->_getColName('parentId'), $this->_getColName('parentId'), $this->_getColName('id'), $id); if( DB::isError( $res = $this->dbh->getRow($query) ) ) { return $this->_throwError( $res->getMessage() , __LINE__ ); } if( !$res ) return false; return $this->_prepareResult( $res ); } /** * returns if $childId is a child of $id * * @abstract * @version 2002/04/29 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param int id of the element * @param int id of the element to check if it is a child * @return boolean true if it is a child */ function isChildOf( $id , $childId ) { // check simply if the left and right of the child are within the // left and right of the parent, if so it definitly is a child :-) $parent = $this->getElement($id); $child = $this->getElement($childId); if( $parent['left'] < $child['left'] && $parent['right'] > $child['right'] ) { return true; } return false; } // end of function /** * return the maximum depth of the tree * * @version 2003/02/25 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @return int the depth of the tree */ function getDepth() {// FIXXXME TODO!!! return $this->_throwError( 'not implemented yet' , __LINE__ ); } /** * Tells if the node with the given ID has children. * * @version 2003/03/04 * @access public * @author Wolfram Kriesing <wolfram@kriesing.de> * @param integer the ID of a node * @return boolean if the node with the given id has children */ function hasChildren($id) { $element = $this->getElement($id); return $element['right']-$element['left']>1; // if the diff between left and right>1 then there are children } // // PRIVATE METHODS // /** * * * @access private * @version 2002/04/20 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param string the current where clause * @return */ function _getWhereAddOn( $addAfter=' AND ' , $tableName='' ) { if( $where=$this->getOption('whereAddOn') ) { return ' '.($tableName?$tableName.'.':'')." $where$addAfter "; } return ''; } // for compatibility to Memory methods function getFirstRoot() { return $this->getRoot(); } /** * gets the tree under the given element in one array, sorted * so you can go through the elements from begin to end and list them * as they are in the tree, where every child (until the deepest) is retreived * * @see &_getNode() * @access public * @version 2001/12/17 * @author Wolfram Kriesing <wolfram@kriesing.de> * @param integer $startId the id where to start walking * @param integer $depth this number says how deep into the structure the elements shall be retreived * @return array sorted as listed in the tree */ function &getNode( $startId=0 , $depth=0 ) { }}?>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?