<?php session_start(); 
$previous = $_SESSION['previous'];
$_SESSION['previous'] = "search_display";
echo $previous.'<br>';
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Results </title>

<?php
$connect = mysql_connect("localhost","root","programming");
if (!$connect)
{ die('Could not connect: '.mysql_error());  }
mysql_select_db("BOOK_DB", $connect);


$query = "SELECT COURSE_ID as cid from COURSE;";
echo $query.'<br>';
$result = mysql_query($query);

echo'
<form id="form1" name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'">

	  <br><br>
	  SELECT Course : 
	  <select name="course">';
	  
	  while( $row = mysql_fetch_array($result))
	  {
	  echo sprintf("<option value='%s'>'%s'</option>",$row['cid'],$row['cid']);
      }
      
echo  '</select> 
       <input type="submit" name="button" id="button" value="Submit" />
       </form>';
       
       
if (isset($_POST['course']))
{
	echo '<h2> Displaying Recommended Books for Course : '.$_POST['course'].'</h2>';
	$query= sprintf("SELECT BOOK.ISBN as isbn,BOOK.TITLE as title,BOOK.LANG as lang, BOOK.MRP as mrp
			 FROM RECOMMENDATION,BOOK
			 WHERE RECOMMENDATION.ISBN = BOOK.ISBN AND 
			 RECOMMENDATION.COURSE_ID='%s';", $_POST['course']);
	
	echo $query.'<br>';
	$result = mysql_query($query);
	
	echo "<table>
	<tr>
	<th></th>
	<th></th>
	<th></th>
	<th>ISBN</th>
	<th>TITLE</th>
	<th>LANGUAGE</th>
	<th>MRP</th>
	<th></th>
	</tr>";

	$i = 1;
	while($row = mysql_fetch_array($result))
	  {
	  echo sprintf('<form id="form%s" name="form%s" method="post" action="search_display.php">',$i,$i); 
	  
	  echo "<tr>";
	  echo sprintf('<td> <input type="hidden" name="publisher" id="publisher" readonly="readonly" value="" ></input> </td>');
	  echo sprintf('<td> <input type="hidden" name="author" id="author" readonly="readonly" value="" ></input> </td>');
	  echo sprintf('<td> <input type="hidden" name="libref" id="libref" readonly="readonly" value="" ></input> </td>');
	  echo sprintf('<td> <input type="text" name="isbn" id="isbn" readonly="readonly" value="%s" ></input> </td>',$row['isbn']);
	  echo sprintf('<td> <input type="text" name="title" id="title" readonly="readonly" value="%s" ></input> </td>',$row['title']);
	  echo sprintf('<td> <input type="text" name="lang" id="lang" readonly="readonly" value="%s" ></input> </td>',$row['lang']);
	  echo sprintf('<td> <input type="text" name="mrp" id="mrp" readonly="readonly" value="%s" ></input> </td>',$row['mrp']);
	  echo sprintf('<td> <input type="submit" name="button" id="button" value="Search/Buy" />');
	  echo "</tr>";
	  echo "</form>";
	  $i=$i+1;
	 }
	  
	echo "</table>"; 
}
?>

</body> 
</html>
