PHP and MySQL Web Development - P143 pot

5 68 0
PHP and MySQL Web Development - P143 pot

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

Thông tin tài liệu

682 Chapter 29 Building Web Forums 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. 35 525x ch29 1/24/03 3:36 PM Page 682 683 Viewing the Tree of Articles 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.php in 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 parameter expand with the value all. Expanding and Collapsing Let’s see how it’s done by looking at the index.php script, 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 if(!isset($HTTP_SESSION_VARS['expanded'])) 35 525x ch29 1/24/03 3:36 PM Page 683 684 Chapter 29 Building Web Forums { $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 postid of 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.php script with new parameters for expand or collapse.We use Listing 29.2 Continued 35 525x ch29 1/24/03 3:36 PM Page 684 685 Viewing the Tree of Articles expanded from 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 expand or collapse parameter and modifies the expanded array 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 expanded array. (We’ll look at this in a moment.) 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. 35 525x ch29 1/24/03 3:36 PM Page 685 686 Chapter 29 Building Web Forums 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 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 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 the display_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>'; } 35 525x ch29 1/24/03 3:36 PM Page 686 . reads http://webserver/chapter29/index .php? expand=all Clicking the Expand button has passed the parameter expand with the value all. Expanding and Collapsing Let’s see how it’s done by looking at the index .php script,. Figures 29.5 and 29.6, you can see that we are passing some parameters back to index .php in the command line. In Figure 29.5, the URL looks as follows: http://webserver/chapter29/index .php? expand=6#6 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) { //

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

Từ khóa liên quan

Mục lục

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

    • Part II: Using MySQL

      • Chapter 7: Designing Your Web Database

      • Chapter 8: Creating Your Web Database

      • Chapter 9: Working with Your MySQL Database

      • Chapter 10: Accessing Your MySQL Database from the Web with PHP

      • Chapter 11: Advanced MySQL

      • Part III: E-commerce and Security

        • Chapter 12: Running an E-commerce Site

        • Chapter 13: E-commerce Security Issues

        • Chapter 14: Implementing Authentication with PHP and MySQL

        • Chapter 15: Implementing Secure Transactions with PHP and MySQL

        • Part IV: Advanced PHP Techniques

          • Chapter 16: Interacting with the File System and the Server

Tài liệu cùng người dùng

Tài liệu liên quan