WebReference.com logo
tip archive  •   about  •   sitemap  •   contact  •   jobs  •   write for us  •   subscribe


[previous]

User Personalization with PHP:
The Home Page [con't]

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

The News Headlines Section

This section is responsible for displaying the latest news headlines. The idea here is that a user can feed any XML document to our custom PHP based XML parser. This parser will then process and together with some HTML, scroll the headlines in this corner of the main page. The best thing about this parser is that it can take RSS files and that it is entirely modifiable. In other words, you can make it display the headlines in any fashion that you like. Take a look at the code for the parser:

Let's take a closer look at the code. The first part declares some global variables that we will need in the subsequent code lines; first of which is the boolean variables:

  $GLOBALS['titletag'] = false;
  $GLOBALS['linktag']  = false;
  $GLOBALS['descriptiontag'] = false;

Then we define sub-variables for that will cater for each major tag of a given XML document:

  $GLOBALS['thetitletxt'] = null;
  $GLOBALS['thelinktxt'] = null;
  $GLOBALS['thedesctxt'] = null;

The above variables will later be filled with data that will be extracted from a given XML document. The functions, which are yet to be discussed, will extract the title, link and description of a news story. The functions will then fill the global variables with the appropriate names i.e $GLOBALS['thetitletxt'] will be filled with the title of the story etc. The parser has four functions:

To use the parser, we simply call the parsefile() function like so:

parserfile(yourXMLfilename);

Implementing the Recommendations

The recommendations section of the main page is responsible for making suggestions of bookmarks that may be of interest to the current user. Now, this can be implemented in various ways, but we'll do it the easy way. We will simply retrieve all the bookmarks stored in the database and remove all bookmarks that are used by the current user. The remaining bookmarks will then be passed off as recommendations. At the very beginning of the article we ran a couple of queries and stored the results in two arrays called $_SESSION['curruserbm'] and $_SESSION['otheruserbm']. To get the bookmark information for both the current and other users we run the following SQL queries. First, we run a query to retrieve the current user's bookmarks from the database:

We then create a new array:

$s[]="";

Finally, we iterate through the array and add the contents to the new array using the array_push() function:

Then to make this array available to all the script in the application we store it in a session variable. This way we dont have to run the same queries again, we simply use the stored session variables which will be available to the entire application:

$_SESSION['curruserbm'] = $s;

The same process is executed to obtain the bookmarks used by other users, resulting in a session variable called $_SESSION['otheruserbm']. To differentiate between bookmarks of the current user and those of the other users we store the bookmarks in the variables as described above. Now, it's just a simple matter of comparing the contents of the two arrays and removing any duplicates. This process is made extremely painless by PHP's array functions. First, we built an HTML table in which to display the information:


Then we run a foreach loop to retrieve and compare the bookmarks stored in the currentuser array with that of the bookmarks stored in the otheruser array. The reason for this is so that we obtain the index key of bookmarks that is in the otheruser array, which is the same as the bookmarks in the curruser array. To get the key, we use the array_search() function. The result of the function is then stored in a variable called $key. Then we use the unset() function to remove that bookmark from the $_SESSION]'otheruserbm']array:


We use another array function called array_unique() to remove duplicates from the $_SESSION]'otheruserbm'] array and store the new values in an array called $res:

$res= array_unique($_SESSION['otheruserbm']);

We run a second foreach() construct, and repeat the above process:


You may also enjoy:

Finally, we run the third and last foreach construct to display the remaining bookmarks as recommendations:


In the next article, we'll look at the last remaining scripts in the series.

Original: April 24, 2009


[prev]

Recent Articles

WebReference.com site name
Rolling Out Your Own HTML Application Version Control
HTML 5: Client-side Storage
Working with Ajax Server Extensions
internet.com site name
Wi-Fi Product Watch, November 2009
Chip Market Recovering From '08 Collapse
Low-Cost Tools to Kickstart Your New Business


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs