647 Implementing User Functions Now that we know how this list is produced, let’s look at the action buttons associat- ed with these displays. Viewing List Information The Information button shown in Figure 28.7 triggers the 'information' action, which is as follows: case 'information' : { display_information($HTTP_GET_VARS['id']); break; } To see what the display_information() function does, look at Figure 28.8. Figure 28.8 The display_information() function shows a blurb about a mailing list. The function displays some general information about a particular mailing list, as well as listing the number of subscribers and the number of newsletters that have been sent out to that list and are available in the archive (more on that in a minute). The code for this function is shown in Listing 28.9. 34 525x ch28 1/24/03 2:55 PM Page 647 648 Chapter 28 Building a Mailing List Manager Listing 28.9 display_information() Function from output_fns.php—Displaying List Information function display_information($listid) { if(!$listid) return false; $info = load_list_info($listid); if($info) { echo '<h2>'.pretty($info[listname]).'</h2>'; echo '<p>'.pretty($info[blurb])'</p>; echo '<p>Number of subscribers:' . $info[subscribers]'</p>; echo '<p>Number of messages in archive:' . $info[archive]'</p>; } } The display_information() function uses two other functions to help it achieve its Web task: the load_list_info() function and the pretty() function.The load_list_info() function actually retrieves the data from the database.The pretty() function simply formats the data from the database by stripping out slashes, turning newlines into HTML line breaks, and so on. Let’s look briefly at the load_list_info() function.This function is in the mlm_fns.php function library.The code for it is shown in Listing 28.10. Listing 28.10 load_list_info() Function from mlm_fns.php—Building an Array of List Information function load_list_info($listid) { if(!$listid) return false; if(!db_connect()) return false; $query = "select listname, blurb from lists where listid = $listid"; $result = mysql_query($query); if(!$result) { echo 'Cannot retrieve this list'; return false; } $info = mysql_fetch_array($result); 34 525x ch28 1/24/03 2:55 PM Page 648 649 Implementing User Functions $query = "select count(*) from sub_lists where listid = $listid"; $result = mysql_query($query); if($result) { $info['subscribers'] = mysql_result($result, 0, 0); } $query = "select count(*) from mail where listid = $listid and status = 'SENT'"; $result = mysql_query($query); if($result) { $info['archive'] = mysql_result($result, 0, 0); } return $info; } This function runs three database queries to collect the name and blurb for a list from the lists table, the number of subscribers from the sub_lists table, and the number of newsletters sent from the mail table. Viewing List Archives In addition to viewing the list blurb, users can look at all the mail that has been sent to a mailing list by clicking on the Show Archive button.This activates the show-archive action, which triggers the following code: case 'show-archive' : { display_items('Archive For '.get_list_name($HTTP_GET_VARS['id']), get_archive($HTTP_GET_VARS['id']), 'view-html', 'view-text', ''); break; } Again, this function uses the display_items() function to list out the various items of mail that have been sent to the list.These items are retrieved using the get_archive() function from mlm_fns.php.This function is shown in Listing 28.11. Listing 28.11 get_archive() Function from mlm_fns.php—Building an Array of Archived Newsletters for a Given List function get_archive($listid) { //returns an array of the archived mail for this list //array has rows like (mailid, subject) $list = array(); Listing 28.10 Continued 34 525x ch28 1/24/03 2:55 PM Page 649 650 Chapter 28 Building a Mailing List Manager $listname = get_list_name($listid); $query = "select mailid, subject, listid from mail where listid = $listid and status = 'SENT' order by sent"; if(db_connect()) { $result = mysql_query($query); if(!$result) { echo "<p>Unable to get list from database - $query.</p>"; return false; } $num = mysql_numrows($result); for($i = 0; $i<$num; $i++) { $row = array(mysql_result($result, $i, 0), mysql_result($result, $i, 1), $listname, $listid); array_push($list, $row); } } return $list; } Again, this function gets the required information—in this case, the details of mail that has been sent—from the database and builds an array suitable for passing to the display_items() function. Subscribing and Unsubscribing On the list of mailing lists shown in Figure 28.7, each list has a button that enables users to subscribe to it. Similarly, if users use the Show My Lists option to see the lists to which they are already subscribed, they will see an Unsubscribe button next to each list. These buttons activate the subscribe and unsubscribe actions, which trigger the fol- lowing two pieces of code, respectively: case 'subscribe' : { subscribe(get_email(), $HTTP_GET_VARS['id']); display_items('Subscribed Lists', get_subscribed_lists(get_email()), 'information', 'show-archive', 'unsubscribe'); break; } case 'unsubscribe' : { Listing 28.11 Continued 34 525x ch28 1/24/03 2:55 PM Page 650 651 Implementing User Functions unsubscribe(get_email(), $HTTP_GET_VARS['id']); display_items('Subscribed Lists', get_subscribed_lists(get_email()), 'information', 'show-archive', 'unsubscribe'); break; } In each case, we call a function (subscribe() or unsubscribe()) and then redisplay a list of mailing lists the user is now subscribed to using the display_items() function again. The subscribe() and unsubscribe() functions are shown in Listing 28.12. Listing 28.12 subscribe() and unsubscribe() Functions from mlm_fns.php—These Functions Add and Remove Subscriptions for a User function subscribe($email, $listid) { if(!$email||!$listid||!list_exists($listid)||!subscriber_exists($email)) return false; //if already subscribed exit if(subscribed($email, $listid)) return false; if(!db_connect()) return false; $query = "insert into sub_lists values ('$email', $listid)"; $result = mysql_query($query); return $result; } function unsubscribe($email, $listid) { if(!$email||!$listid) return false; if(!db_connect()) return false; $query = "delete from sub_lists where email = '$email' and listid = $listid"; $result = mysql_query($query); return $result; } 34 525x ch28 1/24/03 2:55 PM Page 651 . function again. The subscribe() and unsubscribe() functions are shown in Listing 28.12. Listing 28.12 subscribe() and unsubscribe() Functions from mlm_fns .php These Functions Add and Remove Subscriptions. 'SENT'"; $result = mysql_ query($query); if($result) { $info['archive'] = mysql_ result($result, 0, 0); } return $info; } This function runs three database queries to collect the name and blurb. = $listid and status = 'SENT' order by sent"; if(db_connect()) { $result = mysql_ query($query); if(!$result) { echo "<p>Unable to get list from database - $query.</p>"; return