Advertisement
a_mcruer

php3_6

Feb 17th, 2013
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.19 KB | None | 0 0
  1. <?php
  2.     ERROR_REPORTING(E_ALL);
  3.     ini_set("display_errors", 1);
  4. ?>
  5.  
  6. <! DOCTYPE html>
  7. <html>
  8.     <head>
  9.         <meta charset = UTF-8>
  10.         <title>Aaron McRuer CS3380 Lab 3</title>
  11.     </head>
  12.     <body>
  13.         <form method = "POST" action = "/~asm3kf/cs3380/lab3/lab3.php">
  14.             Search for a:
  15.             <input type = "radio" name = "search_by" value = "country" />Country
  16.             <input type="radio" name="search_by" value="city"/>City
  17.             <input type="radio" name="search_by" value="language"/>Language
  18.             </br>
  19.             </br>
  20.             That begins with:
  21.             <input type="text" name="query_string" value=" "/>
  22.             </br>
  23.             </br>
  24.             <input type= "submit" name="submit" value="Submit"/>
  25.         </form>
  26.    
  27.     <table border="1">
  28.     <?php
  29.        
  30.     if(isset($_POST['submit']))
  31.     {
  32.                 //post if
  33.             echo "hello";
  34.         include 'connect.php';
  35.                 $result;
  36.         $cityQuery = "SELECT *
  37.                                FROM lab2.city
  38.                 WHERE name
  39.                                ILIKE $1
  40.                                 ORDER BY name";    
  41.         $langQuery = "SELECT *
  42.                                FROM lab2.name
  43.                                WHERE name
  44.                                ILIKE $1
  45.                                ORDER BY name";
  46.         $counQuery = "SELECT *
  47.                 FROM lab2.country
  48.                                WHERE name
  49.                 ILIKE $1
  50.                                ORDER BY name";
  51.                 $connection = pg_connect("host=dbhost-pgsql.cs.missouri.edu user=asm3kf password=fpZaYn3U dbname=asm3kf");
  52.                 if(!$connection)
  53.                 {
  54.                     echo "unable to connect to database";
  55.                     die("Failed to connect to database.");                    
  56.                 }
  57.                 //select query based on user input
  58.                
  59.                 else
  60.                 {
  61.                     switch ($_POST['search_by'])
  62.                     {
  63.                         case "city":
  64.                             $statement = pg_prepare($connection, "city", $cityQuery);
  65.                             $result = pg_execute($connection, "city", array($_POST['query_string']."%"));
  66.                             echo "city";
  67.                             to_table($result);
  68.                             break;
  69.                         case "country":
  70.                             $statement = pg_prepare($connection, "country", $counQuery);
  71.                             $result = pg_execute($connection, "country", array($_POST['query_string']."%"));
  72.                             echo "country";
  73.                             to_table($result);
  74.                             break;
  75.                         case "language":
  76.                             $statement = pg_prepare($connection, "language", $langQuery);
  77.                             $result = pg_execute($connection, "language", array($_POST['query_string']."%"));
  78.                             echo "language";
  79.                             to_table($result);
  80.                     }
  81.                 }
  82.         if(!$result)
  83.         {
  84.                     echo "Unable to execute query.";
  85.                     die("Unable to execute query: ".pg_last_error($connection));
  86.                        
  87.         }
  88.         //$result = pg_execute($connection, $_POST['search_by'], array($_POST['textbox']."%"));
  89.             }
  90.    
  91.         function to_table($result)
  92.         {
  93.             //Print the table headers
  94.             $row = pg_fetch_assoc($result);
  95.             $counter = 0;
  96.             $row_counter = 0;
  97.        
  98.             if(!$row)
  99.             {
  100.                 return FALSE;
  101.             }
  102.        
  103.             echo "<tr>";
  104.        
  105.             foreach($row as $key => $value)
  106.             {
  107.                 echo "<th>$key</th>";
  108.                 $row_counter++;
  109.             }
  110.        
  111.             echo "</tr>";
  112.        
  113.             //Now print the data from the first row- otherwise that data is lost
  114.        
  115.             echo "<tr>";
  116.        
  117.             foreach($row as $res)
  118.             {
  119.                 echo "<td>$res</td>";
  120.                 $counter++;
  121.             }
  122.        
  123.             echo "</tr>";
  124.        
  125.             while($row = pg_fetch_assoc($result))
  126.             {
  127.                 echo "<tr>";
  128.            
  129.                 foreach($row as $res)
  130.                 {
  131.                     echo "<td>$res</td>";
  132.                     $counter++;
  133.                 }
  134.            
  135.                 echo "</tr>";
  136.             }
  137.        
  138.             echo "</br>";
  139.        
  140.             $rows = $counter/$row_counter;
  141.        
  142.             echo "There were "." <i>$rows</i> "." rows returned.";
  143.        
  144.             echo "</br>";
  145.                    
  146.             }
  147. //test            
  148.         ?>
  149.         </table>
  150.     </body>
  151. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement