31.6 查看单个的文章

display_tree()函数的调用将会给出一组文章的链接。如果我们点击其中的某篇文章,将进入view_post.php脚本程序,同时传递给该脚本这篇文章的postid。该脚本的示例输出如图31-7所示。

31.6 查看单个的文章 - 图1

图 31-7 现在,我们可以看到该帖子的消息正文

view_post.php脚本如程序清单31-6所示,除了显示该消息的回复外,还可以显示该消息的正文。可看到这些回复也是以树形结构显示出来的,不过这个树形结构已经被完全展开了,并且不带有加号或减号按钮。这是$sublist开关变量起作用的结果。

程序清单31-6 view_post.php——显示单个消息正文


<?php

//include function libraries

include('include_fns.php');

$postid=$_GET['postid'];

//get post details

$post=get_post($postid);

do_html_header($post['title']);

//display post

display_post($post);

//if post has any replies,show the tree view of them

if($post['children']){

echo"<br/><br/>";

display_replies_line();

display_tree($_SESSION['expanded'],0,$postid);

}

do_html_footer();

?>


该脚本主要调用3个函数来实现其功能:get_post()、display_post()和display_tree()函数。get_post()函数将一条消息细节从数据库中取出来。该函数的代码如程序清单31-7所示。

程序清单31-7 discussion_fns.php函数库中的get_post()函数——从数据库中取出一则消息


function get_post($postid){

//extract one post from the database and return as an array

if(!$postid){

return false;

}

$conn=db_connect();

//get all header information from'header'

$query="select*from header where postid='".$postid."'";

$result=$conn->query($query);

if($result->num_rows!=1){

return false;

}

$post=$result->fetch_assoc();

//get message from body and add it to the previous result

$query="select*from body where postid='".$postid."'";

$result2=$conn->query($query);

if($result2->num_rows>0){

$body=$result2->fetch_assoc();

if($body){

$post['message']=$body['message'];

}

}

return$post;

}


该函数根据一个给定的postid执行两个查询语句,为该文章取出消息的标题和正文,并将它们一起放在一个相关的数组中返回。

接下来,该函数返回结果将传递给output_fns.php函数库中的display_post()函数,该函数仅以某种HTML格式将数组打印出来,因此这里我们不再讨论。

最后,view_post.php脚本将检查该文章是否有回复并调用display_tree()函数以子列表的格式显示它们——即不带加号或减号并且完全展开的形式。