1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P143 pot

5 70 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 167,35 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Some of them have plus symbols next to them.This means that these articles have been replied to.To see the replies to a particular article, you can click the plus symbol.The result of cl

Trang 1

What we see here are all the initiating articles None of these are replies; they are all the first article on a particular topic

You will see that we have a number of options.There is a menu bar that will let us add a new post and expand or collapse the view that we have of the articles

To understand what this means, look at the posts Some of them have plus symbols next to them.This means that these articles have been replied to.To see the replies to a particular article, you can click the plus symbol.The result of clicking one of these sym-bols is shown in Figure 29.5

Figure 29.5 The thread of discussion about persistence has been expanded.

As you can see, clicking the plus symbol has displayed the replies to that first article.The plus symbol has now turned to a minus symbol If we click this, all the articles in this thread will be collapsed, returning us to the initial view

You might also notice that one of the replies has a plus symbol next to it.This means that there are replies to this reply.This can continue to an arbitrary depth, and you can view each reply set by clicking on the appropriate plus symbol

The two menu bar options, Expand and Collapse, will expand all possible threads and collapse all possible threads, respectively.The result of clicking the Expand button is shown in Figure 29.6

Trang 2

Figure 29.6 All the threads have now been expanded.

If you look closely at Figures 29.5 and 29.6, you can see that we are passing some parameters back to index.phpin the command line In Figure 29.5, the URL looks as follows:

http://webserver/chapter29/index.php?expand=6#6 The script reads this as “Expand the item with postid 6”.The #is just an HTML anchor that will scroll the page down to the part that has just been expanded

In Figure 29.6, the URL reads http://webserver/chapter29/index.php?expand=all Clicking the Expand button has passed the parameterexpandwith the value all

Expanding and Collapsing

Let’s see how it’s done by looking at theindex.phpscript, shown in Listing 29.2

Listing 29.2 index.php—Script to Create the Article View on the Main Page of the

Application

<?php include ('include_fns.php');

session_start();

// check if we have created our session variable

Trang 3

$HTTP_SESSION_VARS['expanded'] = array();

}

// check if an expand button was pressed // expand might equal 'all' or a postid or not be set if(isset($HTTP_GET_VARS['expand']))

{ if($HTTP_GET_VARS['expand'] == 'all') expand_all($HTTP_SESSION_VARS['expanded']);

else

$HTTP_SESSION_VARS['expanded'][$HTTP_GET_VARS['expand']] = true;

}

// check if a collapse button was pressed // collapse might equal all or a postid or not be set if(isset($HTTP_GET_VARS['collapse']))

{ if($HTTP_GET_VARS['collapse']=='all')

$HTTP_SESSION_VARS['expanded'] = array();

else unset($HTTP_SESSION_VARS['expanded'][$HTTP_GET_VARS['collapse']]);

}

do_html_header('Discussion Posts');

display_index_toolbar();

// display the tree view of conversations display_tree($HTTP_SESSION_VARS['expanded']);

do_html_footer();

?>

This script uses three variables to do its job.They are

n The session variable expanded, which keeps track of which threads are expanded This can be maintained from view to view, so we can have multiple threads expanded.This variable is an associative array that contains the postidof articles which will have their replies displayed 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 we click a plus or minus symbol or the Expand or Collapse button, they will re-call the index.phpscript with new parameters for expandor collapse.We use Listing 29.2 Continued

Trang 4

expandedfrom page to page to track which threads should be expanded in any given view

The 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 expandor collapse parameter and modifies the expandedarray accordingly Look at the code for the expand parameter:

if(isset($HTTP_GET_VARS['expand'])) {

if($HTTP_GET_VARS['expand'] == 'all') expand_all($HTTP_SESSION_VARS['expanded']);

else

$HTTP_SESSION_VARS['expanded'][$HTTP_GET_VARS['expand']] = true;

}

If we have clicked on the Expand button, the function expand_all()is called to add all the threads that have replies into the expandedarray (We’ll look at this in a moment.)

If we are trying to expand a particular thread, we will have been passed a postidvia expand.We therefore add a new entry to the expandedarray 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 expandedarray.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

Trang 5

Collapsing the articles works in a similar but opposite way, as follows:

if(isset($HTTP_GET_VARS['collapse'])) {

if($HTTP_GET_VARS['collapse']=='all')

$HTTP_SESSION_VARS['expanded'] = array();

else unset($HTTP_SESSION_VARS['expanded'][$HTTP_GET_VARS['collapse']]);

} You can remove items from the expandedarray by unsetting them.We remove the thread that is to be collapsed, or unset the entire array if the entire page is to be col-lapsed

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($HTTP_SESSION_VARS['expanded']); which will actually generate the tree of displayed articles

Displaying the Articles

Let’s look at thedisplay_tree()function, shown in Listing 29.4

Listing 29.4 display_tree() Function from output_fns.php—Creates the Root Node of

the Tree Structure

function display_tree($expanded, $row = 0, $start = 0) {

// display the tree view of conversations

global $table_width;

echo "<table width=\"$table_width\">;

// see if we are displaying the whole list or a sublist if($start>0)

$sublist = true;

else

$sublist = false;

// construct tree structure to represent conversation summary

$tree = new treenode($start, '', '', '', 1, true, -1, $expanded, $sublist);

// tell tree to display itself

$tree->display($row, $sublist);

echo '</table>';

}

Ngày đăng: 07/07/2014, 03:20