Heloo all,
I've been working on some code from the Oreilly PHP/Web App book and am trying to create links from the db to either an image or a page. If you look at this code, how would I make the img_src table/attributes an actual link back to either an image or a page? Consider me a new person to PHP, so if there is a better example of what I am trying to accomplish, please push me in the right direction. THANKS YOU!
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
</head>
<body>
<?php
include 'db.inc';
include 'error.inc';
define(ROWS, 5);
// Browse through the $connection by the running $query.
//
// Begin the display of data with row $rowOffset.
// Put a header on the page, $pageHeader
//
// Use the array $header[]["header"] for headers on
// each <table> 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 "<table border=\"0\">\n<tr>";
// (4b) Print out the column headers from $header
foreach ($header as $element)
echo "\n\t<th>" . $element["header"] . "</th>";
echo "\n</tr>";
// (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<tr>";
// (5b) For each of the attributes in a row
foreach($header as $element)
{
echo "\n\t<td>";
// Get the database attribute name for the
// current attribute
$temp = $element["attrib"];
// Print out the value of the current
// attribute
echo $row["$temp"];
echo "</td>";
} // end foreach attribute
echo "\n</tr>\n";
} // end for rows in the page
// Finish the results table, and start a footer
echo "\n</table>\n<br>";
// (6) Show the row numbers that are being viewed
echo ($rowOffset + 1) . "-" .
($rowCounter + $rowOffset) . " of ";
echo "$rowsFound records found matching " .
"your criteria\n<br>";
// (7a) Are there any previous pages?
if ($rowOffset > 0)
// Yes, so create a previous link
echo "\n\t<a href=\"" . $scriptName .
"?offset=" . rawurlencode($previousOffset) .
"&" . $browseString .
"\">Previous</a> ";
else
// No, there is no previous page so don't
// print a link
echo "Previous ";
// (7b) Are there any Next pages?
if (($row != false) && ($rowsFound > $nextOffset))
// Yes, so create a next link
echo "\n\t<a href=\"" . $scriptName .
"?offset=" . rawurlencode($nextOffset) .
"&" . $browseString .
"\">Next</a> ";
else
// No, there is no next page so don't
// print a link
echo "Next ";
} // end if rowsFound != 0
else
{
echo "<br>No rows found matching your criteria.\n";
}
// (7c) Create a link back to the query input page
echo "<br><a href=\"" . $scriptName .
"\">Back to Search</a><br>";
}
// Untaint the user data
$regionName = clean($regionName, 30);
$scriptName = "example.5-9.php";
// Is there any user data?
if (empty($regionName))
{
// No, so show the <form>
?>
<form action="<?=$scriptName;?>" method="GET">
<br>Enter a region to browse :
<input type="text" name="regionName" value="All">
(type All to see all regions)
<br>
<input type="submit" value="Get Images">
</form>
<br><a href="index.html">Home</a>
<?php
} // if user data
else
{
// Yes, there is user data so show the results
// Connect to the DBMS
if (!($connection = @ mysql_connect("localhost",
"root",
"root")))
die("Could not connect to database");
if (!mysql_select_db($databaseName, $connection))
showerror();
// Set $offset to zero if not previously set
if (empty($offset))
$offset = 0;
// Build the query
$query = "SELECT img_dir, img_name,
img_src FROM images";
// Add the regionName if the user has provided it
if ($regionName != "All")
$query .= " AND r.region_name = \"$regionName\"";
// Add a sort on the end of the query
// $query .= " ORDER by w.wine_name";
// Initialize the browse() function parameters
// Query prefix for the next/previous links
$browseString = "regionName=" .
rawurlencode($regionName);
// Page header for the browse screen
$pageHeader = "Wineries of " . $regionName;
// HTML <TABLE> column headers
$header[0]["header"] = "Image Directory";
$header[1]["header"] = "Image Name";
$header[2]["header"] = "Image Source";
// Query attributes to display in <TABLE> columns
$header[0]["attrib"] = "img_dir";
$header[1]["attrib"] = "img_name";
$header[2]["attrib"] = "img_src";
// Call generic browsing code to browse query
browse($scriptName, $connection,
$browseString, $offset, $query,
$pageHeader, $header);
} // end if else user data
?>
</body>
</html>