Skip to content

Commit

Permalink
added example 10 and renumbered one component query examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Donal Ellis committed Jul 16, 2011
1 parent eec67da commit 8b42dcf
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 6 deletions.
10 changes: 5 additions & 5 deletions 10.one_component_query_1.php → 09.one_component_query_1.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
<title>One Component Test Page</title>
</head>
<body>
<br><a href="10.one_component_query_2.php?input=item1">Add Item 1</a>
<br><a href="10.one_component_query_2.php?input=item2">Add Item 2</a>
<br><a href="10.one_component_query_2.php?input=item3">Add Item 3</a>
<br><a href="10.one_component_query_2.php?input=item4">Add Item 4</a>
<br><a href="10.one_component_query_2.php?input=item5">Add Item 5</a>
<br><a href="09.one_component_query_2.php?input=item1">Add Item 1</a>
<br><a href="09.one_component_query_2.php?input=item2">Add Item 2</a>
<br><a href="09.one_component_query_2.php?input=item3">Add Item 3</a>
<br><a href="09.one_component_query_2.php?input=item4">Add Item 4</a>
<br><a href="09.one_component_query_2.php?input=item5">Add Item 5</a>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@
// variable $_SERVER["HTTP_REFERER"]
header("Location: {$_SERVER["HTTP_REFERER"]}");
exit;
?>
25 changes: 25 additions & 0 deletions 10.html_form.php
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>
38 changes: 38 additions & 0 deletions 10.regions.php
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

46 changes: 46 additions & 0 deletions 10.results.php
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>
59 changes: 59 additions & 0 deletions 10.wines.php
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

0 comments on commit 8b42dcf

Please sign in to comment.