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:

// 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
<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
$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 
         $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));

-----Original Message-----
From: James Taylor [mailto:[EMAIL PROTECTED]] 
Sent: Thursday, February 14, 2002 2:10 PM
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:


   if ($submitdata) {
   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>";

PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to