PHP: Output Formatting FdSc Module 109 Server side scripting and Database design 2011
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 $result = mysql_query("SELECT title, author FROM books"); while ($row = mysql_fetch_array($result)) {
Assign variables Assign the results of the query to variables $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