All pastes #2118316 Raw Edit

Unnamed

public text v1 · immutable
#2118316 ·published 2012-02-17 03:21 UTC
rendered paste body
<html>

<head>
  <title>Module Database</title>
  <style>
     .spaceAfter { margin-bottom:20px }
  .bold { font-weight:bold }
  </style>  
  
</head>

<body>

<?php
  include ("dbConnect.php");

  // set default values for heading and query.
  // If no special request has been made for a subset of the data, 
  // then these values will be used later in the code.
  // Otherwise, these values will be overwritten as the request is processed
  // Note how "select * from p3_modules" returns all fields in the row
  $heading="<h1>All Modules</h1>";
  $dbQuery="select * from p3_modules";
  
  // now check for any subset codes passed in the $_GET or $_POST arrays

  // first check if we want compulsory modules only
  if (isset($_GET["status"]) && $_GET["status"]=="C") {
     $heading="<h1>Compulsory Modules</h1>";  
     $dbQuery="select * from p3_modules where status='C'";
  }
  
  // add code here to check for optional modules only (2a)
  if (isset($_GET["status"]) && $_GET["status"]=="O") {
     $heading="<h1>Optional Modules</h1>";  
     $dbQuery="select * from p3_modules where status='O'";
  }
  
  // add code here to check for only Year 1, Year 2 or Year 4 modules (2b)
  // use the LIKE operator in the query to determine the year of the module
  // in the LIKE comparision, use the underscore '_' as a single character wildcard
  // The 4th character of the module code determines the year
  // xxx1nn for first year (e.g. COM102)
  // xxx2nn for second year (e.g. ELE258)
  // xxx4nn for final year (e.g. MAT425)
//  if (isset($_GET["year"]) && $_GET["moduleCode"]="xxx1nn") {
  if (isset($_GET["year"]) && $_GET["year"]=="1") {
 $heading="<h1>Year 1 Modules</h1>";
 $dbQuery="select * from p3_modules where moduleCode='___1__'";
 } 
 
//  if (isset($_GET["year"]) && $_GET["moduleCode"]="___2__") {
  if (isset($_GET["year"]) && $_GET["year"]=="2") {
 $heading="<h1>Year 2 Modules</h1>";
// $dbQuery="select * from p3_modules where moduleCode='xxx2nn'";
 $dbQuery="select * from p3_modules where moduleCode='___2__'";
 }
  
//  if (isset($_GET["year"]) && $_GET["moduleCode"]="___4__") {
  if (isset($_GET["year"]) && $_GET["year"]=="4") {
 $heading="<h1>Year 4 Modules</h1>";
 $dbQuery="select * from p3_modules where moduleCode='___4__'";
 }
  
  
  // code to check for computing modules
  if(isset($_POST["subject"]) && $_POST["subject"]=="COM") {
     $heading="<h1>Computing Modules</h1>";
  $dbQuery="select * from p3_modules where moduleCode like 'COM%'";
  }
  
  // add code here to check for electronics modules only (2c)
    if(isset($_POST["subject"]) && $_POST["subject"]=="ELE") {
     $heading="<h1>Electronics Modules</h1>";
  $dbQuery="select * from p3_modules where moduleCode like 'ELE%'";
  }
  
  // add code here to check for mathematics modules only (2c)
  if(isset($_POST["subject"]) && $_POST["subject"]=="MAT") {
     $heading="<h1>CMathematics Modules</h1>";
  $dbQuery="select * from p3_modules where moduleCode like 'MAT%'";
  }
  
  // add code here to check for modules by a particular lecturer (3b)
  
  
  // now send the query
  $dbResult=mysql_query($dbQuery,$db);

  // display the heading and table of results
  echo "$heading";
  echo "<table class=\"spaceAfter\">".
       "<tr><th>Module Code</th><th>Lecturer</th><th>Status</th><th>Class Size</th></tr>";
  while ($dbRow=mysql_fetch_array($dbResult)) {
     $theModuleCode=$dbRow["moduleCode"];
  $theLecturer=$dbRow["lecturer"];
  $theStatus=$dbRow["status"];
  $theClassSize=$dbRow["classSize"];
     echo "<tr><td>$theModuleCode</td><td>$theLecturer</td>".
       "    <td>$theStatus</td><td>$theClassSize</td></tr>";
  }
  echo "</table>";
?>

<div class="spaceAfter">
  <a href="showModules.php">Show all modules</a><br>
</div>

<div class="spaceAfter">
  <a href="showModules.php?status=C">Show compulsory modules only</a> |
  <a href="showModules.php?status=O">Show optional modules only</a><br>
</div>  

<div class="spaceAfter">
  <a href="showModules.php?year=1">Show Year 1 modules only</a> |
  <a href="showModules.php?year=2">Show Year 2 modules only</a> |
  <a href="showModules.php?year=4">Show Year 4 modules only</a><br>
</div>

<div>
<span class="bold">Choose a subject:</span><br>
<form method="post" action="showModules.php">
  <input type="radio" name="subject" value="COM" checked>Computing
  <input type="radio" name="subject" value="ELE">Electronics
  <input type="radio" name="subject" value="MAT">Mathematics
  <input type="submit" value="Choose">
</form>
</div>   

<div>
<span class="bold">Choose a lecturer:</span><br>
<?php
   // note how the keyword "distinct" in the following query prevents duplicate values from being returned
   $dbQuery="select distinct lecturer from p3_modules";
   $dbResult=mysql_query($dbQuery,$db);
   
   // convert this loop to generate a drop down list (3a)
   // the value of each <option> element should be the lecturer's name
   // enclose the drop down list in a form with action=showModules.php 
   echo "<form action='showModules.php' method='post'>";
   echo "<select>";
   while ($dbRow=mysql_fetch_array($dbResult)) {
      $thisLecturer=$dbRow["lecturer"];
      echo "<option>$thisLecturer</option><br>";
   }
   echo "</select>";
   // add a submit button for the form
   echo "<input type='submit'value='submit'>";
   echo "</form>";
   
   
?>
</div>   

</body>

</html>