RE: [PHP] good practice
| From: | Matt Schroebel | Date: | Thu, 14 Feb 2002 19:48:29 +0000 |
| Subject: | RE: [PHP] good practice | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-84808@lists.php.net to get a copy of this message | ||
What I do is use templates to separate the html from the php. So I end up with (at least) 2 files
per page, one the template; the other the html. If you have tables, then you need templates for the
rows. My model is basically:
If ("POST" == $HTTP_SERVER_VARS['REQUEST_METHOD']) {
// do data validation loading error messages into an array
// loading the form variables as I go in case there are errors // if (empty($errors)) then build the
output // otherwise redisplay the page with errors -- retaining the user input // } // else if get
then this is the first time we've seen the request, so show empty form { }
It's pretty clean this way. The templates I use came from Web Application Development with PHP
4.0 (with CD-ROM)
Tobias Ratschiller, Till Gerken and I'm not sure they're on the web. It's a simple
engine that I tweaked a little to log and report errors. You don't actually have to output the
template straight to the browser, but can save them into strings vars and use them as data for
another template.
In the sample below the variables in the HTML are in the curly braces i.e. {FIRSTNAME}
--- sample chop of html JobTitles.inc.php
<html>
<head>
</head>
<body>
{ERRORS}
<form method="post" action="{ACTION}">
First Name: <input type="text" name="firstName" size="25"
maxlength="20" value="{FIRSTNAME}"><br> Last Name: <input
type="text" name="lastName" size="25" maxlength="20"
value="{LASTNAME}"><br> Job Title: {JOB_TITLE}<br> Job Description:
{JOB_DESCRIPTION}<br> <input type="submit" name="Submit"
value="Submit"> <input type="Submit" name="Reset"
value="Reset"> </form>
--- sample php
<?php
$REQUEST_METHOD = $HTTP_SERVER_VARS[REQUEST_METHOD];
$submit = $HTTP_POST_VARS[Submit];
if ('POST' == $REQUEST_METHOD && !empty($submit)) {
$lastName = $HTTP_POST_VARS[lastName];
$firstName = $HTTP_POST_VARS[firstName];
$sql = "select * from employees";
$sql .= " where EmpFirstName like '%" . addslashes($firstName) . "%' and
";
$sql .= " EmpLastName like '%" . addslashes($lastName) . "%'";
$db = mysql_connect("host", "username", "secretword") or
die(log_mysql_error($sql,__FILE__,__LINE__));
mysql_select_db("myDbname",$db);
$result = mysql_query($sql,$db) or die(mysql_error());
if ($row = mysql_fetch_array($result)) {
$lastName = $row[EmpLastName];
$firstName = $row[EmpFirstName];
$jobTitle = $row[EmpTitle];
$jobDescription = $row[EmpJobd];
} else {
$error[] = "Employee not found.";
}
} else { // do something different on get (for example)
$firstName = "Type first name here";
$lastName = "Type last name here";
}
// ----------------------------------------
// build the webpage
// ----------------------------------------
If (is_array($error)) {
foreach ($error as $value) {
$errors .= $value . "<br>";
}
}
$tpl = new EasyTemplate("JobTitles.inc.htm");
$tpl->assign("ERRORS",$errors);
$tpl->assign("FIRSTNAME",htmlspecialchars($firstName));
$tpl->assign("LASTNAME",htmlspecialchars($lastName));
$tpl->assign("JOB_TITLE",nl2br($jobTitle));
$tpl->assign("JOB_DESCRIPTION",nl2br($jobDescription));
$tpl->assign("ACTION",$PHP_SELF);
$tpl->easy_print();
?>
-----Original Message-----
From: James Taylor [mailto:jtx@nestllc.com]
Sent: Thursday, February 14, 2002 2:10 PM
To: php-general@lists.php.net
Subject: [PHP] good practice
Can someone recommend a better method for doing something like the following?
All of my programs are written like this, but it's really poor form
considering I'm not predeclaring my variables, etc. Only thing I can really
think of is to assign a value in the form a number like 1 or something, then
do a if $value == 1 {do something} but that seems kinda hokey. Here's an
example of something I'd do:
<HTML><BODY>
<?
if ($submitdata) {
dosomething;
exit;
}
echo "<form name=\"form\" action=\"$PHP_SELF?\">\n";
echo " <input type=\"text\" name=\"data\">\n";
echo " <input type=\"submit\" name=\"submitdata\" value=\"
Submit \">\n";
echo "</form>\n</body>\n</html>";