-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added example 10 and renumbered one component query examples
- Loading branch information
Donal Ellis
committed
Jul 16, 2011
1 parent
eec67da
commit 8b42dcf
Showing
6 changed files
with
173 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,3 @@ | |
// variable $_SERVER["HTTP_REFERER"] | ||
header("Location: {$_SERVER["HTTP_REFERER"]}"); | ||
exit; | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
require_once "10.regions.php"; | ||
$regions = getRegions(); | ||
?> | ||
<!DOCTYPE HTML PUBLIC | ||
"-//W3C//DTD HTML 4.01 Transitional//EN" | ||
"http://www.w3.org/TR/html401/loose.dtd"> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> | ||
<title>Wines</title> | ||
</head> | ||
<body> | ||
<form action="10.results.php" method="GET"> | ||
Region: | ||
<select name="regionName"> | ||
<?php foreach ($regions as $id => $name): ?> | ||
<option value="<?php echo $id; ?>"><?php echo $name; ?></option> | ||
<?php endforeach; ?> | ||
</select> | ||
<br> | ||
<input type="submit" name="submit" value="Show Wines"> | ||
</form> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
if (basename($_SERVER[SCRIPT_NAME]) != "10.html_form.php") { | ||
header("Location: 10.html_form.php"); | ||
exit; | ||
} | ||
|
||
require_once "db_secure.php"; | ||
|
||
// selectDistinct() function | ||
function getRegions() { | ||
// Connect to the server | ||
if (!($connection = @ mysql_connect(DB_HOST, DB_USER, DB_PW))) { | ||
showerror(); | ||
} | ||
|
||
if (!mysql_select_db(DB_NAME, $connection)) { | ||
showerror(); | ||
} | ||
|
||
// Query to find distinct values of $attributeName in $tableName | ||
$distinctQuery = "SELECT DISTINCT region_id, region_name FROM region"; | ||
|
||
// Run the distinctQuery on the databaseName | ||
if (!($resultId = @ mysql_query ($distinctQuery, $connection))) { | ||
showerror(); | ||
} | ||
|
||
$regions = array(); | ||
|
||
// Retrieve each row from the query | ||
while ($row = @ mysql_fetch_array($resultId)) { | ||
// Get the value for the attribute to be displayed | ||
$regions[$row['region_name']] = $row['region_name']; | ||
} | ||
|
||
return $regions; | ||
} // end of function | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
if ($_GET['submit'] != 'Show Wines') { | ||
header("Location: 10.html_form.php"); | ||
exit; | ||
} | ||
|
||
require_once('10.wines.php'); | ||
$regionName = $_GET['regionName']; | ||
$wines = getWines($regionName); | ||
?> | ||
<!DOCTYPE HTML PUBLIC | ||
"-//W3C//DTD HTML 4.01 Transitional//EN" | ||
"http://www.w3.org/TR/html401/loose.dtd"> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> | ||
<title>Exploring Wines in a Region</title> | ||
</head> | ||
|
||
<body bgcolor="white"> | ||
<?php | ||
if (isset($regionName) && $regionName != 'All') { | ||
echo "Wines of {$regionName}<br/>\n"; | ||
} | ||
?> | ||
<table> | ||
<tr> | ||
<th>Wine ID</th> | ||
<th>Wine Name</th> | ||
<th>Year</th> | ||
<th>Winery</th> | ||
<th>Description</th> | ||
</tr> | ||
<?php foreach($wines as $wine): ?> | ||
<tr> | ||
<td><?php echo $wine['wine_id']; ?></td> | ||
<td><?php echo $wine['wine_name']; ?></td> | ||
<td><?php echo $wine['year']; ?></td> | ||
<td><?php echo $wine['winery_name']; ?></td> | ||
<td><?php echo $wine['description']; ?></td> | ||
</tr> | ||
<?php endforeach; ?> | ||
</table> | ||
<?php echo count($wines); ?> records found matching your criteria | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
if (basename($_SERVER[SCRIPT_NAME]) != "10.results.php") { | ||
header("Location: 10.html_form.php"); | ||
exit; | ||
} | ||
|
||
require 'db_secure.php'; | ||
|
||
// get all wines | ||
function getWines($regionName) { | ||
// Connect to the server | ||
if (!($connection = @ mysql_connect(DB_HOST, DB_USER, DB_PW))) { | ||
showerror(); | ||
} | ||
|
||
if (!mysql_select_db(DB_NAME, $connection)) { | ||
showerror(); | ||
} | ||
|
||
// manually clean data | ||
$regionName = substr($regionName, 0, 30); | ||
$regionName = mysql_real_escape_string($regionName, $connection); | ||
|
||
// Start a query ... | ||
$query = "SELECT wine_id, wine_name, description, year, winery_name | ||
FROM winery, region, wine | ||
WHERE winery.region_id = region.region_id | ||
AND wine.winery_id = winery.winery_id"; | ||
|
||
// ... then, if the user has specified a region, add the regionName | ||
// as an AND clause ... | ||
if (isset($regionName) && $regionName != "All") { | ||
$query .= " AND region_name = '{$regionName}'"; | ||
} | ||
|
||
// ... and then complete the query. | ||
$query .= " ORDER BY wine_name"; | ||
|
||
// Run the query on the server | ||
if (!($result = @ mysql_query ($query, $connection))) { | ||
showerror(); | ||
} | ||
|
||
// Find out how many rows are available | ||
$rowsFound = @ mysql_num_rows($result); | ||
|
||
$wines = array(); | ||
|
||
// If the query has results ... | ||
if ($rowsFound > 0) { | ||
// Fetch each of the query rows | ||
while ($row = @ mysql_fetch_assoc($result)) { | ||
$wines[$row['wine_id']] = $row; | ||
} // end while loop body | ||
} // end if $rowsFound body | ||
|
||
return $wines; | ||
} // end of function | ||
|