Introduction To Output Formatting
Introduction To Output Formatting
formatting
Web Systems and Technologies
basicSelection.php
• This script did not format the data very well
• We should use tables to display data
• Table tags
• <table> </table>
• <tr> </tr> a row
• <td> </td> a data cell
• We can also use print to output HTML
print
• print("<table width='500' border='1'>");
• This command will create a table with a width of 500 pixels and a
border
• Make this the first command in the modified basicSelection.php
• (after the if ($connectionSuccess == 1) { )
Be more selective
Use SQL to retrieve the title and author
$Title = $row['title'];
$Author = $row['author'];
Print the results in table cells
print("<tr>");
print("<td>$Title </td><td> $Author</td>");
print("</tr>");
Complete code
if ($connectionSuccess == 1) {
print("<table width='500' border='1'>");
$result = mysql_query("SELECT title, author FROM books");
while ($row = mysql_fetch_array($result)) {
$Title = $row['title'];
$Author = $row['author'];
print("<tr>");
print("<td>$Title </td><td> $Author</td>");
print("</tr>");
}
mysql_free_results($result); //free up server memory
}
Appearance
PHP: Selecting Pieces
of Data
Web Systems and Technologies
PHP includes
• To avoid repeating code we can use an
include to add an existing script to a new
script
• Take the connection.php script and cut the
$hostname, $username, $password and
$databaseName and paste into a new file
called basicSelection.php
basicSelection file
• <?php
• /*
• File: basicSelection.php
• By: Bob
• Date: 2012-03-04
And so it goes
Escaping PHP for HTML
if ($connectionSuccess == 1) {
$result = mysql_query("SELECT * FROM books");
while ($row = mysql_fetch_array($result)) {
echo $row[2];
echo "<br/>";
echo $row["author"];
?>
<br/><br/>
<?php //This restores PHP after escaping it for the HTML
}
mysql_free_results($result); //free up server memory
}
Exercises
• Try:
• retrieving different columns
• formatting the results using HTML and CSS