Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- ERROR_REPORTING(E_ALL);
- ini_set("display_errors", 1);
- ?>
- <! DOCTYPE html>
- <html>
- <head>
- <meta charset = UTF-8>
- <title>Aaron McRuer CS3380 Lab 3</title>
- </head>
- <body>
- <form method = "POST" action = "/~asm3kf/cs3380/lab3/lab3.php">
- Search for a:
- <input type = "radio" name = "search_by" value = "country" />Country
- <input type="radio" name="search_by" value="city"/>City
- <input type="radio" name="search_by" value="language"/>Language
- </br>
- </br>
- That begins with:
- <input type="text" name="query_string" value=" "/>
- </br>
- </br>
- <input type= "submit" name="submit" value="Submit"/>
- </form>
- <table border="1">
- <?php
- if(isset($_POST['submit']))
- {
- //post if
- echo "hello";
- include 'connect.php';
- $result;
- $cityQuery = "SELECT *
- FROM lab2.city
- WHERE name
- ILIKE $1
- ORDER BY name";
- $langQuery = "SELECT *
- FROM lab2.name
- WHERE name
- ILIKE $1
- ORDER BY name";
- $counQuery = "SELECT *
- FROM lab2.country
- WHERE name
- ILIKE $1
- ORDER BY name";
- $connection = pg_connect("host=dbhost-pgsql.cs.missouri.edu user=asm3kf password=fpZaYn3U dbname=asm3kf");
- if(!$connection)
- {
- echo "unable to connect to database";
- die("Failed to connect to database.");
- }
- //select query based on user input
- else
- {
- switch ($_POST['search_by'])
- {
- case "city":
- $statement = pg_prepare($connection, "city", $cityQuery);
- $result = pg_execute($connection, "city", array($_POST['query_string']."%"));
- echo "city";
- to_table($result);
- break;
- case "country":
- $statement = pg_prepare($connection, "country", $counQuery);
- $result = pg_execute($connection, "country", array($_POST['query_string']."%"));
- echo "country";
- to_table($result);
- break;
- case "language":
- $statement = pg_prepare($connection, "language", $langQuery);
- $result = pg_execute($connection, "language", array($_POST['query_string']."%"));
- echo "language";
- to_table($result);
- }
- }
- if(!$result)
- {
- echo "Unable to execute query.";
- die("Unable to execute query: ".pg_last_error($connection));
- }
- //$result = pg_execute($connection, $_POST['search_by'], array($_POST['textbox']."%"));
- }
- function to_table($result)
- {
- //Print the table headers
- $row = pg_fetch_assoc($result);
- $counter = 0;
- $row_counter = 0;
- if(!$row)
- {
- return FALSE;
- }
- echo "<tr>";
- foreach($row as $key => $value)
- {
- echo "<th>$key</th>";
- $row_counter++;
- }
- echo "</tr>";
- //Now print the data from the first row- otherwise that data is lost
- echo "<tr>";
- foreach($row as $res)
- {
- echo "<td>$res</td>";
- $counter++;
- }
- echo "</tr>";
- while($row = pg_fetch_assoc($result))
- {
- echo "<tr>";
- foreach($row as $res)
- {
- echo "<td>$res</td>";
- $counter++;
- }
- echo "</tr>";
- }
- echo "</br>";
- $rows = $counter/$row_counter;
- echo "There were "." <i>$rows</i> "." rows returned.";
- echo "</br>";
- }
- //test
- ?>
- </table>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement