手机端宝贝分类分类树如何实现

京东分类树怎么能在ecshop中实现?_百度知道
京东分类树怎么能在ecshop中实现?
ecshop中需要做哪些修改呢://b.jpg" target="_blank" title="点击查看大图" class="ikqb_img_alink"><img class="ikqb_img" src="http。如果能够在ecshop中完好实现。.com/zhidao/pic/item/91ef76c6a7efce1b245adeb58f65de://b://b求教京东分类树如何实现的。。求高人回答,将追加高分(50分以上)。<a href="/zhidao/wh%3D600%2C800/sign=7ab128cebaa294fc6a7efce1b245adeb58f65de.baidu.hiphotos.jpg" esrc="http.baidu。./zhidao/wh%3D450%2C600/sign=3eded21b799c26e/91ef76c6a7efce1b245adeb58f65de?用在&nbsp.hiphotos
如果能加一下QQ聊聊将会更好了。。我Q
我有更好的答案
按默认排序
可以把京东的js和html代码扣下来,在用foreach嵌套循环就能实现了!
其他类似问题
您可能关注的推广回答者:
京东的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁无限分类与树型论坛的实现方法
无限分类与树型论坛的实现方法
无限分类与树型论坛的实现方法
――浮点型字段排序法
& && && && &Joe Teng&&
在此我不想讨论其他实现方法的利与弊。
既然是使用字段排序,那么我们便设一个名为order的字段。问题是,在这里是使用整数还是使用浮点数类型呢?考虑到会有在两个连续order值中间插入新值的可能,自然是需要使用浮点类型了。
建一个menus表,我们还需要以下字段:
id : 类别编号
mainid : 主分类编号,但不作具体分类使用。如果在树型论坛里,它代表的是主题id
parentid : 父类编号
level : 类别级别,作用其实是方便显示的时候作其他处理
info : 类别名称等。
由此可以得到menus的表结构:
CREATE TABLE `menus` (
`id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT ,
`mainid` INT( 10 ) UNSIGNED NOT NULL ,
`parentid` INT( 10 ) UNSIGNED NOT NULL ,
`order` FLOAT UNSIGNED NOT NULL ,
`level` SMALLINT( 5 ) UNSIGNED NOT NULL ,
`info` VARCHAR( 128 ) NOT NULL ,
INDEX ( `mainid` , `parentid` , `order` , `level` ) ,
) TYPE = MYISAM ;
很容易可以看出,输出的时候是如此简单便实现树结构了:
SELECT * FROM `menus` ORDER BY `mainid` ASC, `order` ASC ;
前提是添加类别的时候,order能正确排序。
添加根分类:
很简单,取得上一个主类的mainid, 如A_mainid,则新根分类的mainid则为A_mainid + 1。parentid 为 0 , order 为0, level也为0, info则自行设定。
添加子分类:
核心思想是,取得新增子分类的前一个分类的order以及它后一个分类的order。
取得前一个分类的order是这里的难点,因为涉及到同级与非同级的情况。非同级的情况很简单,新增别类的前一个order其实就是它的父类的order。如果有同级分类,情况就很复杂了,因为它前面的同级分类有可能会拥有子分类,子分类下又可能还会有子分类,如此下来,要取得前一个order就很难了。
解决的办法有两个:
1.取得新增类同级的前一个类别,如类别A的ID,使用递归的方法,直到取得A类别下最后最小分类的order,那便是要新增分类的前一个order了。这种方法的缺点是,如果A类别下有很多子分类,那么递归需要一定的时间。这种方法适用于普通的分类处理,不适用于树型论坛。不过总体来说,因为是添加类别的时候才使用递归,输出类别的时候跟前面一样,效率还是很高的。
2.作一个记录,记录着与A有关联的最后order。于是我们就需要增加一个表,建利关系树。这种关系树做起来很简单。表结构如下:
CREATE TABLE `menu_tree` (
&&`mainid` int(10) unsigned NOT NULL default '0',
&&`tree` text NOT NULL,
&&`order` float unsigned NOT NULL default '0',
&&KEY `mainid` (`mainid`,`order`),
&&FULLTEXT KEY `tree` (`tree`)
) TYPE=MyISAM;
(构建方式请看我后面给出的源码)
取得前一个order之后,要取得后一个order就很简单了。取同mainid下大于前一个order的最小order便是了。如果存在后一个order,那么新增order就取前一个order与后一个order的平均值。如果不存在后一个order,那说明新增类别是main下的最小order,取大于前一个order的最小整数就行了。
主要实现方法便如上面说的。
处理方法:CODE:&?php
* ID: class FreeRoad
* Author: Joe Teng &Joe_&;
* Notice: Infinite category maker.
$arrDatabase = array
& & &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 $resD
& && &&&var $strD
& && &&&var $strMenuT
& && &&&var $strMenuTreeT
& && &&&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& && && &&&= $resD
& && && && &$this-&;strDatabase& && &= $strD
& && && && &$this-&;strMenuTable& &&&= $strMenuT
& && && && &$this-&;strMenuTreeTable = $strMenuTreeT
& && && && &
& && && && &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 $intLevelLastestI
& && &&&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 $strQ
& && && && &$resResult = mysql_query ( &$strQuery , $this-&;resDbc ) ;
& && && && &while ( $arrRow = mysql_fetch_row ( $resResult ) )
& && && && &{
& && && && && & $floSelectItemLastestOrder = $arrRow[0] ;
& && && && &}
& && && && &mysql_free_result ( $resResult ) ;
& && && && &if ( ! $floSelectItemLastestOrder ) $floSelectItemLastestOrder = $floSelectItemO
& && && && &return $floSelectItemLastestO
& && &&&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 )
& && && && &$intLevelLastestId = $this-&;get_level_lastest_id ( $intParentId ) ;
& && && && &// get pre order
& && && && &if ( $intLevelLastestId )
& && && && &{
& && && && && & $floPreOrder = $this-&;get_level_lastest_order ( $intLevelLastestId );
& && && && && &// echo $floPreO
& && && && &}
& && && && &else
& && && && &{
& && && && && & $floPreOrder = $floO
& && && && &}
& && && && &
& && && && &// 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 )
& && && && &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 ) ;
& && && && && &
& && && && &}
& && && && &else
& && && && &{
& && && && && & $strQuery& &= & UPDATE `$this-&;strMenuTreeTable`
& && && && && && && && && && &&&SET `order` =&&$floOrder
& && && && && && && && && && &&&WHERE `mainid` = $intMainId AND `tree` = '$strTree' & ;
& && && && && & $resResult&&= mysql_query ( &$strQuery , $this-&;resDbc ) ;
& && && && && & @mysql_free_result ( $resResult ) ;
& && && && && &
& && && && &}
$pFreeRoad = new FreeRoad ( $resDbc , $arrDatabase[&dbname&] , 'menus' , 'menu_tree' ) ;
$info = 'change here';
$intParentId =
$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 ) ;
昵称: Joe Teng &时间:
CODE:&?php
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 &= $intL $i++ )
& && &&&$strSpace .= &  &;
& & if ( $i&;1 )
& && & $strSpace .= '--';
& & echo $strSpace .&&$arrRows[&id&] . $arrRows[&info&] .&&br&;&;
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 ) ;
昵称: Joe Teng &时间:
昵称: wsswan &时间:
昵称: Joe Teng &时间:
昵称: wsswan &时间:
昵称: Joe Teng &时间:
昵称: wsswan &时间:
昵称: Joe Teng &时间:
昵称: Joe Teng &时间:
昵称: Joe Teng &时间:
昵称: imbiss &时间:
原帖由 &imbiss& 发表:
递推的原理简单。但是效率不高。在处理n级的时候,需要查询数据库n次。
与之相对,还有一种方法,即左右值的方法,只要查询一次即可。
具体参见 /index.php?show=blog&id=75不瞒您说,两年前当我看这篇文章的时候就已经有一大批的业余程序员被其误倒,没想到今天这篇文章竟然还在害人。而且我也怀疑您是否通读(或着说读懂了)这篇文章。目前还没有一个查询就能把所有信息都查出来,而且带结构的算法,除非您不用库,用 XML ,而 XML engine 处理上也是递归。
另外 RDBMS 效率肯定比任何一个 Script Aplication 速度要块,资源要省。其次,递归‘效率不高’的传说就如同‘汇编程序一定要比 C 程序小(快)’的传说一样,都是听说的,一般的语言在处理递归的时候一般都跟 fork() 一样,不修改不拷贝,也就是我上面说的同样多的水是放在桶里还是放在盆里的问题,其实水都是这么多:)
昵称: wsswan &时间:
昵称: Joe Teng &时间:
原帖由 &wsswan& 发表:
不瞒您说,两年前当我看这篇文章的时候就已经有一大批的业余程序员被其误个查询就能把所有信息都查出来,而?.........感情我被误导了。我觉得这篇文章没有错啊。他说的递归方法我实际上在用了。
左右值的方法,我也实验过了,没错啊。你说误导在哪里呢?
昵称: imbiss &时间:
昵称: wsswan &时间:
原帖由 &imbiss& 发表:
感情我被误导了。我觉得这篇文章没有错啊。他说的递归方法我实际上在用了。
左右值的方法,我也实验过了,没错啊。你说误导在哪里呢?说两年实在是有点过,在 chinaunix 的精华区里就有这篇文章的讨论,时间是 14:34:43,当时&&shukebeita 的说法是‘如何用数据库保存多级结构的数据’,根本就没说树。
您的错误的确是误导。多极结构和树还是不一样的。要不然 C 的在结构体中定义指向结构体的指针岂不是也是树了……
所以人家说多级结构我也就不多出这一口气了,而您要是说树……等等,我先把《数据结构》翻出来
1)&&毗邻目录模式(adjacency list model):可以理解为先跟遍历(按广度遍历)。预排序遍历树算法(modified preorder tree traversal algorithm) 可以理解为:插错了就是图的双向链表。而树插错了还是树。
1.5) 右值可以算出来,但是不能给出。因为给出了插错了就是物理错误。
2)&&在一棵树中,有且仅有一个跟节点。food 是逻辑节点,我们不能把一个论坛当作节点吧……您要是说版(board)是跟节点,帖子(post)是子节点,那我这句话就算是没说:)
2.5)&&一开始用先跟遍历,到后来用后跟遍历记数
3)&&左右值的是典型的双链表操作,父节点是典型的树操作。您知道典型的图操作是什么么:)链表是针对 C 语言指针的典型算法,PHP 没有指针,定义结构体也很困难。
4) 树型转线性结构是为了存入关系型数据库。链表(还是双链)转树形是为了什么,怎么控制树存成图。
当然,理论上的东西深究起来无穷无尽,我向您认错,您也可以跟我一样抱着本算法书挑活人的错,呵呵。
--------------
说正经的,如果要是理解为树的话:
我认为误导的是插入一个‘节点(node)’的操作。可这个实际上是入链的操作。也就是说一个值插入一个链的中间 n ,那么 n 到 tail 之间的值加一。这么说没什么,可实际上要是修改第一个帖子(就是 Fruit ),在插入之前如果需要有个 left join 什么的查询,再检查个把 Foreign Key (比如查询用户信息吧),然后就开始又加又插的(几乎是对整个库操作),这不又是一个 worm santy&&诞生了……还是您自己写的,这不是给自己刨坑么
树形结构加入一个节点的操作很简单:
1)new_node.child = old_node.id(继承子孙,通常子孙大于等于两个)
2)new_node.parent = old_node.parent(继承祖先)
3)old_node.parent = new_node.id(承认父节点)
至于提取是数据库查询还是脚本处理我就不叫真了,我推荐数据库查询。
昵称: wsswan &时间:
昵称: Joe Teng &时间:
昵称: Joe Teng &时间:
昵称: powerpolly &时间:
昵称: Joe Teng &时间:
昵称: powerpolly &时间:
昵称: geel &时间:
昵称: Joe Teng &时间:
昵称: Joe Teng &时间:
昵称: imbiss &时间:
昵称: Joe Teng &时间:
昵称: Joe Teng &时间:
CODE:&?php
* ID: class FreeRoad
* Author: Joe Teng &Joe_&;
* Notice: Infinite category maker.
$arrDatabase = array
& & &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 $resD
& && &&&var $strD
& && &&&var $strT
& && &&&var $arrFileds&&= array ( ) ;
& && &&&function FreeRoad ( $resDbc , $strDatabase , $strTable , $arrSetFileds = array( ) )
& && && && &$this-&;resDbc& && &= $resD
& && && && &$this-&;strDatabase = $strD
& && && && &$this-&;strTable& & = $strT
& && && && &$arrFileds = array
& && && && &(
& && && && && & &id&& && & =&; &id&,
& && && && && & &mainid&& &=&; &mainid&,
& && && && && & &parentid& =&; &parentid&,
& && && && && & &level&& & =&; &level&,
& && && && && & &map&& && &=&; &map&
& && && && &);
& && && && &
& && && && &$this-&;arrFileds = array_merge ( $arrFileds , $arrSetFileds ) ;
& && &&&function get_new_mainid ()
& && && && &mysql_select_db ( $this-&;strDatabase , $this-&;resDbc ) ;
& && && && &$strQuery&&= & SELECT `{$this-&;arrFileds['mainid']}`&&
& && && && && && && && && &FROM `$this-&;strTable`&&
& && && && && && && && && &WHERE `{$this-&;arrFileds['parentid']}` = 0
& && && && && && && && && &ORDER BY `{$this-&;arrFileds['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_elements ( $intParentId = 0 )
& && && && &mysql_select_db ( $this-&;strDatabase , $this-&;resDbc ) ;
& && && && &if ( $intParentId == 0 )
& && && && &{
& && && && && & $intMainId = $this-&;get_new_mainid ( );
& && && && && & return array ( &mainid& =&; $intMainId , &level& =&; 0 ) ;
& && && && &}
& && && && &$strQuery&&= & SELECT `{$this-&;arrFileds['mainid']}` , `{$this-&;arrFileds['level']}` , `{$this-&;arrFileds['map']}`
& && && && && && && && && &FROM `$this-&;strTable`&&
& && && && && && && && && &WHERE `{$this-&;arrFileds['id']}` = $intParentId & ;
& && && && &$resResult = mysql_query ( &$strQuery , $this-&;resDbc ) ;
& && && && &while ( $arrRow = mysql_fetch_row ( $resResult ) )
& && && && &{
& && && && && & $intMainId = $arrRow[0] ;
& && && && && & $intLevel&&= $arrRow[1] ;
& && && && && & $strMap& & = $arrRow[2] ;
& && && && &}
& && && && &mysql_free_result ( $resResult ) ;
& && && && &if ( ! $intMainId )
& && && && &$strQuery = & SELECT COUNT(`{$this-&;arrFileds['id']}`)
& && && && && && && && &&&FROM `$this-&;strTable`&&
& && && && && && && && &&&WHERE `{$this-&;arrFileds['parentid']}` = $intParentId & ;
& && && && &$resResult = mysql_query ( &$strQuery , $this-&;resDbc ) ;
& && && && &while ( $arrRow = mysql_fetch_row ( $resResult ) ) $intChildNum = $arrRow[0] ;
& && && && &mysql_free_result ( $resResult ) ;
& && && && &$intChildNum = intval( $intChildNum ) + 1 ;
& && && && &$intLevel++;
& && && && &$strNewMap = $strMap . '[' . $intChildNum . ']' ;
& && && && &return array ( &mainid& =&; $intMainId , &level& =&; $intLevel , &map& =&; $strNewMap ) ;
//添加数据
$pFreeRoad = new FreeRoad ( $resDbc , $arrDatabase[&dbname&] , 'menus'&&) ;
$info = 'Sports'; // change here
//取得parentid , 测试时自己赋值
$intParentId = 0 ; // change here
//取得相关参数
$arrItems&&= $pFreeRoad-&;get_elements( $intParentId ) ;
$intMainId = $arrItems['mainid'] ;
$intLevel&&= $arrItems['level'] ;
$strMap& & = $arrItems['map'] ;
//写入数据库
$strQuery&&= & INSERT INTO `menus` VALUES ( '' , $intMainId , $intParentId , $intLevel, '$strMap' , '$info' ) & ;
$resResult = mysql_query ( &$strQuery , $resDbc ) ;
//输出示例
$strQuery = & SELECT * FROM `menus` ORDER BY `mainid` DESC , `map` ASC &;
$resResult = mysql_query ( &$strQuery , $resDbc ) ;
while ( $arrRows = mysql_fetch_array ( $resResult ))
& & $intLevel = $arrRows[&level&] ;
& & $strSpace = str_repeat( &&&&, $intLevel ) ;
& & if ($intLevel &; 0 )
& && & $strSpace .= '--';
& & echo $strSpace .&&$arrRows[&id&] . $arrRows[&info&] .&&br&;&;
@mysql_close( $resDbc ) ;
昵称: Joe Teng &时间:
原帖由 &Joe Teng& 发表:
2维数组?说说你的实现方法
:)产生我上面的那个站点地图就用这一个函数str_findchild()就完了。
$maincode .= str_findchild(-1,-1);//起始节点是-1,代表根目录,起始层数设为-1CODE:function str_findchild($id,$stufe=1){& & & &
& & & & $code =&&;
& & & & $db = new DB_S
& & & & $db-&;connect();
& & & & $sql = &SELECT * FROM table where praentid = &.$id.& AND status = 1 ORDER BY Titel ASC&; //status=1就是表示显示,否则
& & & & $db-&;query($sql);
& & & & if($db-&;nf()) //
& & & & {& & & & $stufe ++;//就是层数加一& & & &
& & & & & & & & for ($i=0;$i& $ $i++)
& && && && && && && && && && && && &&&{$code .=&\t&;//为了html源码缩进排版好看,没有别的用处}
& && && && &&&& & & & & & & &
& & & & & & & & $code .=&\n&ul&;\n&;
& & & & & & & & while($db-&;next_record())
& & & & & & & & {//存在子节点
& & & & & & & & & & & & $child = $db-&;f(&id&);//子节点编号
& & & & & & & & & & & & $titel = $db-&;f(&Titel&);//子节点标题
& & & & & & & & & & & & for ($i=0;$i& $stufe+1; $i++){$code .=&\t&;//排版用,没有实际作用}& & & & & & & & & & & &
& & & & & & & & & & & & $code .= &&li&;&a href=\&view.php?aid=&.$child.&\&&;&.$titel.&&/a&;&;& & & & & & & &
& & & & & & & & & & & & $code .= str_findchild($child,$stufe);//关键!函数调用自身,即递归调用。看这个子节点还有没有子子节点,同时把自己所在的层数作为参数传递下去。
& & & & & & & & }
& & & & & & & & $code .= &&/ul&;\n&;
& & & & & & & &
& & & & & & & & if($stufe != 0){$code .= &&/li&;\n&;}
& & & & & & & & for ($i=0;$i& $stufe-1; $i++){$code .= &\t&;}
& & & & else{& & & & & & & &
& & & & & & & & $code .= &&/li&; \n&;
& & & & & & & & $stufe --;& & & & // 层数向上退出一级
& & & & }& & & &
& & & & $db-&;free();
& & & & return $// 返回本级代码
}数据库里就记录
id,titel,parent_id
就完了。不必记录level。level是在递归的过程中自己计算出来的。
昵称: imbiss &时间:
昵称: Joe Teng &时间:
原帖由 &Joe Teng& 发表:
你还是用递归啊。我决定采用整数字段排序
当插入位于中间位置的值时,后面的order递增1。
从效率上讲, 我用奔三700 , 256内存的机器,update 99999行数据用了几秒钟。而我想出现这么多子类(回贴)的机率是很低的?.........插入一个帖子为什么要其它帖子都要update呢??我觉得你的指导思想有问题。
昵称: imbiss &时间:
昵称: Joe Teng &时间:
昵称: imbiss &时间:
昵称: edwardcj &时间:
原帖由 &edwardcj& 发表:
显然楼主的目的是为了在显示时, 新插入的帖子可以出现在正确的地方.显然这是没有必要的。一般的帖子都会记录时间。select的时候按时间排序自然就可以有一定的顺序。不必再人为排位。
对于1。太简单了,不予回答。
对于2。构造selcec语句t的时候可以灵活的按照要求排序。
对于3。没有明白。何谓快速?
对于4.&&自己写个分页类或者函数就不必用select的limit.
昵称: imbiss &时间:
昵称: edwardcj &时间:
原帖由 &edwardcj& 发表:
对于3,我的意思是:
如果用于排序的字段是连续的, 比如从0开始
那么第n页的帖子该字段救在 n*每页帖子数...n*每页帖子数+每页帖子数
之间, 数据库就很快可以定位到指定帖子了
但是如果某中间主题帖子有了新回复, ..........这个你不用担心。采用递归是按照寻找某一个帖子的子孙贴这样一直循环下去。父贴会带出子孙贴,父贴永远会在子孙贴的前面。
至于不用limit就更简单了。往往我们把帖子封装在类内。,比如post类吧。
用一个函数,取出某一个要求的所有帖子的所有id,然后更具分页设置,算出需要多少页,当前是第几页,然后写个循环,调用类似$post-&;getPost($id)把帖子取出。根本不要limit。
至于你说update的,我告诉你,需要update的方法根本就是错的。按照他的思路update的帖子会随着帖子数的增加而增加。后果就是,越来越慢。
昵称: imbiss &时间:
原帖由 &imbiss& 发表:
这个你不用担心。采用递归是按照寻找某一个帖子的子孙贴这样一直循环下去。父贴会带出子孙贴,父贴永远会在子孙贴的前面。
至于不用limit就更简单了。往往我们把帖子封装在类内。,比如post类吧。
用一个函数,..........好吧, 先来说关于update的问题, 楼主的意思是使用一句select就把该页帖子按显示顺序全部取出而非使用递归, 所以使用update来调整次序, 当然我并不认为这样做很好, 因为可以有不用update而同样可以一句select语句办到的.
关于我提的问题, 我想说具体一点:
假如在空的版面里, 依次有人贴了A,B,C,D,E五个主题贴, 显然显示应该为
这时有人回复了C帖, C1, 那显示应该为
& &&&C1&&--&&该帖不用显示
同样, 现在有人回复了B贴, B1, 显示应该为
& &&&B1&&--&&该帖不用显示
& &&&C1&&--&&该帖不用显示
现在假如我一页显示一个帖子, 要直接显示第3页帖子
如何快速知道第三页的起始帖子是 E 呢?
不希望从B, C, E这样自己数过来
因为不太明白您说的, 所以是否可以麻烦您解释的详细一点?谢谢
昵称: edwardcj &时间:
昵称: imbiss &时间:}

我要回帖

更多关于 家园树家长端 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信