13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

712 Chapter 30 Building a Mailing List Manager<br />

n<br />

n $list[n][2] <strong>and</strong> $list[n][3] are optional.You use them to convey that<br />

there is more information.They correspond to the more information text<br />

<strong>and</strong> the more information ID, respectively.We look at an example using<br />

these two parameters when we come to the View Mail action in the<br />

“Implementing Administrative Functions” section.<br />

The optional third, fourth, <strong>and</strong> fifth parameters to the function are used to pass in<br />

three actions that will be displayed on buttons corresponding to each item. In<br />

Figure 30.7, they are the three action buttons shown as Information, Show<br />

Archive, <strong>and</strong> Subscribe.<br />

You get these three buttons for the Show All Lists page by passing in the action names<br />

information, show-archive, <strong>and</strong> subscribe.When you use the display_button()<br />

function, these actions are turned into buttons with those words on them <strong>and</strong> the appropriate<br />

action assigned to them.<br />

Each of the Show actions calls the display_items() function in a different way, as<br />

you can see by looking back at their actions. In addition to having different titles <strong>and</strong><br />

action buttons, each of the three uses a different function to build the array of items to<br />

display. Show All Lists uses the function get_all_lists(), Show Other Lists uses<br />

the function get_unsubscribed_lists(), <strong>and</strong> Show My Lists uses the function<br />

get_subscribed_lists(). All these functions work in a similar fashion <strong>and</strong> are all<br />

from the mlm_fns.php function library.<br />

Let’s look at get_unsubscribed_lists() because that’s the example we’ve followed<br />

so far.The code for the get_unsubscribed_lists() function is shown in Listing 30.8.<br />

Listing 30.8 get_unsubscribed_lists()Function from mlm_fns.php—This<br />

Function Builds an Array of Mailing Lists That a User Is Not Subscribed To<br />

function get_unsubscribed_lists($email) {<br />

$list = array();<br />

$query = "select lists.listid, listname, email from lists<br />

left join sub_lists on lists.listid = sub_lists.listid<br />

<strong>and</strong> email='".$email."' where email is NULL<br />

order by listname";<br />

if($conn=db_connect()) {<br />

$result = $conn->query($query);<br />

if(!$result) {<br />

echo 'Unable to get list from database.';<br />

return false;<br />

}<br />

$num = $result->num_rows;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!