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

?>
<script src="show.js"></script>
<center>
<form action="search_show.php" 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>
</center>
<br/>
<center>
<table id="product_show"  width="1200px"  border="0" style="border-collapse: separate; border-spacing: 5px;">
                    	<tr>
                                                        
                            <?php 
                          
                                $strSQL = "SELECT * FROM product ".$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 id_category LIMIT $Page_Start , $Per_Page";
                                $objQuery  = mysql_query($strSQL);
								$NoP=0;
								while($product = mysql_fetch_array($objQuery))
								{
									$NoP++;
							?>
									<td width="25%" height="180" align="center" valign="top">
                                    	<a href="product_show_full.php?id_product=<?=$product['id_product'];?>">
										<div id="product_show_css" title="<?=$product['name_product'];?>"><?php echo substr($product['name_product'],0,30)."...";?><br/>
											<img class="img" id="img_product_show"  src="upload/<?=$product['image1']?>" width="250" height="250" /><br/><?php echo "ราคา ".substr($product['price'],0,30)." บาท";?><br/>
										</div></a>
									</td>
                          
							<?		if(($NoP%4)==0){
										echo "<tr>";
									}
								}
								
								for($col=0; $col<4-$NoP; $col++)
									echo "<td></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> ";
}

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