Source of: example.5-12.php

<?
/*
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.
*/
?>
<!DOCTYPE HTML PUBLIC 
               "-//W3C//DTD HTML 4.0 Transitional//EN"
               "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Explore Wines</title>
</head>

<body bgcolor="#ffffff">
<form action="example.5-1.php" method="GET">
<?
  
include 'clean.inc';

   function 
selectDistinct ($connection,
                            
$tableName,
                            
$columnName,
                            
$pulldownName,
                            
$additionalOption,
                            
$defaultValue)
  {
     
$defaultWithinResultSet FALSE;

     
// Query to find distinct values of $columnName
     // in $tableName
     
$distinctQuery "SELECT DISTINCT $columnName
                       FROM 
$tableName";

     
// Run the distinctQuery on the databaseName
     
if (!($resultId = @ mysql_query ($distinctQuery
                                      
$connection)))
        
showerror();

     
// Retrieve all distinct values
     
$i 0;
     while (
$row = @ mysql_fetch_array($resultId))
        
$resultBuffer[$i++] = $row[$columnName];

     
// Start the select widget
     
echo "\n<select name=\"$pulldownName\">";       

     
// Is there an additional option?
     
if (isset($additionalOption))
        
// Yes, but is it the default option?
        
if ($defaultValue == $additionalOption)
           
// Show the additional option as selected
           
echo "\n\t<option selected>$additionalOption";
        else
           
// Just show the additional option
           
echo "\n\t<option>$additionalOption";

     
// check for a default value
     
if (isset($defaultValue))
     {
        
// Yes, there's a default value specified

        // Check if the defaultValue is in the 
        // database values
        
foreach ($resultBuffer as $result)
           if (
$result == $defaultValue)
              
// Yes, show as selected
              
echo "\n\t<option selected>$result";
           else
              
// No, just show as an option
              
echo "\n\t<option>$result";
     }  
// end if defaultValue
     
else 
     { 
        
// No defaultValue
       
        // Show database values as options
        
foreach ($resultBuffer as $result)
           echo 
"\n\t<option>$result";
     }
     echo 
"\n</select>";
  } 
// end of function


  // Connect to the DBMS
  
if (!($connection = @ mysql_connect($hostName
                                      
$username
                                      
$password)))
     die(
"Could not connect to database");

  if (!
mysql_select_db($databaseName))
     
showerror();

  echo 
"\nRegion: ";

  
// Produce the select list
  // Parameters:
  // 1: Database connection
  // 2. Table that contains values
  // 3. Attribute that contains values
  // 4. <SELECT> element name
  // 5. An additional non-database value
  // 6. Optional <OPTION SELECTED>
  
selectDistinct($connection,
                 
"region",
                 
"region_name",
                 
"regionName",
                 
"All",
                 
"All");

  echo 
"\n<br><input type=\"submit\"" 
       
"value=\"Show wines\">" .
       
"\n</form>\n<br>";
  echo 
"<a href=\"index.html\">Home</a>";
?>
</body>
</html>


© 2000--2001 Hugh E. Williams and David Lane.
Valid HTML 4.01!