Home Backend Development PHP Tutorial 非递归法实现论坛树型结构及分页!!(心血结晶啊,呵呵)_PHP

非递归法实现论坛树型结构及分页!!(心血结晶啊,呵呵)_PHP

Jun 01, 2016 pm 12:42 PM
accomplish structure forum

现将本人的实践结果show给大家,不足之处就是分页的方法不太好,不能显示具体的页数,可实在又没有其它更好的解决办法,只好先如此了,如果哪位有类似本论坛的分页方法,表赐教一二,二泉不胜感激!具体可访问我的个人小网站:http://web.nyist.net/~wbgwrq,不废话了,开始吧......

//表的结构如下:
//creat.sql
//简单说明:RootId 论题序数;Layer:帖子层次,缩进的依据;Orders:帖子的顺序
CREATE TABLE over_post (
  id int(11) NOT NULL auto_increment,
  title varchar(80) NOT NULL default ',
  content text,
  postat datetime NOT NULL default '0000-00-00 00:00:00',
  readed int(11) NOT NULL default '0',
  size int(11) NOT NULL default '0',
  rootid int(11) NOT NULL default '0',
  orders int(4) NOT NULL default '0',
  layer int(4) NOT NULL default '0',
  PRIMARY KEY  (id)
) TYPE=MyISAM;
//creat.sql End

//发表根帖,即RootId,Layer,Orders为0的帖子
//said.php
//begin


  
    
      
      
    
    
      
      
    
    
      
    
    
      
    
  

        
你的想法:
        

      

        
      

        
你的内容:
        

      

        
      

      

        

          
          
        

      

  

//said.php End


//帖子内容,且在本页进行跟帖
//content.php
//Begin

$result=mysql_query("select

over_post.title,over_post.content,over_post.postat,over_post.readed,over_post.rootid,over_post.la

yer,over_post.orders from over_post where over_post.id=$id");
$readed=mysql_result($result,0,"readed");
$title=mysql_result($result,0,"title");
$content=mysql_result($result,0,"content");
$date=mysql_result($result,0,"postat");
$rootid=mysql_result($result,0,"rootid");
$orders=mysql_result($result,0,"orders");
$layer=mysql_result($result,0,"layer");
?>

  
   
  
  
     
     
              
   
     
     
   
发表人:
主 题: 》  

Readed:】  
内 容:




  论坛发表跟帖
  

    
      
        
        
      
      
        
        
      
      
        
      
      
        
      
    

          
题目:

        

          
        

          
内容:

        

          
        

        

          

          
          
          
          
          
          

        

//content.php End

//更新数据库
//post.php
//Begin


$content=nl2br(htmlspecialchars($content));
$title=htmlspecialchars($title);        //决不允许在标题上使用html
$date=date("Y-m-d H:i:s");
$length=strlen($content);

if(isset($said))    //发表新帖子
{
$query="insert into over_post

values(null,'$title','$content',$user_id,'$date',0,$length,$img,',',')";
$result=mysql_query($query) or die(mysql_error());
$r=mysql_query("select max(id) from over_post");
$rootid = mysql_result($r,0)+0;
mysql_query("update over_post set rootid=$rootid where ID=$rootid")or die(mysql_error());
}

if(isset($reply)):    //发表跟帖

mysql_query("update over_post set orders=orders+1 where rootid=$rootid and orders>$orders")or

die(mysql_error());

$layer=$layer+1;
$orders=$orders+1;
$query="insert into over_post

values(null,'$title','$content',$user_id,'$date',0,$length,$img,$rootid,$orders,$layer)";

$result=mysql_query($query) or die(mysql_error());

endif;
     if($result) {
    include"list.php";
}
?>
//post.php End

//重头戏,显示所有帖子,并实现分页
//list.php
//Begin

//找到最新论题的rootid
$query = "select max(rootid) as maxid1, min(rootid) as minid1 from over_post";
$result = mysql_query($query);
$maxid1 = mysql_result($result, 0, "maxid1");
$startid1 = mysql_result($result, 0, "minid1");
if(!($maxid1>0)) $maxid1=0;
if(!($startid1>0)) $startid1=0;
$totalid1 = $maxid1; //这是真正的最大的rootid值, $maxid1要根据$nextmaxid1变的
if($nextmaxid1>0) $maxid1=$nextmaxid1; //翻页

//计算最小rootid:注意下面的desc,与limit结合,保证选取记录的范围.
//如果使用asc, 在mysql_result中检索第0个,将大大错误!
$itemsperpage=30;
$query="select distinct rootid from over_post where rootid
$itemsperpage";
$r=mysql_query($query);
$n=mysql_num_rows($r);
if($n>0) {
  $minid1=mysql_result($r,$n-1);
$query="select * from over_post where rootid=$minid1 order by rootid

desc,orders";
$result=mysql_query($query);
$num=mysql_num_rows($result);

}
else {
  $minid1=0;
  $maxid1=0;
  echo "

没有更多的发言内容
";
}

$query="select distinct rootid from over_post where rootid>$maxid1 order by rootid limit

$itemsperpage";
$r=mysql_query($query);
$n=mysql_num_rows($r);
if($n>0) $up=mysql_result($r,$n-1);
else $up=$totalid1;

$query="select distinct rootid from over_post where rootid
$itemsperpage";
$r=mysql_query($query);
$n=mysql_num_rows($r);
if($n>0) $down=mysql_result($r,0);
else $down=$maxid1;
?>



  
    
      
    
    
      
    
    
      
    
  

href="javascript:window.location.reload()" class=a1>刷新
        首页

上页

        下页  
:$startid1-$totalid1 ";?>
        

echo"
    ";
    while ($array=mysql_fetch_array($result)){
            $id=$array["id"];
            $title=$array["title"];
            $content=$array["content"];
            $postat=$array["postat"];
            $readed=$array["readed"];
            $size=$array["size"];
                if($size==0) $size="无内容";
                    else $size.=" Bytes";
            $rootid=$array["rootid"];
            $orders=$array["orders"];
            $layer=$array["layer"];
                
            $ul="";        //开始树型结构
            $_ul="";
        for($j=0;$j             $ul=$ul."
      ";
                  $_ul=$_ul."
    ";    
                }
    echo $ul."
  • "."$title($size) 【

    ".作者."】 $postat  

    ".$_ul;
    flush();
                    //树型结构结束
    }
    ?>

href="javascript:window.location.reload()" class=a1>刷新
        首页

上页

        下页  
:$startid1-$totalid1 ";?>
        



//list.php End
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement dual WeChat login on Huawei mobile phones? How to implement dual WeChat login on Huawei mobile phones? Mar 24, 2024 am 11:27 AM

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

How to implement the WeChat clone function on Huawei mobile phones How to implement the WeChat clone function on Huawei mobile phones Mar 24, 2024 pm 06:03 PM

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

PHP Programming Guide: Methods to Implement Fibonacci Sequence PHP Programming Guide: Methods to Implement Fibonacci Sequence Mar 20, 2024 pm 04:54 PM

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

Discuz Forum Permission Management: Read Permission Setting Guide Discuz Forum Permission Management: Read Permission Setting Guide Mar 10, 2024 pm 05:33 PM

Discuz forum permission management: Read the permission setting guide In Discuz forum management, permission setting is a crucial part. Among them, the setting of reading permissions is particularly important, as it determines the scope of content that different users can see in the forum. This article will introduce in detail the reading permission settings of the Discuz forum and how to flexibly configure it for different needs. 1. Basic concepts of reading permissions In the Discuz forum, reading permissions mainly include the following concepts that need to be understood: Default reading permissions: Default after new user registration

Master how Golang enables game development possibilities Master how Golang enables game development possibilities Mar 16, 2024 pm 12:57 PM

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.

PHP Game Requirements Implementation Guide PHP Game Requirements Implementation Guide Mar 11, 2024 am 08:45 AM

PHP Game Requirements Implementation Guide With the popularity and development of the Internet, the web game market is becoming more and more popular. Many developers hope to use the PHP language to develop their own web games, and implementing game requirements is a key step. This article will introduce how to use PHP language to implement common game requirements and provide specific code examples. 1. Create game characters In web games, game characters are a very important element. We need to define the attributes of the game character, such as name, level, experience value, etc., and provide methods to operate these

What are the syntax and structure characteristics of lambda expressions? What are the syntax and structure characteristics of lambda expressions? Apr 25, 2024 pm 01:12 PM

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

Detailed explanation of using Golang to implement data export function Detailed explanation of using Golang to implement data export function Feb 28, 2024 pm 01:42 PM

Title: Detailed explanation of data export function using Golang. With the improvement of informatization, many enterprises and organizations need to export data stored in databases into different formats for data analysis, report generation and other purposes. This article will introduce how to use the Golang programming language to implement the data export function, including detailed steps to connect to the database, query data, and export data to files, and provide specific code examples. To connect to the database first, we need to use the database driver provided in Golang, such as da

See all articles