Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > SERVER SIDE CODING > PHP TUTORIALS > PERSONALIZED WEB SITES IN A JIFFY


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

Personalized Web sites in a jiffy
By Steve Fox - 2004-02-02 Page:  1 2 3 4 5 6 7

Show 'em what you got

Many sites have some sort of a consistent style, which you can think of as a template. The template is often used to display headers, ads, and navigational menus that change as users visit different sections of the site. The easiest way to maintain this template is to create a function that outputs most of the HTML used to structure the site. You can pass parameters to this function to customize the site appearance. Listing 2, shown below, is the function used to generate the page header for my example site.

Listing 2. Defining the page header


site.php (site_header function)

function site_header($params) {

        global $s_first_name;
        
        print '<HTML><HEAD><TITLE>My Site Inc.';

        
        if ($params['title']) { print " - " . $params['title']; }
        
        print '</TITLE><LINK rel="stylesheet" href="example.css" type="text/css"></HEAD>
                <BODY topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" marginheight="0" marginwidth="0">

                <TABLE cellpadding="5" cellspacing="0" border="0" width="100%" bgcolor="'.color().'">

                <TR><TD class="page_title" width="50%">';
                        
        if (logged_in()) {
        
                print "Welcome back";
                if ($s_first_name != "") { print ", ".$s_first_name; }
        }
        else { print "Welcome to My Site Inc."; }
        

        print '</TD><TD class="page_title" width="50%" align="right">';

        if (logged_in()) {
        
                print '<a href="example.php" class="page_title">Home</a> | '
                        . '<a href="edit_user.php" class="page_title">Preferences</a> | '

                        . '<a href="example.php?action=logout" class="page_title">Logout</a>  ';
         
        }
        else { 
         
                 print '<a href="example.php" class="page_title">Home</a> | '

                         . '<a href="login.php" class="page_title">Login</a>';
        }
         
        print '</TD></TR></TABLE><BR>';
 

The $params array lets each script that calls this function pass in data to use in Web page customization. In this example, it is only used to add navigation text to the <TITLE> tag.

Next in this example is a check to see whether the visitor is logged in. If so, he or she is greeted with a warm "Welcome back" message that includes the visitor's first name, if it was provided in the user preferences. Visitors who are logged in are also given menu selections to change their site preferences or log out. If visitors are not logged in, they see a generic greeting and the menu contains only one option, which leads to the login page.

After the visitor has been properly greeted, it is time to show some news (which is, of course, why someone would visit our site). Listing 3 contains the function used to display news headlines from a single news source.

Listing 3. Displaying news items from a single news source


example.php (show_news function)

function show_news ($news_source) {

        $data_query = "SELECT headline, link FROM news WHERE news_source='$news_source' ORDER BY timestamp DESC LIMIT 5";
        $data_result = mysql_query($data_query) or die ("Could not get news data");
                        
        if (mysql_num_rows($data_result) > 0) {

                                
                while ($data_row = mysql_fetch_array($data_result)) {
                        
                        print '<LI><SMALL><A href="'.$data_row['link'].'" class="news">'.$data_row['headline']
                                . '</A></SMALL></LI>';
                }

        }
}

The show_news function is passed an integer that represents the source_id of the news source. Next, the headline text and URL link are fetched from the database for the five most recent news items. The item limit would make a good configurable parameter if you wanted to extend this example. If the news items are found, it simply prints out an HTML list item for each item as a link to the story.



View Personalized Web sites in a jiffy Discussion

Page:  1 2 3 4 5 6 7 Next Page: Tie it together

First published by IBM developerWorks


Copyright 2004-2024 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.