|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today! |
|
#1
|
|||
|
|||
|
Article Discussion: Implementing a Template Based Web Site With PHP
Implementing a Template Based Web Site With PHP Need help with this article or have your own outtake on a PHP template based website? Please post your comments/questions under this thread.
You can read the article here . |
|
#2
|
|||
|
|||
|
Just a quick question about the actuall template,
(im going to use an example here) say this is my template <html> <head> <title>$title</title> </head> <body> $content<br> Todays Date: $date </body> </html> now that would work, but what about this. <html> <head> <title>$title</title> </head> <body> $content<br> Todays Date: <?php echo gmdate("d-m-Y"); ?> </body> </html> Would that work/is their any way to make it run any php inside the template?? I know this defeats the point of a template (seperating the php from the html) but it saves me declairing the date on every page Thanks |
|
#3
|
|||
|
|||
|
if you used the eval() function then yes, it would.
|
|
#4
|
|||
|
|||
|
PHP templates for web pages
I developed my own template-based PHP/MySQL default web site. Anyone is welcome to use it if they like. Be sure to read all the comments so you can follow my logic.
I use three files: "page.inc", "baseCalendar.inc" and "template.php". I cannot attach the files since they are not valid file types for attachment, so I enclose them in their entirety in the main body of this post. Here is page.inc: <?php // page.inc // to create a new page, use template.php class Page { // default properties for all class members var $title = "PHP Page"; var $host = "phpHost"; var $user = "phpUser"; var $pwd = "phpPwd"; var $db = "phpDb"; var $MYSQL_ERRNO = ''; var $MYSQL_ERROR = ''; // constructor function (to create a Page object) function Page($newTitle=false, $newHost=false, $newUser=false, $newPwd=false, $newDb=false) { $this->title = ($newTitle) ? $newTitle : $this->title; $this->host = ($newHost) ? $newHost : $this->host; $this->user = ($newUser) ? $newUser : $this->user; $this->pwd = ($newPwd) ? $newPwd : $this->pwd; $this->db = ($newDb) ? $newDb : $this->db; } // default methods function displayTitle() {echo("<title>" .$this->title. "</title>");} function displayHead() { ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <META name="robots" content="noindex, nofollow"> <meta name="keywords" content="noindex, nofollow"> <meta name="description" content="noindex, nofollow"> <META NAME="author" CONTENT="Bill Sanders"> <META NAME="copyright" CONTENT="©2002"> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript"> <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> <META HTTP-EQUIV="Window-target" CONTENT="_top"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> <META HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT"> <style type="text/css"> <!-- body { margin:10px; background-color:#F7D79B; color:black; font:14px/1.2 arial,helvetica,sans-serif } h2 { background-color:#9D7F46; color:#FFFFC5; padding:5px; border:1px solid black; margin:0; text-align:center } h3 { text-align:center } tr { vertical-align:top } .indent { margin-left:50px } .fl { clear:both; float:left; height:1.8em } .fr { clear:right; float:right; height:1.8em } .center { text-align:center } .dark { background-color:#f7c86d } .light { background-color:#f7e4bf } .navbar { text-align:right; background-color:#ccaf7d; font-size:smaller; padding:3px; margin:0; border-left:1px solid black; border-bottom:1px solid black } .navbar a { color:#FFFFC5 } .navbar a:visited { color:#7D5F26 } @media print { body { font:11pt/1.1 'times new roman',times,serif; margin:0; background-color:white; color:black; } h2 { background-color:#ccc; color:black; border:1px solid black; padding:0.3em; text-align:center } .dark { background-color:#bbb } .light { background-color:#ddd } } --> </style> <?php $this->displayTitle(); ?> <script type="text/javascript"> <!-- // for refresh time in footer and status bar greeting today = new Date(); hours = today.getHours(); minutes = today.getMinutes(); if (hours<7) greeting = 'Good morning! (getting an early start?)'; else if (hours<12 && hours>6) greeting = 'Good morning!'; else if (hours<18 && hours>11) greeting = 'Good afternoon!'; else if (hours<21 && hours>17) greeting = 'Good evening!'; else greeting = 'Good evening! (working late?)'; halfDay = (hours>11 && hours<24) ? 'PM' : 'AM'; hour = (hours>12) ? (hours-12) : hours; minute = (minutes<10) ? ':0' +minutes : ':' +minutes; timeStr = hour+minute+ ' ' +halfDay; window.defaultStatus=greeting; // --> </script> <?php } function displayFoot(){ ?> <br style="clear:both"><br> <div style="text-align:right; font-size:x-small"> This page was last modified on our server on <?php echo date("j M Y",getlastmod()); ?><br> and last refreshed on our server at <?php echo date("h:i a, "). " Central Time, " .date("j M Y"); ?><br> <script language="JavaScript" type="text/javascript"> <!-- document.write('Which reads as '+timeStr+ ' on this computer.'); // --> </script> </div> </body> </html> <?php } function dbConnect() { $dbConn = mysql_connect($this->host, $this->user, $this->pwd); if (!$dbConn) { $this->MYSQL_ERRNO = mysql_errno(); $this->MYSQL_ERROR = mysql_error(); return false; } else if ($this->db && !mysql_select_db($this->db,$dbConn)) { $this->MYSQL_ERRNO = mysql_errno(); $this->MYSQL_ERROR = mysql_error(); return false; } else return $dbConn; } function sql_error() { if (!$MYSQL_ERROR) { $this->MYSQL_ERRNO = mysql_errno(); $this->MYSQL_ERROR = mysql_error(); } return "error number ".$this->MYSQL_ERRNO.": ".$this->MYSQL_ERROR; } // end of Page class } ?> |
|
#5
|
|||
|
|||
|
I developed my own template-based PHP/MySQL default web site. Anyone is welcome to use it if they like. Be sure to read all the comments so you can follow my logic.
I use three files: "page.inc", "baseCalendar.inc" and "template.php". I cannot attach the files since they are not valid file types for attachment, so I enclose them in their entirety in the main body of this post. Here is baseCalendar.inc: <?php class BaseCalendar { var $numYear = ""; var $textMonth = ""; var $thisMonthTimestamp = ""; var $numMonth = ""; var $nextMonthTimestamp = ""; var $numNextMonth = ""; var $textNextMonth = ""; var $yearNextMonth = ""; var $followingMonthTimestamp = ""; var $numFollowingMonth = ""; var $calWidth = "100px"; var $calBGColor = "#f7e4bf"; var $calTextColor = "#000000"; // constructor function, sets current date as default function BaseCalendar($YYYY=false, $Mmm=false, $calWid=false, $calBGC=false, $calTC=false) { $this->numYear = ($YYYY) ? $YYYY : date("Y"); $this->textMonth = ($Mmm) ? $Mmm : date("M"); $this->thisMonthTimestamp = strtotime("1 $this->textMonth $this->numYear"); $this->numMonth = date("n",$this->thisMonthTimestamp); $this->nextMonthTimestamp = strtotime("next month",$this->thisMonthTimestamp); $this->numNextMonth = date("n",$this->nextMonthTimestamp); $this->textNextMonth = date("M",$this->nextMonthTimestamp); $this->yearNextMonth = date("Y",$this->nextMonthTimestamp); $this->followingMonthTimestamp = strtotime("+2 month",$this->thisMonthTimestamp); $this->numFollowingMonth = date("n",$this->followingMonthTimestamp); $this->calWidth = ($calWid) ? $calWid : $this->calWidth; $this->calBGColor = ($calBGC) ? $calBGC : $this->calBGColor; $this->calTextColor = ($calTC) ? $calTC : $this->calTextColor; echo("<table padding=0 cellpadding=0 style=\"border:1px solid black; float:right; background-color:".$this->calBGColor."; color:".$this->calTextColor."\">\n"); echo("<tr valign=\"top\"><td>"); $this->displayMonth(); echo("</td><td style=\"border-left:1px solid black\">"); $this->displayNextMonth(); echo("</td></tr></table>\n"); } function displayMonth() { $daysInMonth = date("t",$this->thisMonthTimestamp); $dayMonthStarts = date("w",$this->thisMonthTimestamp); $dayNextMonthStarts = date("w",$this->nextMonthTimestamp); $today = date("n/j"); echo "<table style=\"padding:0; color:#6D4F16; font-size:x-small; line-height:1em; width:".$this->calWidth."\">\n"; echo "<tr> <th colspan=\"7\" style=\"text-align:center\">".$this->textMonth." ".$this->numYear."</th></tr>\n"; echo "<tr><th>Su</th><th>Mo</th><th>Tu</th> <th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr>\n"; echo "<tr style=\"text-align:center\">\n"; for ($i=0; $i<$dayMonthStarts; $i++) { echo "<td> </td>\n"; } for ($i=1; $i<=$daysInMonth; $i++) { if ($today==date("n/j",strtotime("$i $this->textMonth $this->numYear"))) { echo "<td style=\"background-color:#f7c86d; text-align:center; color:black\">$i</td>"; } else { echo "<td style=\"text-align:center\">$i</td>\n"; } if (date("w",strtotime("$i $this->textMonth $this->numYear"))==6 && $i<$daysInMonth) { echo "</tr><tr style=\"text-align:center\">\n"; } else if (date("w",strtotime("$i $this->textMonth $this->numYear"))==6 && $i==$daysInMonth) { echo "</tr>\n"; } else if ($i==$daysInMonth) { for ($h=$dayNextMonthStarts; $h<7; $h++) { echo "<td> </td>\n"; } echo "</tr>\n"; } } echo "</table>\n"; } function displayNextMonth() { $daysInNextMonth = date("t",$this->nextMonthTimestamp); $dayNextMonthStarts = date("w",$this->nextMonthTimestamp); $dayFollowingMonthStarts = date("w",$this->followingMonthTimestamp); echo "<table style=\"padding:0; color:#6D4F16; font-size:x-small; line-height:1em; width:".$this->calWidth."\">\n"; echo "<tr><th colspan=\"7\" style=\"text-align:center\">".$this->textNextMonth." ".$this->yearNextMonth."</th></tr>\n"; echo "<tr><th>Su</th><th>Mo</th><th>Tu</th> <th>We</th><th>Th</th><th>Fr</th><th>Sa</th></tr>\n"; echo "<tr style=\"text-align:center\">\n"; for ($i=0; $i<$dayNextMonthStarts; $i++) { echo "<td> </td>\n"; } for ($i=1; $i<=$daysInNextMonth; $i++) { echo "<td style=\"text-align:center\">$i</td>\n"; if (date("w",strtotime("$i $this->textNextMonth $this->yearNextMonth"))==6 && $i<$daysInNextMonth) { echo "</tr><tr style=\"text-align:center\">\n"; } else if (date("w",strtotime("$i $this->textNextMonth $this->yearNextMonth"))==6 && $i==$daysInNextMonth) { echo "</tr>\n"; } else if ($i==$daysInNextMonth) { for ($h=$dayFollowingMonthStarts; $h<7; $h++) { echo "<td> </td>\n"; } echo "</tr>\n"; } } echo "</table>\n"; } } ?> |
|
#6
|
|||
|
|||
|
I developed my own template-based PHP/MySQL default web site. Anyone is welcome to use it if they like. Be sure to read all the comments so you can follow my logic.
I use three files: "page.inc", "baseCalendar.inc" and "template.php". I cannot attach the files since they are not valid file types for attachment, so I enclose them in their entirety in the main body of this post. Here is template.php: <?php // The next three lines prevent this page from caching // header ("Cache-Control: no-cache, must-revalidate"); // header ("Pragma: no-cache"); // header ("Expires: Mon,26 Jul 1997 05:00:00 GMT"); # set error reporting level to fatal only error_reporting(1); // Set the default values for this page ONLY if different from page.inc. // Remember to put strings in quotes! // change 'Template' to your new title for this page $newTitle = "Template"; // change 'localhost' to your default host name for this page $host = ($newHost) ? $newHost : 'localhost'; // change 'Satch' to your default user name for this page $userID = ($newUser) ? $newUser : 'Satch'; // change '123456' to your default password for this page $password = ($newPwd) ? $newPwd : '123456'; // change 'SUDSY' to your default database name for this page $database = ($newDb) ? $newDb : 'SUDSY'; // If a Form on this page is to change any of these values, set // the form Element name="newDb" or name="newPwd" or // name="newUser" or name="newHost" respectively. // For example, <input type="text" name="newUser" // value="type new user ID here"> // "page.inc" establishes a class for the Page object require_once("includes/page.inc"); // Instantiate the Page class (create a new Page object) $thisPage = new Page($newTitle,$host,$userID,$password,$database); // Display the HTML from the beginning of the document $thisPage -> displayHead(); ?> <style type="text/css"> <!-- /* Add style rules specific to this page only. */ --> </style> <script language="JavaScript" type="text/javascript"> <!-- /* Add scripting specific to this page only. */ // --> </script> </head> <body> <?php include( 'includes/BaseCalendar.inc' ); $newCals = new BaseCalendar(); ?> <h2>header</h2> <div class="navbar"> <a href="../main.php">Return to: Main Page</a> </div> <br> <div style="text-align:right"> <i><script language="JavaScript" type="text/javascript"> <!-- document.writeln(greeting); // --> </script></i> </div> <br style="clear:both"><br> <!-- content goes here --> <?php // Display the HTML from the end of the document $thisPage -> displayFoot(); ?> |
|
#7
|
|||
|
|||
|
Not working with me!
Here is the code: PHP Code:
It gives me: Parse error: parse error in /home/telefon/public_html/admin/test.php on line 47 I have both tables (items & templates) setup as said in the article |
|
#8
|
|||
|
|||
|
$query = mysql_query("SELECT title,hits FROM items ORDER BY hits DESC LIMIT 10")
whats wrong with this line? your missing the ';' i make this mistake all the time, it should work now me thinks |
|
#9
|
|||
|
|||
|
oops!
How didn't I notice it!Any way this is how it is in the article, I also noticed anther error in the article, in page 3 Code:
CREATE TABLE items ( id int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, title varchar(30) NOT NULL, hits int NOT NULL DEFAULT '', ); There is an extra ",". Now after I added the missing ";" I'm getting anther error: -START- Warning: Supplied argument is not a valid MySQL result resource in /home/public_html/test.php on line 21 Warning: Supplied argument is not a valid MySQL result resource in /home/public_html/test.php on line 21 Warning: Supplied argument is not a valid MySQL result resource in /home/public_html/test.php on line 21 Warning: Supplied argument is not a valid MySQL result resource in /home/public_html/test.php on line 21 -END- It would be nice if the author of this article check it and fix all the error. Thanx alot ![]() |
|
#10
|
||||
|
||||
|
Any chance of getting the support material for this article posted? I am kind of lost, but I would probably be okay if I had the files in front of me to examine.
Thanks |
|
#11
|
|||
|
|||
|
eval() and functions
i was hoping someone would help me out with this...
i used the article's template design in my pages, and it's working quite well. however, i wanted to include a poll within the template HTML, but the poll code is grabbed as... display_poll(n), where n is the poll id when i place $poll in the template and use $poll = display_poll(n); and then the eval statement to parse $poll, "display_poll(n);" just comes up as plain text instead of the actual poll HTML. any help on this would be greatly appreciated! |
|
#12
|
|||
|
|||
|
Hi everyone. I've just paid another visit to devArticles and noticed all your comments. The two syntax errors you have pointed out have been corrected on the original article at URL There is also a support file available here.
I'd forgotten that this article had already been published here, so I'll have to send an email of to Mitchell. m0h: The error 'Warning: Supplied argument is not a valid MySQL result resource' means that for some reason an error has occured on the MySQL statement. I suggest you get the actual error message by doing echo mysql_error(); and see what's gone wrong. I hope this helps you all Cheers, James URL |
|
#13
|
|||
|
|||
|
I get a syntax error on line 13 when I try to execute this script (came from the article). It is labeled with "(13)".
db.php has the connection details for MySQL. It also selects the database: PHP Code:
|