Advertisement
Guest User

DB Assignment 6

a guest
Oct 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.07 KB | None | 0 0
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>SQL Display</title>
  6. </head>
  7. <body> 
  8. <div style="text-align:center">
  9. <h1>Display SQL Table</h1>
  10.  
  11. <?php
  12.  
  13.     $servername = "localhost";
  14.     $username = "webapp17Us3r";
  15.     $password = "5DfJa2aduPkQtmEK";
  16.     $database = "playground17";
  17.     $table = "jma_information_form";
  18.    
  19.     // Create connection
  20.     $conn = new mysqli($servername, $username, $password, $database);
  21.    
  22.     // Check connection
  23.     if ($conn->connect_error) {
  24.         die("Connection failed: " . $conn->connect_error);
  25.     }
  26.     echo "Connected successfully to ". $database."<br><br>";
  27.     echo "Using table " . $table ."<br><br>";
  28.  
  29.     $headerString = "Showing ";
  30.    
  31.     if (empty($_REQUEST) || isset($_REQUEST['all'])) {
  32.        
  33.         $headerString .= "all entries";
  34.        
  35.         $sql = "* FROM " . $table; 
  36.         $countSql = "COUNT(*) FROM " . $table;
  37.            
  38.     } else {
  39.        
  40.        
  41.         if (isset($_REQUEST['child'])) {
  42.             $headerString .= " child entries";
  43.             $sql = "* FROM " . $table . " WHERE age < 18";
  44.             $countSql = "COUNT(*) FROM " . $table . " WHERE age < 18";
  45.         } elseif (isset($_REQUEST['adult'])) {
  46.             $headerString .= " adult entries";
  47.             $sql = "* FROM " . $table . " WHERE age >= 18";
  48.             $countSql = "COUNT(*) FROM " . $table . " WHERE age >= 18";
  49.         } elseif (isset($_REQUEST['organizations'])) {
  50.             $headerString .= " organizations";
  51.             $sql = "organization FROM " . $table;
  52.             $countSql = "COUNT(organization) FROM " . $table;
  53.             $countDistinctSql = "COUNT(DISTINCT organization) FROM " . $table;
  54.         } elseif (isset($_REQUEST['occupations'])) {
  55.             $headerString .= " occupations";
  56.             $sql = "occupation FROM " . $table;
  57.             $countSql = "COUNT(occupation) FROM " . $table;
  58.             $countDistinctSql = "COUNT(DISTINCT occupation) FROM " . $table;
  59.         } else {
  60.             // do nothing
  61.         }  
  62.        
  63.     }
  64.    
  65.     if (isset($_REQUEST['distinct'])) {
  66.         $headerString .= " (distinct)";
  67.         if (isset($countDistinctSql)) {
  68.             $data = $conn->query('SELECT ' . $countDistinctSql);
  69.         } else {
  70.             $data = $conn->query('SELECT ' . $countSql);
  71.         }
  72.         $sql = 'DISTINCT ' . $sql;
  73.     } else {
  74.         $data = $conn->query('SELECT '. $countSql);
  75.     }
  76.    
  77.     echo "<h2>".$headerString."</h2>";
  78.    
  79.     if ($data->num_rows > 0) {
  80.         while($row = $data->fetch_assoc()) {
  81.             echo "<h3>" . $row[array_keys($row)[0]] . " results</h3>";
  82.         }
  83.     }
  84.    
  85.     $data = $conn->query('SELECT '. $sql);
  86.     echo displayTable($data).'<br>';
  87.  
  88.     function displayTable($table) {
  89.        
  90.         $returnString = '<table border="1" style="text-align: left; margin-left: auto; margin-right: auto">';
  91.        
  92.         $lables = NULL;
  93.        
  94.         if ($table->num_rows > 0) {
  95.             // output data of each row
  96.            
  97.            
  98.             while($row = $table->fetch_assoc()) {
  99.                
  100.                 if (!isset($labels)) {
  101.                    
  102.                     $returnString .= "<tr>";
  103.                    
  104.                     $labels = array_keys($row);
  105.                    
  106.                     for ($i = 0; $i < count($labels); $i++) {
  107.                         $returnString .= "<th>" . $labels[$i] . "</th>";
  108.                     }
  109.                    
  110.                     $returnString .= "</tr>";
  111.                    
  112.                    
  113.                 }
  114.                 $returnString .= "<tr>";
  115.                    
  116.                 for ($i = 0; $i < count($row); $i++) {
  117.                     $returnString .= "<td>" . $row[$labels[$i]] . "</td>";
  118.                 }
  119.                
  120.                
  121.                
  122.                 $returnString .= "</tr>";
  123.                
  124.             }
  125.             return $returnString .= "</table>";
  126.    
  127.         } else {
  128.             return NULL;
  129.         }
  130.     }
  131.        
  132.    
  133.    
  134.     function showLastNameSearch() {
  135.         // implement name search maybe in future
  136.     }
  137.    
  138.    
  139. ?>
  140.  
  141. <br><form action="showTable.php" method="post">
  142.        
  143.         <input type="checkbox" name="distinct" value="checked"
  144.        <?php
  145.             // Makes sure distinct checkbox stays checkd
  146.                
  147.             if(isset($_REQUEST['distinct'])) {
  148.                 echo 'checked="checked"';
  149.             }
  150.         ?>
  151.         > Distinct Values<br>
  152.         <input type="submit" name="all" value="Show All">
  153.         <input type="submit" name="child" value="Show Children Only">
  154.         <input type="submit" name="adult" value="Show Adults Only">
  155.         <input type="submit" name="organizations" value="Show Organizations Only">
  156.         <input type="submit" name="occupations" value="Show Occupations Only">
  157.                    
  158.                
  159. </form>
  160. <br>
  161. PS: the checkbox stays checked, even when you submit info (it remembers)
  162.  
  163.  
  164. </div>
  165. </body>
  166. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement