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

PHP and MySQL Web Development - P108 pps

5 83 0

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

THÔNG TIN TÀI LIỆU

507 Implementing Recommendations where b1.username='$valid_user' and b1.username != b2.username and b1.bm_URL = b2.bm_URL This query uses aliases to join the database table bookmark to itself—a strange but some- times useful concept. Imagine that there are actually two bookmark tables, one called b1 and one called b2. In b1,we look at the current user and his bookmarks. In the other table, we look at the bookmarks of all the other users.We are looking for other users (b2.username) who have an URL the same as the current user (b1.bm_URL = b2.bm_URL) and are not the current user (b1.username != b2.username). This query will give us a list of like-minded people to our current user.Armed with this list, we can search for their other bookmarks with the following query: select bm_URL from bookmark where username in $sim_users and bm_URL not in $user_urls group by bm_URL having count(bm_URL)>$popularity The variable $sim_users contains the list of like-minded users.The $user_urls variable contains the list of the current user’s bookmarks—if b1 already has a bookmark, there’s no point in recommending it to him. Finally, we add some filtering with the $popular- ity variable—we don’t want to recommend any URLs that are too personal, so we only suggest URLs that a certain number of other users in the list of like-minded users have bookmarked. If we were anticipating a lot of users using our system, we could adjust $popularity upwards to only suggest URLs have been bookmarked by a large number of users. URLs bookmarked by many people might be higher quality and certainly have more general appeal than an average Web page. The full script for making recommendations is shown in Listing 24.26 and 24.27.The main script for making recommendations is called recommend.php (see Listing 24.26). It calls the recommender function recommend_urls() from url_fns.php (see Listing 24.27) . Listing 24.26 recommend.php—This Script Suggests Some Bookmarks That a User Might Like <?php require_once('bookmark_fns.php'); session_start(); do_html_header('Recommending URLs'); check_valid_user(); $urls = recommend_urls($HTTP_SESSION_VARS['valid_user']); display_recommended_urls($urls); 30 525x ch24 1/24/03 3:36 PM Page 507 508 Chapter 24 Building User Authentication and Personalization display_user_menu(); do_html_footer(); ?> Listing 24.27 recommend_urls() Function from url_fns.php—This Script Works Out the Actual Recommendations function recommend_urls($valid_user, $popularity = 1) { // We will provide semi intelligent recomendations to people // If they have an URL in common with other users, they may like // other URLs that these people like if (!($conn = db_connect())) return false; // find other matching users // with an url the same as you if (!($result = mysql_query(" select distinct(b2.username) from bookmark b1, bookmark b2 where b1.username='$valid_user' and b1.username != b2.username and b1.bm_URL = b2.bm_URL "))) return false; if (mysql_num_rows($result)==0) return false; // create set of users with urls in common // for use in IN clause $row = mysql_fetch_object($result); $sim_users = "('".($row->username)."'"; while ($row = mysql_fetch_object($result)) { $sim_users .= ", '".($row->username)."'"; } $sim_users .= ')'; // create list of user urls // to avoid replicating ones we already know about if (!($result = mysql_query(" select bm_URL from bookmark Listing 24.26 Continued 30 525x ch24 1/24/03 3:36 PM Page 508 509 Implementing Recommendations where username='$valid_user'"))) return false; // create set of user urls for use in IN clause $row = mysql_fetch_object($result); $user_urls = "('".($row->bm_URL)."'"; while ($row = mysql_fetch_object($result)) { $user_urls .= ", '".($row->bm_URL)."'"; } $user_urls .= ')'; // as a simple way of excluding people's private pages, and // increasing the chance of recommending appealing URLs, we // specify a minimum popularity level // if $popularity = 1, then more than one person must have // an URL before we will recomend it // find out max number of possible URLs if (!($result = mysql_query(" select bm_URL from bookmark where username in $sim_users and bm_URL not in $user_urls group by bm_URL having count(bm_URL)>$popularity "))) return false; if (!($num_urls=mysql_num_rows($result))) return false; $urls = array(); // build an array of the relevant urls for ($count=0; $row = mysql_fetch_object($result); $count++) { $urls[$count] = $row->bm_URL; } return $urls; } Some sample output from recommend.php is shown in Figure 24.11. 30 525x ch24 1/24/03 3:36 PM Page 509 510 Chapter 24 Building User Authentication and Personalization Figure 24.11 The script has recommended that this user might like pets.com.Two other users in the database who both like slashdot.org have this bookmarked. Wrapping Up and Possible Extensions That’s the basic functionality of the PHPBookmark application.There are many possible extensions.You might consider adding n A grouping of bookmarks by topic n An “Add this to my bookmarks” link for recommendations n Recommendations based on the most popular URLs in the database, or on a par- ticular topic n An administrative interface to set up and administer users and topics n Ways to make recommended bookmarks more intelligent or faster n Additional error checking of user input Experiment! It’s the best way to learn. Next In the next project we’ll build a shopping cart that will enable users to browse our site, adding purchases as they go, before finally checking out and making an electronic pay- ment. 30 525x ch24 1/24/03 3:36 PM Page 510 25 Building a Shopping Cart In this chapter, you will learn how to build a basic shopping cart.We will add this on top of the Book-O-Rama database that we implemented in Part II, “Using MySQL.”We will also explore another option—setting up and using an existing Open Source PHP shopping cart. In case you have not heard it before, the term shopping cart (sometimes also called a shopping basket) is used to describe a specific online shopping mechanism. As you browse an online catalog, you can add items to your shopping cart.When you’ve finished brows- ing, you check out of the online store—that is, purchase the items in your cart. In order to implement the shopping cart, we will implement the following function- ality: n A database of the products we want to sell online n An online catalog of products, listed by category n A shopping cart to track the items a user wants to buy n A checkout script that processes payment and shipping details n An administration interface The Problem You will probably remember the Book-O-Rama database we developed in Part II. In this project, we will get Book-O-Rama’s online store up and going.The following are requirements for this system: n We will need to find a way of connecting the database to a user’s browser. Users should be able to browse items by category. n Users should also be able to select items from the catalog for later purchase.We will need to be able to track which items they have selected. 31 525x ch25 1/24/03 3:39 PM Page 511 . this on top of the Book-O-Rama database that we implemented in Part II, “Using MySQL. ”We will also explore another option—setting up and using an existing Open Source PHP shopping cart. In case. payment and shipping details n An administration interface The Problem You will probably remember the Book-O-Rama database we developed in Part II. In this project, we will get Book-O-Rama’s. = mysql_ fetch_object($result); $user_urls = "('".($row->bm_URL)."'"; while ($row = mysql_ fetch_object($result)) { $user_urls .= ", '".($row->bm_URL)."'"; } $user_urls

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

Xem thêm: PHP and MySQL Web Development - P108 pps

Mục lục

    PHP and MySQL Web Development

    Part I: Using PHP

    Chapter 1: PHP Crash Course

    Chapter 2: Storing and Retrieving Data

    Chapter 4: String Manipulation and Regular Expressions

    Chapter 5: Reusing Code and Writing Functions

    Part II: Using MySQL

    Chapter 7: Designing Your Web Database

    Chapter 8: Creating Your Web Database

    Chapter 9: Working with Your MySQL Database

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN