Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

PHP by example, Part I

Effortless Webzine authoring and delivery


Listing 1. Getting user preferences from the database



user_funcs.php (get_user_data)

function get_user_data($user_id) {

        global $s_first_name, $s_last_name, $s_preferences, $s_color;
        
        $query = "SELECT * FROM user WHERE user_id='$user_id' AND active='Y'";
        $result = mysql_query($query) or die ("Query failed");
        
        if (mysql_num_rows($result) > 0) {
        
                session_register('s_first_name');
                session_register('s_last_name');
                session_register('s_preferences');
                session_register('s_color');
                
                $row = mysql_fetch_array($result);
                $s_first_name = $row['first_name'];
                $s_last_name = $row['last_name'];
                $length = strlen($row['preferences']);
                for ($i = 0; $i < $length; $i++) { $s_preferences[$i] = $row['preferences'][$i]; }
                $s_color = $row['color'];
        }
}

Back to the article.


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>';
 

Back to the article.


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>';
                }
        }
}

Back to the article.


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>

Back to the article.