<!--
    Brian Platz
    CS85 Section 1643
    Professor Geddes
    May 31, 2012
    Assignment: Test 4
        part 5. Write the HTML, PHP, and SQL to do the following:

            Display a Web form with a field for the user to enter a class section number.
            When the form is submitted, a server-side script should access the classes database 
                and return the following data to the browser:
                    course number
                    title
                    instructor name
                    
            If the class section number does not exist in the database, the message "Section not found." 
                should be displayed by the browser.
            Hint: You may find the following PHP function useful: mysql_num_rows — Get number of rows in result
                    http://us2.php.net/manual/en/function.mysql-num-rows.php
    --> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
      <title></title>
      <style type="text/css">
         body {background-color:#B0E0E6}
         h1 {color:black; font-family:Ariel; font-size:14pt; 
                 font-weight:bold; text-align: center}
         h2 {color:red; font-family:Ariel; font-size:18pt; font-weight:bold; text-align: center}
         h3 {color:black; font-family:Ariel; font-size:24pt; font-weight:normal}
         .center {margin-left: auto;
                    margin-right: auto;
                    width: 25em;}
         table {border: 1px solid black;}
         th {text-align: center; border: 1px solid black;}
         td {text-align: center; border: 1px solid black;}
      </style>    
   </head>
   <body> 
      <?php
          extract
($_REQUEST);                         // create form variables
          
error_reporting(E_ALL & ~E_NOTICE);         // get rid of runtime notices
          
          // base logic
          
if ( !isset( $_REQUEST['section'] ) ) {            // first time through (no section entered)
              
drawForm();                                            // draw form
          
} else {                                            // section number present
              
$section $_REQUEST['section'];
              
              
$queryResult runQuery($section);                // get info on section number
              
if ( mysql_num_rows($queryResult) ) {                // if query returned results
                  
print drawResults($queryResult);                    // print them out
                  
$section null;                                    // reset section number
                  
$_REQUEST['section'] = null;
                  
drawForm();                                            // redraw form
              
} else {                                            // if not, let user know that section doesn't exist
                  
print "<h2>Section not found!</h2>";                // let user know that section doesn't exist
                  
$section null;                                    // reset section number
                  
$_REQUEST['section'] = null;
                  
drawForm();                                            // redraw form
              
}
          }
         
          function 
drawForm() {                    // function to draw form
              
ECHO <<< HERE
                <h1>FIND MY SECTION...<br/><br/>
                <form class='center' id='myTest' name='myTest' method='post' > 
                   <label>Enter Section #: <input type='text' name='section' id='section' size='4' maxlength='4'/></label><br/> 
                   <button type='submit' value='Submit'>Submit</button> 
                </form></h1><br/><br/>
HERE;
          }
          
          function 
runQuery($section) {            // function to run query. Returns data as array

              // connect to mysql using user4 user with password pass4, dbname classes
            
$conn mysql_connect"thehole.ipowermysql.com""bboy""bboy5701") or die( mysql_error() );
            
            
mysql_select_db("classes"$conn);

            
// return course number, title, instructor name
            
$sql "SELECT c.course_number, c.title, i.name
                    FROM instructors AS i INNER JOIN classes AS c ON i.instructor_id = c.instructor_id
                    WHERE c.section = '
$section' ";

            
$result mysql_query($sql$conn) or die( mysql_error() );
                
            
mysql_close();   
            return 
$result;

            }
            
            function 
drawResults($results) {    // formats table of results
                
$retString "<table class='center'><tr><th>Course Number</th><th>Course Title</th><th>Instructor</th></tr><tr>";
                while (
$row mysql_fetch_assoc($results) ) {
                     foreach (
$row as $name => $value) {
                      
$retString .= "<td>$value</td>";
                      }
                  
$retString .= "</tr></table><br/>\n";
                  }
                  return 
$retString;
            }
      
?>

<a href='../index.html#test4' >Main Page</a>
   </body>
</html>