85
761
Viewing the Tree of Articles
This script uses the following three variables to do its job:
n
The session variable
expanded
,which keeps track of which threads are expanded.
This variable can be maintained from view to view,so you can have multiple
threads expanded.The
expanded
variable is an associative array that contains the
postid
of articles that will have their replies expanded.
n
The parameter
expand
,which tells the script which new threads to expand.
n
The parameter
collapse
,which tells the script which threads to collapse.
When you click a plus or minus symbol or the Expand or Collapse button,this action
recalls the
index.php
script with new parameters for
expand
or
collapse
.You use
expanded
from page to page to track which threads should be expanded in any given view.
The
index.php
script begins by starting a session and adding the
expanded
variable
as a session variable if this has not already been done.After that,the script checks
whether it has been passed an
expand
or
collapse
parameter and modifies the
expanded
array accordingly. Look at the code for the
expand
parameter:
if(isset($_GET[‘expand’]))
{
if($_GET[‘expand’] == ‘all’)
expand_all($_SESSION[‘expanded’]);
else
$_SESSION[‘expanded’][$_GET[‘expand’]] = true;
}
If you click on the Expand button,the function
expand_all()
is called to add all the
threads that have replies into the
expanded
array.(We look at this in a moment.)
If you try to expand a particular thread,you will be passed a
postid
via
expand
.You
therefore add a new entry to the
expanded
array to reflect this.
The
expand_all()
function is shown in Listing 31.3.
Listing 31.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 = $conn->query($query);
$num = $result->num_rows;
for($i = 0; $i<$num; $i++)
{
$this_row = $result->fetch_row();
$expanded[$this_row[0]]=true;
}
}