<?php
## portal.php - sample page generation script ## for use with TechRepublic article "Dynamic Web with PHP" ## uses mySQL database ## this script pretends that a user named 'Eddie' has logged in, ## and that he has entered his user and config info
$portal_page = new Portal;
class Portal {
function portal() { global $configs, $userinfo; $uname = "eddie"; $dbid = $this->dbConnect(); $configs = $this->runQuery($uname, "config"); $userinfo = $this->runQuery($uname, "info"); $this->showPage(); mysql_close($dbid); }
## connect to db - this includes a check at the end to make sure the db tablespace we wanted is available ## '$db' is our database link identifier function dbConnect() { $dbhost = "localhost"; $dbuname = "root"; $dbpass = ""; $dbname = "samples"; $db = mysql_connect($dbhost, $dbuname, $dbpass); @mysql_select_db("$dbname") or die ("Unable to select database"); return($db); }
## query the database for either user configuration information, or for stored user data function runQuery($user, $query_type) { switch($query_type) { case "config" : $table = "configs"; break; case "info" : $table = "userdata"; break; } $prefs = mysql_query("SELECT * FROM $table where uname = '$user'"); while ($row = mysql_fetch_array($prefs, MYSQL_ASSOC)) { $data = $row; } return($data); }
## draw the Web page, using data retrieved from the database function showPage() { global $configs, $userinfo; ?> <html> <head> <title>portal.php - sample script</title> </head> <body marginwidth=0 marginheight=0 leftmargin=0 rightmargin=0 topmargin=0 bgcolor=white> <table border=0 cellpadding=0 cellspacing=0 width="100%"> <tr><td colspan=3> <? ## show the header in the user's style $header = $configs[style]."_header.php"; require $header; ?> </td></tr> <tr valign=top><td width="20%"><table bgcolor="#000000" border=0 cellpadding=5 cellspacing=0 valign=top> <? ## show links the user configured to see if ($configs[favlinks] == '1') { require "favlinks.php"; } if ($configs[phplinks] == '1') { require "phplinks.php"; } ?> </table></td> <td width="60%"> <table border=0 cellpadding=5 cellspacing=0 valign=top><tr><td valign=top bgcolor="<? ## use the user's favorite color for the background echo $configs[favcolor]; ?>"> <? ## show the user's picture if ($configs[picture]) { echo "<img src="./".$configs[picture]."" width="200">"; } ?> </td><td> <b><font size="+1">About <? echo $userinfo[first]; ?></font></b><br><br> <? ## display bio information from the database echo $userinfo[bio]; ?> </td></tr></table></td> <td width="20%"><table border=0 cellpadding=5 cellspacing=0 valign=top> <? if ($configs[goodlinks] == '1') { require "goodlinks.php"; } if ($configs[badlinks] == '1') { require "badlinks.php"; } ?> </table></td></tr> <tr><td align=center colspan=3> <? echo $userinfo[first]." ".$userinfo[last]." - ".$userinfo[address]." - ".$userinfo[phone]." - <a href="mailto:".$userinfo[email]."">".$userinfo[email]."</a>"; ?> </td></tr> </table> </body> </html> <? }
}
?> |