/*
Source code example for Web Database Applications
Unless otherwise stated, the source code distributed with this book can be
redistributed in source or binary form so long as an acknowledgment appears
in derived source files.
The citation should list that the code comes from Hugh E.
Williams and David Lane, "Web Database Application with PHP and MySQL"
published by O'Reilly & Associates.
This code is under copyright and cannot be included in any other book,
publication, or educational product without permission from O'Reilly &
Associates.
No warranty is attached; we cannot take responsibility for errors or fitness
for use.
*/
?>
Wines of a Wine Region
column
// Use the array $header[]["attrib"] for the names
// of the database attributes to show in each column
//
// Use $browseString to prefix an embedded link
// to the previous, next, and other pages
function browse($scriptName,
$connection,
$browseString,
$rowOffset,
$query,
$pageHeader,
$header)
{
// (1) Run the query on the database through the
// connection
if (!($result = @ mysql_query ($query, $connection)))
showerror();
// Find out how many rows there are
$rowsFound = @ mysql_num_rows($result);
// Is there any data?
if ($rowsFound != 0)
{
// Yes, there is data.
// (2a) The "Previous" page begins at the current
// offset LESS the number of ROWS per page
$previousOffset = $rowOffset - ROWS;
// (2b) The "Next" page begins at the current offset
// PLUS the number of ROWS per page
$nextOffset = $rowOffset + ROWS;
// (3) Seek to the current offset
if (!mysql_data_seek($result, $rowOffset))
showerror();
// (4a) Output the header and start a table
echo $pageHeader;
echo "\n";
// (4b) Print out the column headers from $header
foreach ($header as $element)
echo "\n\t" . $element["header"] . " | ";
echo "\n
";
// (5a) Fetch one page of results (or less if on the
// last page)
for ( $rowCounter = 0;
(($rowCounter < ROWS) &&
($row = @ mysql_fetch_array($result)) );
$rowCounter++)
{
// Print out a row
echo "\n";
// (5b) For each of the attributes in a row
foreach($header as $element)
{
echo "\n\t";
// Get the database attribute name for the
// current attribute
$temp = $element["attrib"];
// Print out the value of the current
// attribute
echo $row["$temp"];
echo " | ";
} // end foreach attribute
echo "\n
\n";
} // end for rows in the page
// Finish the results table, and start a footer
echo "\n
\n
";
// (6) Show the row numbers that are being viewed
echo ($rowOffset + 1) . "-" .
($rowCounter + $rowOffset) . " of ";
echo "$rowsFound records found matching " .
"your criteria\n
";
// (7a) Are there any previous pages?
if ($rowOffset > 0)
// Yes, so create a previous link
echo "\n\tPrevious ";
else
// No, there is no previous page so don't
// print a link
echo "Previous ";
// Output the page numbers as links
// Count through the number of pages in the results
for($x=0, $page=1;
$x<$rowsFound;
$x+=ROWS, $page++)
// Is this the current page?
if ($x < $rowOffset ||
$x > ($rowOffset + $numRowsToFetch - 1))
// No, so print out a link
echo "" . $page . " ";
else
// Yes, so don't print a link
echo $page . " ";
// (7b) Are there any Next pages?
if (($row != false) && ($rowsFound > $nextOffset))
// Yes, so create a next link
echo "\n\tNext ";
else
// No, there is no next page so don't
// print a link
echo "Next ";
} // end if rowsFound != 0
else
{
echo "
No rows found matching your criteria.\n";
}
// (7c) Create a link back to the query input page
echo "
Back to Search
";
}
// Untaint the user data
$regionName = clean($regionName, 30);
$scriptName = "example.5-11.php";
// Is there any user data?
if (empty($regionName))
{
// No, so show the
Home
column headers
$header[0]["header"] = "Wine ID";
$header[1]["header"] = "Wine Name";
$header[2]["header"] = "Wine Type";
$header[3]["header"] = "Year";
$header[4]["header"] = "Winery";
$header[5]["header"] = "Description";
// Query attributes to display in columns
$header[0]["attrib"] = "wine_id";
$header[1]["attrib"] = "wine_name";
$header[2]["attrib"] = "type";
$header[3]["attrib"] = "year";
$header[4]["attrib"] = "winery_name";
$header[5]["attrib"] = "description";
// Call generic browsing code to browse query
browse($scriptName, $connection,
$browseString, $offset, $query,
$pageHeader, $header);
} // end if else user data
?>