<?php include('up_index.php');?>
  <?php include('admin.php');?>
  <?php
	if($_GET['id_category']!=""||$_GET['name_product']!=""){
		if($_GET['name_product']!=""&&$_GET['id_category']!=""){
			$chkcategory="and p.id_category ='".$_GET['id_category']."' and name_product like '%".$_GET['name_product']."%'";
		}else if($_GET['name_product']!=""){
			$chkcategory="and name_product like '%".$_GET['name_product']."%'";
		}else{
			$chkcategory="and p.id_category ='".$_GET['id_category']."'";
		}
	}else{
		
		$chkcategory=""; 
	}

?><p>

</p>
<center>
  <br/>
	<script src="show.js"></script>
	 <div class="table_product_show">
	 <br/>

    <table width="200" border="0">
  <tr>
	<td><a href="menu_admin.php"><div class="link3">จัดการระบบ</div></a></td>
    <td><a class="alink" href="add_product.php"><div class="link3">เพิ่มสินค้า</div></a></td>
  </tr>
</table>
    <br/>
	 <form action="#" method="get">
<table width="520" border="0" cellpadding="1" cellspacing="1" >
   <tr>
    <td width="513">ประเภทสินค้า :
      <select name="id_category" id="id_category" >
        <option value="" <?php if($_GET['id_category']==""){echo"selected";}?>>ทั้งหมด</option>
        <?php
			$sql2 = "select * from category";
			$result2 = mysql_query($sql2);
			while ($category = mysql_fetch_array($result2))
			{	?>
        <option value="<?=$category['id_category'];?>"<?php if($category['id_category']==$_GET['id_category']){echo"selected";}?>><?=$category['name_category'];?></option>
        <?php	}	?>
      </select> 
      คำค้นหา
      <input name="name_product" type="text" id="name_product" value="<?=$_GET['name_product']?>" maxlength="100" /> <input type="submit" name="button" id="button" value="ค้นหา" /></td>
    </tr>
</table></form>
	 <br/>
	<table width="800" border="1" cellpadding="0" cellspacing="0">
  <tr>
    <td width="193" align="center">ชื่อสินค้า</td>
    <td width="165" align="center">ประเภทสินค้า</td>
    <td width="163" align="center">ราคา</td>
    <td width="105" align="center">จัดการ</td>
  </tr>
<?php

	$strSQL = "SELECT * FROM product p,category c WHERE c.id_category = p.id_category ".$chkcategory."";
	$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
	$Num_Rows = mysql_num_rows($objQuery);
	
	$Per_Page = 20;   // Per Page
	
	$Page = $_GET["Page"];
	if(!$_GET["Page"])
	{
		$Page=1;
	}
	
	$Prev_Page = $Page-1;
	$Next_Page = $Page+1;
	
	$Page_Start = (($Per_Page*$Page)-$Per_Page);
	if($Num_Rows<=$Per_Page)
	{
		$Num_Pages =1;
	}
	else if(($Num_Rows % $Per_Page)==0)
	{
		$Num_Pages =($Num_Rows/$Per_Page) ;
	}
	else
	{
		$Num_Pages =($Num_Rows/$Per_Page)+1;
		$Num_Pages = (int)$Num_Pages;
	}
	
	$strSQL .=" ORDER BY p.id_category LIMIT $Page_Start , $Per_Page";
	$objQuery  = mysql_query($strSQL);

	while ($objResult  = mysql_fetch_array($objQuery))
	{
		echo "<tr>
			<td>".substr($objResult ['name_product'],0,30)."...</td>
			<td align=\"center\">".$objResult ['name_category']."</td>
			<td align=\"center\">".$objResult ['price']."</td>
			<td align=\"center\"><a href=\"update_product.php?id_product=".$objResult ['id_product']."\"><img src=\"images/refresh.png\" width=\"30\"  /> </a><a href=\"save_delete_product.php?id_product=".$objResult ['id_product']."\"onclick=\"return confirm('ยืนยันการลบข้อมูล?')\"><img src=\"images/delete.png\" width=\"30\"  /></a></td>
		</tr>";
		
	}	
	?>
  </table>
	<br/>
	<?php
	if($Prev_Page)
{
	echo " <a class='alink' href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&id_category=".$_GET['id_category']."&name_product=".$_GET['name_product']."'><< Back</a> ";
}

for($i=1; $i<=$Num_Pages; $i++){
	if($i != $Page)
	{
		echo "[ <a class='alink' href='$_SERVER[SCRIPT_NAME]?Page=$i&id_category=".$_GET['id_category']."&name_product=".$_GET['name_product']."'>$i</a> ]";
	}
	else
	{
		echo "<b> $i </b>";
	}
}
if($Page!=$Num_Pages)
{
	echo " <a class='alink' href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&id_category=".$_GET['id_category']."&name_product=".$_GET['name_product']."'>Next>></a> ";
}

?>
</div>
</center>

<?php include('down_index.php'); ?>