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

Tie it together

The preceding functions provide a nice facility to design any site layout. This example creates a rather simple layout where the news links are shown in a left column and the main site content takes up the rest of the space, as shown in Figure 2.

Figure 2. Site layout (click to enlarge)
Screen capture shows news in small left column and site content in wide right column

Take a look at the code that generates this layout. I think you'll be surprised at how little code is needed, once most of the features are available as functions. Listing 4 contains the details.

Listing 4. Defining the main site


example.php

<?php

if ($action == 'logout') { logout(); }

site_header(array('title'=>'Home'));

?>

<TABLE cellpadding="6" cellspacing="0" border="0" width="100%">

<TR valign="top">
<TD width="180">
                        
<?php

if (logged_in()) {

        foreach ($s_preferences as $index => $pref) {
        
                if ($pref == "1") {
        
                        $query = "SELECT * FROM news_sources WHERE source_id='".($index+1)."'";
                        $result = mysql_query($query) or die ("Could not select news sources");

                        
                        if (mysql_num_rows($result) > 0) {
                        
                                $row = mysql_fetch_array($result);

                                box_top($row['source_name'], $row['source_home']);
                                show_news($index+1);
                                box_bottom();
                        }
                }

        }
}
else {

        $query = "SELECT * FROM news_sources";
        $result = mysql_query($query) or die ("Could not select news sources");

        $i = 1;
        
        while ($row = mysql_fetch_array($result)) {

                box_top($row['source_name'], $row['source_home']);
                show_news($i);
                box_bottom();

                ++$i;
        }
}

?>

</TD>
<TD width="100%">
        <TABLE cellpadding="0" cellspacing="0" border="0" width="100%">
        <TR>
        <TD>

        <DL>
        <DT><SPAN class="heading">Latest News</SPAN></DT>
        <DD><BR>You can now register for an account on our site! With an account you will be able to select
                which news items you would like to see and hide those you don't. You also get the nifty feature of being
                personally greated every time you visit our site. How much would you expect to pay for this? Nothing!

                <a href="http://www.php.net">PHP</a> and <a href="http://www.mysql.net">MySQL</a>
                make this so easy anyone can do it.</DD>
        </DL>

        </TD>
        </TR>
        </TABLE>
</TD>
</TR>
</TABLE>

</BODY>

</HTML>

The code in Listing 4 checks to see whether the $action variable is set to 'logout'. If so, the logout function is called, which unregisters all session variables so that the site will consider this user logged out. Next, the code calls the site_header function to print out the site template and pass it the string 'Home' to be displayed in the title.

Now it's time to select the news items that the visitor will see. The code calls the logged_in function to parse the visitor's preferences string. For each character in the string set to 1, news from one source is displayed. First, the code calls the box_top function (defined in site.php) to print out the necessary table elements to display a box around the source, with the news source's name and home page URL already passed into the function to be shown in the box header. Next, the code calls the show_news function to display the news items for that news source. Last, it calls box_bottom to close off the box boundary. If a site visitor is not logged in, the code will simply query the database for all news sources and display boxes for all of them.

After this point, the PHP script is basically done, and you can add the actual page content. More than likely, a site would have a separate database for its own content and you could write more PHP code to grab the content from that database.

Breaking code down into functions lets you create structures that are reusable anywhere on a Web site. You can then create any page layout easily. It should be very simple to add a right menu containing more news links. Alternatively, the entire page could be a large table of news boxes and with added code so that each visitor could select the position of each box within a row or column. You could add other boxes to show information such as the weather forecast, horoscopes, television schedules, or event reminders. Any of these would simply be an addition to the preferences page. Just remember to encapsulate each new feature so it can be used elsewhere. Most of all, be creative and have fun doing it.



View Personalized Web sites in a jiffy Discussion

Page:  1 2 3 4 5 6 7 Next Page: Resources

First published by IBM developerWorks


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