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

if ( isset($_POST['isbn']) &&  isset($_SESSION['userid']) )
{
echo "came from search page and username is set : ".$_SESSION['userid']."<br>";	
$temp = $_SESSION['userid'];
session_unset();
$_SESSION['userid'] = $temp;
$_SESSION['previous'] = "search_display";

$isbn = $_POST['isbn'];
$title = $_POST['title'];
$lang = $_POST['lang'];
$publisher = $_POST['publisher'];
$author = $_POST['author'];
$libref = $_POST['libref'];
$option = $_POST['option'];
}
else if ( isset($_SESSION['userid']) )
{
echo "came from login page and username is set : ".$_SESSION['userid']."<br>";	
$isbn = $_SESSION['isbn'];
$title = $_SESSION['title'];
$lang = $_SESSION['lang'];
$publisher = $_SESSION['publisher'];
$author = $_SESSION['author'];
$libref = $_SESSION['libref'];
$option = $_SESSION['option'];

$temp = $_SESSION['userid'];
session_unset();
$_SESSION['userid'] = $temp;
$_SESSION['previous'] = "search_display";
}
else
if( isset($_POST['isbn'])  )
{
echo "came from some form and username is not set <br>";
$_SESSION['isbn'] = addslashes($_POST['isbn']);
$_SESSION['title'] = addslashes($_POST['title']);
$_SESSION['lang'] = addslashes($_POST['language']);
$_SESSION['publisher'] = addslashes($_POST['publisher']);
$_SESSION['author'] = addslashes($_POST['author']);
$_SESSION['libref'] = addslashes($_POST['libref']);
$_SESSION['option'] = $_POST['RadioGroup1'];

$isbn = $_SESSION['isbn'];
$title = $_SESSION['title'];
$lang = $_SESSION['lang'];
$publisher = $_SESSION['publisher'];
$author = $_SESSION['author'];
$libref = $_SESSION['libref'];
$option = $_SESSION['option'];
}
else
{
echo "came from older search results and username is not set <br>";
$isbn = $_SESSION['isbn'];
$title = $_SESSION['title'];
$lang = $_SESSION['lang'];
$publisher = $_SESSION['publisher'];
$author = $_SESSION['author'];
$libref = $_SESSION['libref'];
$option = $_SESSION['option'];
}

?>

<!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>

<script type="text/javascript">
function validate(form)
{
form.submit()
}

function re(form1)
{ 
  if(confirm("Do you want to reset the form"))
  {
  }   
}
</script>


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


//  Library reference Results
if ( $option!="Buy" )
{
echo "<h2> Books available in Library </h2> <br>";	
$query = "SELECT
		  BOOK.ISBN as isbn, BOOK.TITLE as title, BOOK.LANG as lang, BOOK.PUBLISHER as publisher,
		  LIBRARY.REF_NUM as refnum 
		  from BOOK, LIBRARY 
		  WHERE BOOK.ISBN=LIBRARY.ISBN";

if ($isbn!="")
	$query=$query.sprintf(" AND BOOK.ISBN='%s'",$isbn);
if ($title!="")
	$query=$query.sprintf(" AND BOOK.TITLE like '%%%s%%'",$title);
if ($lang!="")
	$query=$query.sprintf(" AND BOOK.LANG='%s'",$lang);
if ($publisher!="")
	$query=$query.sprintf(" AND BOOK.PUBLISHER like'%%%s%%'",$publisher);
	
$query=$query.';';
echo $query.'<br>';
				  
$result = mysql_query($query);
echo mysql_num_rows($result);

  echo "<table>
	<tr>
	<th>ISBN</th>
	<th>TITLE</th>
	<th>AUTHORS</th>
	<th>LANGUAGE</th>
	<th>PUBLISHER</th>
	<th>REF NUMBER</th>
	<th></th>
	</tr>";

$i = 1;
while($row = mysql_fetch_array($result))
  {
	  
		  // displaying all authors for each book
		  $query = "SELECT AUTHOR.NAME as authname from BOOK, AUTHOR, WRITTEN
		  WHERE WRITTEN.ISBN=BOOK.ISBN AND WRITTEN.AUTH_ID=AUTHOR.AUTH_ID";
		  $query=$query.sprintf(" AND BOOK.ISBN='%s'",$row['isbn']);
		  $query=$query.';';
		  echo $query.'<br>';
						  
	      $auths = mysql_query($query);
	      echo mysql_num_rows($auths);
		  
  echo sprintf('<form id="form%s" name="form%s" method="post" action="">',$i,$i); 
  echo "<tr>";
  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 '<td> <input type="textarea" name="isbn" id="isbn" readonly="readonly" value=" ';
  $authnames = mysql_fetch_array($auths);
  echo $authnames['authname'];
  while($authnames = mysql_fetch_array($auths))
	{
		echo ','.$authnames['authname'];
	}
  echo "ashish";	
  echo '" ></input></td>'; 
  
  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="publisher" id="publisher" readonly="readonly" value="%s"></input> </td>',$row['publisher']);
  echo sprintf('<td> <input type="text" name="refnum" id="refnum" readonly="readonly" value="%s"></input> </td>',$row['refnum']);
  echo "</tr>";
  echo "</form>";
  $i=$i+1;
  }
 echo "</table>"; 
} 
 
echo "<br><br><h2> Books available for purchase </h2> <br>";
 

// Displaying selling options for the same book
$query = "SELECT distinct
		  BOOK.ISBN as isbn, BOOK.TITLE as title, BOOK.LANG as lang, BOOK.PUBLISHER as publisher
		  from BOOK, SELLER
		  WHERE BOOK.ISBN=SELLER.ISBN
		  AND SELLER.NUM_COPIES>0 ";

if ($isbn!="")
	$query=$query.sprintf(" AND BOOK.ISBN='%s'",$isbn);
if ($title!="")
	$query=$query.sprintf(" AND BOOK.TITLE like '%%%s%%'",$title);
if ($lang!="")
	$query=$query.sprintf(" AND BOOK.LANG='%s'",$lang);
if ($publisher!="")
	$query=$query.sprintf(" AND BOOK.PUBLISHER like'%%%s%%'",$publisher);
	
$query=$query.';';
echo $query.'<br>';
				  
$result = mysql_query($query);
echo mysql_num_rows($result);

  echo "<table>
	<tr>
	<th>ISBN</th>
	<th>TITLE</th>
	<th>LANGUAGE</th>
	<th>PUBLISHER</th>
	<th></th>
	</tr>";

$i = 1;
while($row = mysql_fetch_array($result))
  {
  echo sprintf('<form id="form%s" name="form%s" method="post" action="buy.php">',$i,$i); 
  
  echo "<tr>";
  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="publisher" id="publisher" readonly="readonly" value="%s"></input> </td>',$row['publisher']);
  echo sprintf('<td> <input type="submit" name="button" id="button" value="Buy" />');
  echo "</tr>";
  echo "</form>";
  $i=$i+1;
 }
  
echo "</table>"; 
?>

</body> 
</html>
