PHP and MySQL Web Development - P135 potx

5 222 0
PHP and MySQL Web Development - P135 potx

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

Thông tin tài liệu

642 Chapter 28 Building a Mailing List Manager Now that we have logged in a user, we can proceed to the user functions. Implementing User Functions There are five things we want our users to be able to do after they have logged in: n Look at the lists available for subscription n Subscribe and unsubscribe from lists n Change the way their accounts are set up n Change their passwords n Log out You can see most of these options in Figure 28.6.We will now look at the implementa- tion of each of these options. Viewing Lists We will implement a number of options for viewing available lists and list details. In Figure 28.6, you can see two of these options: Show My Lists, which will retrieve the lists this user is subscribed to, and Show Other Lists, which will retrieve the lists the user is not subscribed to. If you look back at Figure 28.4, you will see that we have another option, Show All Lists, which will retrieve all the available mailing lists on the system. For the system to be truly scalable, we should add paging functionality (to display, say, 10 results per page).We have not done this here for brevity. These three menu options activate the show-my-lists, show-other-lists, and show-all-lists actions, respectively. As you have probably realized, all these actions work quite similarly.The code for these three actions is as follows: case 'show-all-lists' : { display_items('All Lists', get_all_lists(), 'information', 'show-archive',''); break; } case 'show-other-lists' : { display_items('Unsubscribed Lists', get_unsubscribed_lists(get_email()), 'information', 'show-archive', 'subscribe'); break; } 34 525x ch28 1/24/03 2:55 PM Page 642 643 Implementing User Functions case '': case 'show-my-lists' : { display_items('Subscribed Lists', get_subscribed_lists(get_email()), 'information', 'show-archive', 'unsubscribe'); break; } As you can see, all these actions call the display_items() function from the output_fns.php library, but they each call it with different parameters.They all also use the get_email() function we mentioned earlier to get the appropriate email address for this user. To see what this function does, look at Figure 28.7. Figure 28.7 The display_items() function has been used to lay out a list of the lists that the user is not subscribed to. This is the Show Other Lists page. Let’s look at the code for the display_items() function. It is shown in Listing 28.7. Listing 28.7 display_items() Function from output_fns.php—This Function Is Used to Display a List of Items with Associated Actions function display_items($title, $list, $action1='', $action2='', $action3='') { global $table_width; 34 525x ch28 1/24/03 2:55 PM Page 643 644 Chapter 28 Building a Mailing List Manager echo "<table width=\"$table_width\" cellspacing=\"0\" cellpadding=\"0\" border=\"0\">"; // count number of actions $actions = (($action1!='') + ($action2!='') + ($action3!='')); echo '<tr> <th colspan="'. (1+$actions) .'" bgcolor="#5B69A6">'. $title .'</th> </tr>'; // count number of items $items = sizeof($list); if($items == 0) echo '<tr> <td colspan="'.(1+$actions).'" align="center">No Items to Display</td> </tr>'; else { // print each row for($i = 0; $items; $i++) { if($i%2) // background colors alternate $bgcolor = "'#ffffff'"; else $bgcolor = "'#ccccff'"; echo "<tr> <td bgcolor=$bgcolor width=\"".($table_width - ($actions*149)).'">'; echo $list[$i][1]; if($list[$i][2]) echo ' - '.$list[$i][2]; echo '</td>'; // create buttons for up to three actions per line for($j = 1; $j<=3; $j++) { $var = 'action'.$j; if($$var) { echo "<td bgcolor=\"$bgcolor\" width=\"149\">"; // view/preview buttons are a special case as they link to a file if($$var == 'preview-html'||$$var == 'view-html'|| $$var == 'preview-text'||$$var == 'view-text') display_preview_button($list[$i][3], $list[$i][0], $$var); Listing 28.7 Continued 34 525x ch28 1/24/03 2:55 PM Page 644 645 Implementing User Functions else display_button( $$var, '&id=' . $list[$i][0] ); echo '</td>'; } } echo "</tr>\n"; } echo '</table>'; } } This function will output a table of items, with each item having up to three associated action buttons.The function expects five parameters, which are, in order: n $title is the title that appears at the top of the table—in the case shown in Figure 28.7, we are passing in the title Unsubscribed Lists, as shown in the previ- ously discussed code snippet for the action “Show Other Lists”. n $list is an array of items to display in each row of the table. In this case, it is an array of the lists the user is not currently subscribed to.We are building this array (in this case) in the function get_unsubscribed_lists(), which we’ll discuss in a minute.This is a multidimensional array, with each row in the array containing up to four pieces of data about each row. In order, again: n $list[n][0] should contain the item id, which will usually be a row num- ber.This gives the action buttons the id of the row they are to operate upon. In our case, we will use ids from the database—more on this in a minute. n $list[n][1] should contain the item name.This will be the text displayed for a particular item. For example, in the case shown in Figure 28.7, the item name in the first row of the table is PHP Tipsheet. n $list[n][2] and $list[n][3] are optional.We use these to convey that there is more information.They correspond to the more information text and the more information id, respectively.We will look at an example using these two parameters when we come to the View Mail action in the “Implementing Administrative Functions” section. We could, as an alternative, have used keywords as indices to the array. The third, fourth, and fifth parameters to the function are used to pass in three actions that will be displayed on buttons corresponding to each item. In Figure 28.7, these are the three action buttons shown as Information, Show Archive, and Subscribe. We got these three buttons for the Show All Lists page by passing in the action names, information, show-archive,and subscribe. By using the display_ button() function, these actions have been turned into buttons with those words on them, and the appropriate action assigned to them. Listing 28.7 Continued 34 525x ch28 1/24/03 2:55 PM Page 645 646 Chapter 28 Building a Mailing List Manager Each of the Show actions calls the display_items() function in a different way, as you can see by looking back at their actions.As well as having different titles and action buttons, each of the three uses a different function to build the array of items to display. Show All Lists uses the function get_all_lists(), Show Other Lists uses the function get_unsubscribed_lists(), and Show My Lists uses the function get_subscribed_ lists(). All these functions work in a similar fashion.They are all from the mlm_fns.php function library. We’ll look at get_unsubscribed_lists() because that’s the example we’ve followed so far.The code for the get_unsubscribed_lists() function is shown in Listing 28.8. Listing 28.8 get_unsubscribed_lists() Function from mlm_fns.php—Building an Array of Mailing Lists That a User Is Not Subscribed To function get_unsubscribed_lists($email) { $list = array(); $query = "select lists.listid, listname, email from lists left join sub_lists on lists.listid = sub_lists.listid and email='$email' where email is NULL order by listname"; if(db_connect()) { $result = mysql_query($query); if(!$result) echo '<p>Unable to get list from database.</p>'; $num = mysql_numrows($result); for($i = 0; $i<$num; $i++) { array_push($list, array(mysql_result($result, $i, 0), mysql_result($result, $i, 1))); } } return $list; } As you can see, this function requires an email address passed into it.This should be the email address of the subscriber that we are working with.The get_subscribed_lists() function also requires an email address as a parameter, but the get_all_lists() func- tion does not for obvious reasons. Given a subscriber’s email address, we connect to the database and fetch all the lists the subscriber is not subscribed to.As usual for this kind of query in MySQL, we use a LEFT JOIN to find unmatched items. We loop through the result and build the array row by row using the array_push() built-in function. 34 525x ch28 1/24/03 2:55 PM Page 646 . not done this here for brevity. These three menu options activate the show-my-lists, show-other-lists, and show-all-lists actions, respectively. As you have probably realized, all these actions work. 'show-all-lists' : { display_items('All Lists', get_all_lists(), 'information', 'show-archive',''); break; } case 'show-other-lists'. they link to a file if($$var == 'preview-html'||$$var == 'view-html'|| $$var == 'preview-text'||$$var == 'view-text') display_preview_button($list[$i][3],

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

  • Đang cập nhật ...

Tài liệu liên quan