User Personalization with PHP: The Home Page / Page 2

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

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:

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:

startTag( $parser, $tagName, $attrs ) {
This function is responsible for determining which of the three document tags the parser has reached and then sets the appropriate boolean variable to true. The parser only deals with the link, title and description tags. The function takes three parameters, the tagname, parser and attributes. Using the tag name as a determining value, it tests the value contained within the tagname parameter to test which of the document tags have been reached:

function endTag( $parser, $tagName ) {
This function is much more interesting than the one before. It is responsible for matching the document tags to the variables defined in the code. Suppose we have an XML news document that has the following story:

The function will take the $tagname variable as the determining factor. It checks the value contained within the variable to test which of the three main tags have been reached by the parser and then set them to the appropriate text elements:

The code above does the job of checking if the $tagname variable contains a document tag called the 'TITLE', and if so, it adds the text that it extracted from the XML document to the tag. Notice that the title of the news story will be presented as a hyperlink. This is because in our scrolling news headlines section, we want the user to be able to click on a headline and be taken to the appropriate web page. The link to the news page is stored in the $GLOBALS['thelinktxt'] variable and the text of the title is stored in the $GLOBALS['thetitletxt'] variable, both of which are filled when the parser reaches a news story contained in an XML file.
So given our sample news story, our endtag function will look something like this:

The next part of the function checks to see if both the link and title tags have been reached by the parser before it is filled with equivalent document tags:

The txtTag function

This function is responsible for extracting the text parts of the various document tags. It takes two parameters the parser and the text. It extracts the text of a given document tag by checking if the declared tag name variable is set to true. If so, it simply adds the text of the extracted tag to the global title text variable:

The rest of the function does the same for the link and description tags.

parsefile() function

The final function is called parsefile() and does exactly what its name suggests. It takes a given file (an XML document, in this case) and processes it. Meaning that it uses the previous three functions to match and extract the various tags. The parser basically iterates through the XML document picking up the title, link and description of each news story as it goes and then finally displaying them. The function takes one parameter, which is the filename. Let's look at the code. First, we create an XML parser and set up the element handler. The XML parser functions are available in PHP:

Note that we feed the element handler the parser, start and end functions. Then we set the character handler. This function is used to process the text part of the various tags, so we feed it the txtTag function:

xml_set_character_data_handler( $xmlParser, "TxtTag" );

Finally we get to the main part of the entire function, which is to open and read the file contents:

Once we've opened the file successfully, we read its contents into the $data array:

while( $data = fread( $fp, 6000 ) ) {

...and then we parse the contents according to the way we defined our parser, i.e. links and titles in hyperlink form and then free the resources that we've been using:

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:

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.