Re: Indent HTML portions question

From: Ira Baxter (idbaxter_at_semdesigns.com)
Date: 08/07/04


Date: Sat, 7 Aug 2004 12:59:00 -0500


"Squirrel" <no@spam.com> wrote in message
news:ROaQc.3934$RO.1397@newssvr15.news.prodigy.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/Products/Formatters/PHPFormatter.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=dbConnect($dbHost,$dbUser,$dbPassword,$db);
        // Check required fields.
        if ($_POST['newid'] == '' or $_POST['newname'] == '' or
$_POST['newemail'] == '')
        {
            errorMessage("One 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_query($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="test";
        $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.01transitional//en""http://www.w3.org/tr/html4/l
oose.dtd">
<html>
     <head>
          <title>Registration 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=dbConnect($dbHost,$dbUser,$dbPassword,$db);
        // Check required fields.
        if ($_POST['newid'] == '' or $_POST['newname'] == '' or
$_POST['newemail'] == '')
        {
            errorMessage("One 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_query($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="test";
        $query="INSERT INTO user SET"
        . "userid = '$_POST[newid] ',"
        . "password = PASSWORD('$newpassword'),"
        . "fullname = '$_POST[newname] ',"
        . "email = '$_POST[newemail] ',"
        . ".notes = '$_POST[newnotes] '";
        if (!mysql_query($query))
        {
            errorMessage("$query");
        }
        ?>
<!doctype
htmlpublic"-//w3c//dtdhtml4.01transitional//en""http://www.w3.org/tr/html4/l
oose.dtd">
<html>
     <head>
          <title>Registration SUCCESS!</title>
     </head>
     <body> Database insertion was successful. You are now registered.
     </body>
</html>
<?php
    }
?>


Relevant Pages

  • Re: following radio & hidden does not work..... help please.
    ... So you query the database for records and present the concatenated fname and lname for the user to choose from. ... This works the same with a radio button or a select statement. ... This is a HTML or PHP question! ...
    (alt.html)
  • Re: Why not generate static pages instead of dynamic?
    ... html pages out there now, ... But querying a database, ... is fetched through AJAX or a 'more-dynamic' page (php ... dynamic/user-interaction page or not, and on that basis will serve the html ...
    (comp.lang.php)
  • Re: following radio & hidden does not work..... help please.
    ... Using a HTML radio button form as previously stated returns all the data as requested. ... The problem seems to be HOW does one get more than one database variable to be to the PHP action program. ... The objective of the form is to use either a or a radio button form so a user can select a person from the database. ...
    (alt.html)
  • Re: help with posting conditional if statement
    ... certain fields have values, if all the required fields have values, i want ... I'm new to php ... Variables aren't being recognized by the html as defining the form. ... I think this is what Geoff meant, he just forgot the default case. ...
    (comp.lang.php)
  • Re: Accessing Class Method
    ... public $database; ... public $html; ...
    (comp.lang.php)