时间:2021-07-01 10:21:17 帮助过:43人阅读
处理方法
/*
* ID: class FreeRoad
* Author: Joe Teng
* Notice: Infinite category maker.
*/
(
"host" => 'localhost', "user" => 'root', "password" => '123456', "dbname" => 'test'
);
$resDbc = mysql_connect ( $arrDatabase["host"], $arrDatabase["user"], $arrDatabase["password"] );
mysql_select_db( $arrDatabase['dbname'] );
if ( ! class_exists ( "FreeRoad" ))
{
class FreeRoad
{
var $resDbc ;
var $strDatabase ;
var $strMenuTable ;
var $strMenuTreeTable ;
var $strFiled_id = 'id' ;
var $strFiled_mainid = 'mainid' ;
var $strFiled_parentid = 'parentid' ;
var $strFiled_order = 'order' ;
var $strFiled_level = 'level' ;
function FreeRoad ( $resDbc , $strDatabase , $strMenuTable , $strMenuTreeTable , $arrSetFileds = array() )
{
$this->resDbc = $resDbc ;
$this->strDatabase = $strDatabase;
$this->strMenuTable = $strMenuTable ;
$this->strMenuTreeTable = $strMenuTreeTable ;
if ( sizeof ( $arrSetFileds ) > 0 )
{
$this->strFiled_id = $arrSetFileds['id'] ;
$this->strFiled_mainid = $arrSetFileds['mainid'] ;
$this->strFiled_parentid = $arrSetFileds['parentid'] ;
$this->strFiled_order = $arrSetFileds['order'] ;
$this->strFiled_level = $arrSetFileds['level'] ;
}
}
function get_new_mainid ()
{
mysql_select_db ( $this->strDatabase , $this->resDbc ) ;
$strQuery = " SELECT `$this->strFiled_mainid`
FROM `$this->strMenuTable`
WHERE `$this->strFiled_parentid` = 0
ORDER BY `$this->strFiled_id` DESC LIMIT 0 , 1 " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_array ( $resResult ) )
{
$intLastedMainId = $arrRow[0] ;
}
$intLastedMainId = intval ( $intLastedMainId );
mysql_free_result ( $resResult ) ;
return $intLastedMainId + 1 ;
}
function get_level_lastest_id ( $intParentId )
{
mysql_select_db ( $this->strDatabase , $this->resDbc ) ;
$strQuery = " SELECT `$this->strFiled_id`
FROM `$this->strMenuTable`
WHERE `$this->strFiled_parentid` = $intParentId
ORDER BY `$this->strFiled_id` DESC LIMIT 0 , 1 " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$intLevelLastestId = $arrRow[0] ;
}
mysql_free_result ( $resResult ) ;
return $intLevelLastestId ;
}
function get_level_lastest_order ( $intParentId )
{
mysql_select_db ( $this->strDatabase , $this->resDbc ) ;
$strQuery = " SELECT `$this->strFiled_order`
FROM `$this->strMenuTable`
WHERE `$this->strFiled_id` = $intParentId " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$floSelectItemOrder = $arrRow[0] ;
}
mysql_free_result ( $resResult ) ;
$strQuery = " SELECT `$this->strFiled_order`
FROM `$this->strMenuTreeTable`
WHERE BINARY ( `tree`) LIKE '%|$intParentId|%'
ORDER BY `$this->strFiled_order` DESC LIMIT 0 , 1 " ;
//echo $strQuery ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$floSelectItemLastestOrder = $arrRow[0] ;
}
mysql_free_result ( $resResult ) ;
if ( ! $floSelectItemLastestOrder ) $floSelectItemLastestOrder = $floSelectItemOrder ;
return $floSelectItemLastestOrder ;
}
function get_elements ( $intParentId = 0 )
{
mysql_select_db ( $this->strDatabase , $this->resDbc ) ;
if ( $intParentId == 0 )
{
$intMainId = $this->get_new_mainid ( );
return array ( "mainid" => $intMainId , "order" => 0 , "level" => 0 ) ;
}
$strQuery = " SELECT `$this->strFiled_mainid` , `$this->strFiled_order` , `$this->strFiled_level`
FROM `$this->strMenuTable`
WHERE `$this->strFiled_id` = $intParentId " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$intMainId = $arrRow[0] ;
$floOrder = $arrRow[1] ;
$intLevel = $arrRow[2] ;
}
mysql_free_result ( $resResult ) ;
if ( ! $intMainId ) return false ;
$intLevelLastestId = $this->get_level_lastest_id ( $intParentId ) ;
// get pre order
if ( $intLevelLastestId )
{
$floPreOrder = $this->get_level_lastest_order ( $intLevelLastestId );
// echo $floPreOrder ;exit;
}
else
{
$floPreOrder = $floOrder ;
}
// get next order
$strQuery = " SELECT `$this->strFiled_order`
FROM `$this->strMenuTable`
WHERE `$this->strFiled_mainid` = $intMainId AND `$this->strFiled_order` > $floPreOrder
ORDER BY `$this->strFiled_order` ASC LIMIT 0 , 1 " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$floNextOrder = $arrRow[0] ;
}
mysql_free_result ( $resResult ) ;
if ( ! $floNextOrder )
{
$floNewOrder = floor ( $floPreOrder + 1 ) ;
}
else
{
$floNewOrder = number_format ( ( $floPreOrder + $floNextOrder ) / 2 , 14 ) ;
}
$intNewLevel = $intLevel + 1 ;
return array ( "mainid" => $intMainId , "order" => $floNewOrder , "level" => $intNewLevel ) ;
}
function update_tree ( $intMainId , $intParentId , $floOrder )
{
if ( !$intParentId ) return false ;
mysql_select_db ( $this->strDatabase , $this->resDbc ) ;
$strQuery = " SELECT `tree`
FROM `$this->strMenuTreeTable`
WHERE `mainid` = $intMainId AND BINARY ( `tree`) LIKE '%|$intParentId|'
ORDER BY `order` DESC LIMIT 0 , 1 " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$strTree = $arrRow[0] ;
}
mysql_free_result ( $resResult ) ;
if ( ! $strTree )
{
$strQuery = " SELECT `$this->strFiled_parentid`
FROM `$this->strMenuTable`
WHERE `$this->strFiled_id` = $intParentId " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$intPreParentId = $arrRow[0] ;
}
mysql_free_result ( $resResult ) ;
if ( ! $intPreParentId )
{
$strPreTree = '';
}
else
{
$strQuery = " SELECT `tree`
FROM `$this->strMenuTreeTable`
WHERE `mainid` = $intMainId AND BINARY ( `tree`) LIKE '%|$intPreParentId|'
ORDER BY `order` DESC LIMIT 0 , 1 " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
while ( $arrRow = mysql_fetch_row ( $resResult ) )
{
$strPreTree = $arrRow[0] ;
}
mysql_free_result ( $resResult ) ;
}
$strNewTree = $strPreTree . '|'. $intParentId . '|' ;
$strQuery = " INSERT INTO `$this->strMenuTreeTable`
VALUES ( $intMainId, '$strNewTree', $floOrder ) " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
@mysql_free_result ( $resResult ) ;
return true ;
}
else
{
$strQuery = " UPDATE `$this->strMenuTreeTable`
SET `order` = $floOrder
WHERE `mainid` = $intMainId AND `tree` = '$strTree' " ;
$resResult = mysql_query ( &$strQuery , $this->resDbc ) ;
@mysql_free_result ( $resResult ) ;
return true ;
}
}
}
}
/*
$pFreeRoad = new FreeRoad ( $resDbc , $arrDatabase["dbname"] , 'menus' , 'menu_tree' ) ;
$info = 'change here';
$intParentId = change here ;
$arrItems = $pFreeRoad->get_elements( $intParentId ) ;
$intMainId = $arrItems['mainid'] ;
$floOrder = $arrItems['order'] ;
$intLevel = $arrItems['level'] ;
$strQuery = " INSERT INTO `menus` VALUES ( '' , $intMainId , $intParentId , $floOrder , $intLevel, '$info' ) " ;
$resResult = mysql_query ( &$strQuery , $resDbc ) ;
$pFreeRoad->update_tree ( $intMainId , $intParentId , $floOrder ) ;
@mysql_close( $resDbc ) ;
*/
?>
include "freeroad.class.php";
$strQuery = " SELECT * FROM `menus` ORDER BY `mainid` ASC , `order` ASC ";
$resResult = mysql_query ( &$strQuery , $resDbc ) ;
while ( $arrRows = mysql_fetch_array ( $resResult ))
{
$intLevel = $arrRows["level"] ;
$strSpace = '' ;
for ( $i = 0 ; $i <= $intLevel ; $i++ )
{
$strSpace .= " ";
}
if ( $i>1 )
{
$strSpace .= '--';
}
echo $strSpace . $arrRows["id"] . $arrRows["info"] ."
";
}
if ( $_GET["action"] == 'add' )
{
$pFreeRoad = new FreeRoad ( $resDbc , $arrDatabase["dbname"] , 'menus' , 'menu_tree' ) ;
$info = 'F1';
$intParentId = 1 ;
$arrItems = $pFreeRoad->get_elements( $intParentId ) ;
$intMainId = $arrItems['mainid'] ;
$floOrder = $arrItems['order'] ;
$intLevel = $arrItems['level'] ;
$strQuery = " INSERT INTO `menus` VALUES ( '' , $intMainId , $intParentId , $floOrder , $intLevel, '$info' ) " ;
$resResult = mysql_query ( &$strQuery , $resDbc ) ;
$pFreeRoad->update_tree ( $intMainId , $intParentId , $floOrder ) ;
@mysql_close( $resDbc ) ;
}
?>
容易可以看出,输出的时候是如此简单便实现树结构了:
SELECT * FROM `menus` ORDER BY `mainid` ASC, `order` ASC ;
前文输出写成输入了~~~ 晕倒。。
输出的时候,根据level来做树结构。