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.

Implementing Bookmark Storage <strong>and</strong> Retrieval<br />

599<br />

Looking back at add_bm.php, you can see that the last thing it does is call<br />

get_user_urls() <strong>and</strong> display_user_urls(), the same as member.php. We look at<br />

these functions next.<br />

Displaying Bookmarks<br />

The member.php script <strong>and</strong> add_bm() function use the functions get_user_urls() <strong>and</strong><br />

display_user_urls().These functions get a user’s bookmarks from the database <strong>and</strong><br />

display them, respectively.The get_user_urls() function is in the url_fns.php library,<br />

<strong>and</strong> the display_user_urls() function is in the output_fns.php library.<br />

The get_user_urls() function is shown in Listing 27.23.<br />

Listing 27.23 get_user_urls()Function from url_fns.php—This Function<br />

Retrieves a User’s Bookmarks from the Database<br />

function get_user_urls($username) {<br />

//extract from the database all the URLs this user has stored<br />

$conn = db_connect();<br />

$result = $conn->query("select bm_URL<br />

from bookmark<br />

where username = '".$username."'");<br />

if (!$result) {<br />

return false;<br />

}<br />

//create an array of the URLs<br />

$url_array = array();<br />

for ($count = 1; $row = $result->fetch_row(); ++$count) {<br />

$url_array[$count] = $row[0];<br />

}<br />

return $url_array;<br />

}<br />

Let’s briefly step through the get_user_urls() function. It takes a username as a parameter<br />

<strong>and</strong> retrieves the bookmarks for that user from the database. It returns an array of<br />

these URLs or false if the bookmarks could not be retrieved.<br />

The array from get_user_urls() can be passed to display_user_urls().This is<br />

again a simple HTML output function to print the user’s URLs in a nice table format,<br />

so we didn’t include it here. Refer to Figure 27.6 to see what the output looks like.The<br />

function actually puts the URLs into a form. Next to each URL is a check box that<br />

enables the user to mark bookmarks for deletion.We look at this capability next.

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

Saved successfully!

Ooh no, something went wrong!