Indent HTML portions question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Squirrel

    Indent HTML portions question

    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?

    Thanks.

    <?php
    } else {

    // Connect to the database.
    $dbConnection = dbConnect($dbHo st, $dbUser, $dbPassword, $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_query($qu ery);
    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('$newp assword'),
    fullname = '$_POST[newname]',
    email = '$_POST[newemail]',
    notes = '$_POST[newnotes]'";

    if (!mysql_query($ query)) {
    errorMessage("$ query");
    }

    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <TITLE>Registra tion SUCCESS!</TITLE>
    </head>
    <body>
    Database insertion was successful. You are now registered.
    </body>
    </html>
    <?php
    }
    ?>
  • Tim Van Wassenhove

    #2
    Re: Indent HTML portions question

    In article <ROaQc.3934$RO. 1397@newssvr15. news.prodigy.co m>, Squirrel wrote:[color=blue]
    > 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?[/color]

    The lazy solution would be to buffer the output.
    Let tidy clean up the buffer.
    And finally output the clean HTML.

    --
    Tim Van Wassenhove <http://home.mysth.be/~timvw>

    Comment

    • Ira Baxter

      #3
      Re: Indent HTML portions question

      "Squirrel" <no@spam.com> wrote in message
      news:ROaQc.3934 $RO.1397@newssv r15.news.prodig y.com...[color=blue]
      > Given the following piece of code, I was looking for suggestions on how to[/color]
      beautify[color=blue]
      > the portion of the HTML where I jump out of php mode and back in again.[/color]
      The HTML does[color=blue]
      > not have any indenting in order to make the output more readable.[/color]
      However, by not[color=blue]
      > indenting the php code, the php code becomes slightly more difficult to[/color]
      read. Is there[color=blue]
      > a way to have both the php and HTML codes indent correctly?[/color]

      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
      }
      ?>


      Comment

      Working...