Specifications
If we are trying to expand a particular thread, we will have been passed a postid via $expand.
We therefore add a new entry to the $expanded array to reflect this.
The expand_all() function is shown in Listing 29.3.
LISTING 29.3 expand_all() Function from discussion_fns.php— Processes the $expanded
Array to Expand All the Threads in the Forum
function expand_all(&$expanded)
{
// mark all threads with children as to be shown expanded
$conn = db_connect();
$query = “select postid from header where children = 1”;
$result = mysql_query($query);
$num = mysql_numrows($result);
for($i = 0; $i<$num; $i++)
{
$expanded[mysql_result($result, $i, 0)]=true;
}
}
This function runs a database query to work out which of the threads in the forum have replies,
as follows:
select postid from header where children = 1
Each of the articles returned is then added to the $expanded array. We run this query to save
time later. We could simply add all articles to the expanded list, but it would be wasteful to try
processing replies that do not exist.
Collapsing the articles works in a similar but opposite way, as follows:
if($collapse)
{
if($collapse==”all”)
unset($expanded);
else
unset($expanded[$collapse]);
}
You can remove items from the $expanded array by unsetting them. We remove the thread that
is to be collapsed, or unset the entire array if the entire page is to be collapsed.
All this is preprocessing, so we know which articles should be displayed and which should not.
The key part of the script is the call to
display_tree($expanded);
which will actually generate the tree of displayed articles.
Building Web Forums
C
HAPTER 29
29
BUILDING WEB
FORUMS
723
35 7842 CH29 3/6/01 3:34 PM Page 723