"Squirrel" <no@spam.com> wrote in message
news:RO******** ********@newssv r15.news.prodig y.com...
Given the following piece of code, I was looking for suggestions on how to
beautify the portion of the HTML where I jump out of php mode and back in again.
The HTML does not have any indenting in order to make the output more readable.
However, by not indenting the php code, the php code becomes slightly more difficult to
read. Is there a way to have both the php and HTML codes indent correctly?
One answer is to run it through a PHP formatter,
such as ours (See
http://www.semanticdesigns.com/Produ...Formatter.html)
Our formatter will reformat the HTML as well as the PHP source code.
See result below.
I had to add an IF () { fragment head to make your source code legal.
Your $query variable now looks a little funny, because of disagreement
on how to prettyprint very long string constants. We print them
with embedded newlines escaped as "\n". If you want that to
be more readable, you could use the concatenation operator "."
at the end of each line of the string constant, and the prettyprinter
would then line up the string constants nicely.
That version is listed a little further down.
The nice thing about prettyprinting is that you can hack at your
source whatever way is convenient, and let the prettyprinter
clean it all up.
--
Ira D. Baxter, Ph.D., CTO 512-250-1018
Semantic Designs, Inc.
www.semdesigns.com
-- First version ---
<?php
if (dummy)
{
}
else
{
// Connect to the database.
$dbConnection=d bConnect($dbHos t,$dbUser,$dbPa ssword,$db);
// Check required fields.
if ($_POST['newid'] == '' or $_POST['newname'] == '' or
$_POST['newemail'] == '')
{
errorMessage("O ne or more required fields were left blank.
Please fill them in and try again.");
}
$query="SELECT COUNT(*) FROM user WHERE userid = '$_POST[newid] '";
$result=mysql_q uery($query);
if (!$result)
{
errorMessage("A database error occurred.");
}
// Check to see if the userid already exists.
if (mysql_result($ result,0,0)>0)
{
errorMessage("A user with the user id you requested already
exists.");
}
$newpassword="t est";
$query="INSERT INTO user SET\n userid =
'$_POST[newid] ',\n password = PASSWOR
D('$newpassword '),\n fullname = '$_POST[newname] ',\n
email = '$_POST[newemail] ',\n
notes = '$_POST[newnotes] '";
if (!mysql_query($ query))
{
errorMessage("$ query");
}
?>
<!doctype
htmlpublic"-//w3c//dtdhtml4.01tran sitional//en""http://www.w3.org/tr/html4/l
oose.dtd">
<html>
<head>
<title>Registra tion SUCCESS!</title>
</head>
<body> Database insertion was successful. You are now registered.
</body>
</html>
<?php
}
?>
-- Second version --
<?php
if (dummy)
{
}
else
{
// Connect to the database.
$dbConnection=d bConnect($dbHos t,$dbUser,$dbPa ssword,$db);
// Check required fields.
if ($_POST['newid'] == '' or $_POST['newname'] == '' or
$_POST['newemail'] == '')
{
errorMessage("O ne or more required fields were left blank.
Please fill them in and try again.");
}
$query="SELECT COUNT(*) FROM user WHERE userid = '$_POST[newid] '";
$result=mysql_q uery($query);
if (!$result)
{
errorMessage("A database error occurred.");
}
// Check to see if the userid already exists.
if (mysql_result($ result,0,0)>0)
{
errorMessage("A user with the user id you requested already
exists.");
}
$newpassword="t est";
$query="INSERT INTO user SET"
. "userid = '$_POST[newid] ',"
. "password = PASSWORD('$newp assword'),"
. "fullname = '$_POST[newname] ',"
. "email = '$_POST[newemail] ',"
. ".notes = '$_POST[newnotes] '";
if (!mysql_query($ query))
{
errorMessage("$ query");
}
?>
<!doctype
htmlpublic"-//w3c//dtdhtml4.01tran sitional//en""http://www.w3.org/tr/html4/l
oose.dtd">
<html>
<head>
<title>Registra tion SUCCESS!</title>
</head>
<body> Database insertion was successful. You are now registered.
</body>
</html>
<?php
}
?>