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

PHP and MySQL Web Development - P136 potx

5 238 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 94,38 KB

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

Nội dung

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']

Trang 1

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.

Trang 2

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);

Trang 3

$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 liststable, the number of subscribers from the sub_liststable, and the number of newsletters sent from the mailtable.

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)

Listing 28.10 Continued

Trang 4

$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

Trang 5

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;

}

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

TỪ KHÓA LIÊN QUAN