PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


Embedding PHP in HTML
Pages: 1, 2

Embedded code in action

Now that we have a feel of the syntax behind embedded control structures, let's look at a likely situation where our new knowledge could be put to use. What if we wanted to list, in an HTML table, all the numbers between 1 and 7 and determine under what circumstances our friend Billy could purchase a candy bar? Before we begin, we should get an idea of exactly how our HTML table should be constructed. For our purposes, we will be creating a table of the following form:

1234567
nonononoyesyesyes

Now, let's take a loop at the HTML behind this table:



<html>
<body>
<table>
<tr>
  <td align="center">1</td>
  <td align="center">2</td>
  <td align="center">3</td>
  <td align="center">4</td>
  <td align="center">5</td>
  <td align="center">6</td>
  <td align="center">7</td>
</tr>
<tr>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">no</td>
  <td align="center">yes</td>
  <td align="center">yes</td>
  <td align="center">yes</td>
</tr>
</table>
</body>
</html>

Notice that, when dealing with static HTML we have no flexibility whatsoever to alter the content of the table without manually changing it. What if we wanted to show a table of all the possibilities from 1 to 100? Again, accomplishing such a task by hand is a waste of time. However, with PHP, we can find a solution to our dilemma quickly and easily:

<html>
<body>
<table>
<tr>
<?php for($l = 1; $l <=7; $l++) : ?>
<td align="center"><?=$l?></td>
<?php endfor; ?>
        </tr>
        <tr>
            <?php for($l = 1; $l <=7; $l++) : ?>
            <td align="center">
<?php if($l >= 5)  { 
        
        echo "yes";
    
    } else {

        echo "no";

    }
?>
</td>
<?php endfor; ?>
        </tr>    
</table>
</body>
</html>

What have we done? Looking at this example, we start by simply outputting the basic HTML code to construct the web page and begin a table. Then, we use PHP to start a for loop to count from 1 to 7. Within this loop, we display the HTML code to first start a table cell, then display the variable we are counting with ($L) and finally the HTML to close the table cell.

Once this has been completed, we close our row and start a new row and repeat the same looping process. This time, however, instead of simply outputting the looped variable, we use a conditional statement to determine if it is greater than or equal to the value 5 in which case we output "yes" or "no" depending on the value of $L. Finally, we finish the HTML for our table and web page and the script ends.

Also in PHP Foundations:

Using MySQL from PHP, Part 2

Using MySQL from PHP

MySQL Crash Course, Part 3

MySQL Crash Course, Part 2

MySQL Crash Course

The result? Exactly the same static HTML page as we originally constructed except now we have the flexibility to examine any range with no extra effort. It is recommended that you play around and get very familiar with this syntax because it will be used quite often in future articles and in everyday practice.

John Coggeshall is a a PHP consultant and author who started losing sleep over PHP around five years ago.


Read more PHP Foundations columns.

Return to the PHP DevCenter.




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: