Work through this example from a PHP book by Larry Ullman that prints out 5 cities and the zip codes.
>br /> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Cities and Zip Codes
<br />h2 {<br /> font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; <br /> font-size: 14pt; <br /> color : #960;<br /> text-align: center;<br />}<br />td {<br /> font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; <br /> font-size: 10pt; <br /> color : #333<br />}<br />.error {<br /> color: #F30;<br />}<br />
/* This page retrieves and displays all of the
* cities and zip codes for a particular state.
* The results will be shown in a table.
*/
// Abbreviation of state to show:
$state = 'AK';
// Items to display per row:
$items = 5;
// Print a caption:
echo "Cities and Zip Codes found in $state\n";
// Connect to the database:
$dbc = @mysqli_connect ('localhost', 'username', 'password', 'zips') OR die ('
Cannot connect to the database.');
// Get the cities and zip codes, ordered by city:
$q = "SELECT city, zip_code FROM zip_codes WHERE state='$state' ORDER BY city";
$r = mysqli_query($dbc, $q);
// Retrieve the results:
if (mysqli_num_rows($r) > 0) {
// Start a table:
echo '</pre>
<table border="2" width="90%" cellspacing="3" cellpadding="3" align="center">
';
// Need a counter:
$i = 0;
// Retrieve each record:
while (list($city, $zip_code) = mysqli_fetch_array($r, MYSQLI_NUM)) {
// Do we need to start a new row?
if ($i == 0) {
echo "\n";
}
// Print the record:
echo "\t$city, $zip_code\n";
// Increment the counter:
$i++;
// Do we need to end the row?
if ($i == $items) {
echo "\n";
$i = 0; // Reset counter.
}
} // End of while loop.
if ($i > 0) { // Last row was incomplete.
// Print the necessary number of cells:
for (;$i echo " \n";
}
// Complete the row.
echo '';
} // End of ($i > 0) IF.
// Close the table:
echo '</table>';<br><br>} else { // Bad state abbreviation.<br><br> echo '<p class="error">An invalid state abbreviation was used.</p>';<br><br>} // End of main IF.<br><br>// Close the database connection:<br>mysqli_close($dbc);<br><br>?><br><br><p