|
[back] Formatting Data While the previous exercise allows us to see what info we have in our table, we are limited in how the data can be displayed. Using HTML and PHP, we can easily create a table which offers more flexibility in how it is viewed. <?php mysql_connect(localhost,$username,$password); $query="SELECT * FROM students"; $num=mysql_numrows($result); echo "<b><center>Database Output</center></b><br><br>"; ?> <table width=600 border=1>
<tr> <?php ?> Explanation: There are two parts to displaying the output inside a table. The first is setting up the table heads before the loop begins. Since it is not part of the loop, it will not change. I used <th> tags, but regular <td> tags would work just as well. <table width=600 border=1> The main part of the table is created after the loop. Notice how each piece of PHP is placed inside the HTML table row and column tags. <tr> The final piece of PHP is incrementing the loop and closing the table. <?php ++$i; ?> BEYOND THE BASICS Now that we are outputting this inside a table, we can modify the table in any number of ways. If we wanted to combine two variables, we could code it as: We could add any HTML attributes to the output as well: Create a new PHP page and save it as display2.php. Make several changes to your output using HTML attributes. |