<html>
<head>

<? include("header.txt"); ?>
<meta http-equiv="Content-Type" content="text/html; charset=tis-620">
<body leftmargin="0" topmargin="0">
<? include("table_digitlib.htm"); ?>
<table width="100%" border="0" align="center" cellspacing="0">
  <tr> 
    <td valign="top"><p align="center">&nbsp;</p>
      <p align="center"><font color="#3A75AF"><strong>PSU e-Thesis Database</strong></font></p>
      <p align="center">

<?

include("connectdb.php");

if (!($limit)){
     $limit = 10;} // Default results per-page.

if (!($page)){
     $page = 0;} // Default page value.


$i=0;
/*
if (!($_POST["case"]))
	$case = $_POST["case"]; 
if (!($_GET["case"]))
	$case = $_GET["case"]; 
*/


//echo "case=".$case;
//$query=$_GET["$query"];
$query =str_replace( "\\","",$query); 
//echo "query1...=".$query."<br>";
if (!($query)){

switch ( $case )
{
    case 1:
    {
	/*
        $keyword = $_POST["keyword"];
        $select_from = $_POST["select_from"];
		$andor = $_POST["andor"];
		$select_major = $_POST["select_major"];
*/
			$length = strlen($keyword);
			if ($length==0) { 
			 	echo " No Keyword";
			}
			else 
			{
				$exp = explode(" ", $keyword); 
				echo "ค้นหาจาก: <b>". $keyword."</b> ตาม ".$select_from ." <br>";	
//				echo "major=".$select_major;
				$i = 0;
				$condition ="";				
			
		//		echo ("count exp= ".count($exp));
					while ($i < count($exp)) { 
						if ($i==0) {
							$condition = " (".$select_from."  LIKE '%".$exp[$i]."%') ";   
						} else { 
					
//						if ($i < count($exp)-1) { 
							$condition = $condition." ".$andor." (".$select_from."  LIKE '%".$exp[$i]."%') ";   
						}
						$i++; 
					} 
				$query  = "  SELECT  bibno, title, pubyear, author, majorid, linkfull, linkabs  FROM  thesis  WHERE ".$condition." ORDER BY pubyear, title " ;
//				echo "query=".$query;

		}
    }break;
	case 2:  // เลือกปี
	{
	//	$select_year = $_POST["select_year"];

				echo "แสดงรายชื่อวิทยานิพนธ์ในปี <b>". $select_year."</b> <br>";	
				$i = 0;

				$query  = "  SELECT  bibno, title, pubyear, author, majorid, linkfull, linkabs  FROM  thesis  WHERE pubyear = $select_year ORDER BY title " ;
//				echo "query=".$query;
	
	}break;
	
    default:    //	เลือกสาขาวิชา
    {
		echo "แสดงรายชื่อวิทยานิพนธ์ตามสาขาวิชา: <b>". $majorname."</b> <br> ";
		$query = "  SELECT  bibno, title, pubyear, author, majorid, linkfull, linkabs  FROM  thesis  WHERE  majorid = '$majorid' ORDER BY pubyear DESC, title ASC  ";
// $query = 'SELECT  bibno, title, pubyear, author, majorid, linkfull, linkabs  FROM  thesis  WHERE  majorid ="'.$majorid.'" ORDER BY pubyear DESC, title ASC';

    }break;
}
}
//get all result
//echo "query...=".$query;
//echo "<BR>";
$result = mysql_query($query);

//---------CODE for SPLIT PAGE------------------------------------
//-----------------------------math ------------------------------------------
$numrows= mysql_num_rows($result);
if ($numrows == 0){
     echo("No results found."); // bah, modify the "Not Found" error for your needs.
     exit();}



$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows % $limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) || ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.
 
else{
$last = $numrows;} // If last results page, last result equals total number of results.
//------------------------------------------------------------------------------
//escape from PHP mode.


echo "  พบจำนวน <b> ".mysql_num_rows($result). " </b>รายการ <br>";
$query1 = $query." LIMIT $page, $limit";
//echo "query...=".$query;

$result = mysql_query($query1);
?>

<table width="100%" border="0">
 <tr>
  <td width="50%" align="left">
Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>
  </td>
  <td width="50%" align="right">
Page <b><?=$current?></b> of <b><?=$total?></b>
  </td>
 </tr>
 <tr>
  <td colspan="2" align="right">&nbsp;

  </td>
 </tr>
 <tr>
  <td colspan="2" align="right">
<? //echo "query...=".$query."<br>"; ?>

Results per-page: <a href="<?=$PHP_SELF?>?case=$case&query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a>



  </td>
 </tr>
</table>

<?


echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" bgcolor=\"#CCCCCC\">";
echo "    <tr  bgcolor=\"#E2C5C5\"> ";
echo "  <td width=\"5%\"><div align=\"center\">No.</font></div></td>";
echo "   <td width=\"5%\"><div align=\"center\">Year</font></div></td>";
echo "   <td width=\"65%\"><div align=\"center\">Title</font></div></td>";
echo "   <td width=\"15%\"><div align=\"center\">Author</font></div></td>";
echo "   <td width=\"10\"><div align=\"center\">Links</div></td>";
echo "  </tr>";
$i=$first;

while ($row = mysql_fetch_array($result))
{

        echo "<tr valign=\"top\" height=25 bgcolor=\"#FFFFFF\"> ";
        echo "  <td width=\"5%\"><div align=\"center\"> $i. </div></td>";
	    echo "  <td width=\"5%\"><div align=\"center\">$row[2] </a>";   //year
       echo "   <td width=\"65%\">$row[1] <br>&nbsp;</td>";   //title
	   echo "  <td width=\"15%\">$row[3] </a>";  //author
			echo "  <td width=\"10%\"> ";
	     if ($row[5] <> "") {
			echo "  <a href=\"clickme2/out.php?url=$row[5]\"><img src=\"icon_full.gif\" border=0> Fulltext</a><br> ";
		}
	     if ($row[6] <> "") {
			echo "  <a href=\"clickme2/out.php?url=$row[6]\"><img src=\"icon_abs.gif\" border=0> Abstract</a>";
		} 
			echo " </td> ";
		echo "</tr>";
		        $i = $i+1;
}
echo "</table>";

?>
<p align="center">
<?
if ($page != 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?case=$case&query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
 $ppage = $limit*($i - 1);
 if ($ppage == $page){
 echo("<b>$i</b>\n");} // If current page don't give link, just text.
 else{
 echo("<a href=\"$PHP_SELF?case=$case&query=$query&page=$ppage&limit=$limit\">$i</a> \n");}

//echo "query...=".$query."<br>";
}

if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("    <a href=\"$PHP_SELF?case=$case&query=$query&page=$next_page&limit=$limit\">next</a>");}

mysql_close();
?>


</p>

<p align="center"><a href="index.php">กลับหน้าค้นหา</a></p>
      <p>&nbsp;</p></td>
  </tr>
  <tr> 
    <td valign="top" align="center"><? include("footer.txt"); ?></td>
  </tr>
</table>

</body>
</html>
