<?
$rez=include("setup.php");
if (! $rez) //if can't find setup.php or not chmod'ed 
 {
  echo "<font color=red>Check Your setup.php file !</font>";
	exit;
 }

$dir='admin';
checkauth($dir);

include "menu.htm";

//------delete dirs-categories --------------
   if (($deleteme="delete") && isset($cremove[0]))
     {	
	$result=0;
        $i=0;  
     while($elements = each ($cremove))
     {
        $whois=$elements["value"];	
	$sql = "SELECT Photo FROM product where ID=$whois";	 //check if image exist
        $result = mysql_query($sql) or  die (mysql_error(). " : ".mysql_errno());
        $row=mysql_fetch_object($result);
        if ($row->Photo !="nothing")
          @unlink("$dir_to_upload/$row->Photo");    //delete image from image dir if name of image written in DB
        $query="delete from product where ID=$whois";
        $result=$result . mysql_query($query)or die (mysql_error(). " : ".mysql_errno());     
        $i++;
     }
	if ($result==$i)
          print "Operation succeeded<br><br>";


     }


//----------- end remove dirs-categories--------

// --------------- start sort by ---------------
echo "<table><tr><td>";
echo "<form action=$PHP_SELF method=post>Order your sort by
      <select name=orderby>
      <option value='ID'>Product ID</option>
      <option value='Name'>Product name</option>
      <option value='Price'>Product price</option>
      <option value='CategoryID'>Product's category</option>
      </select>";
echo "</td></tr>";
// categories
unset ($result);
unset($row);
       $result=mysql_query("select ID,CategoryName from category ") or die (mysql_error(). " : ".mysql_errno());
echo "<tr><td>";
echo " From category
      <select name=category>
       <option value='nothing'>--Select one--</option>";
       while($row=mysql_fetch_object($result))
       {
         echo "<option value='$row->ID'>$row->CategoryName</option>";
       }
       echo "</select>";

// warranty  company
unset ($result);
unset($row);
       $result=mysql_query("select ID,NameOfCompany from garant ") or die (mysql_error(). " : ".mysql_errno());

echo " With warranty service company
      <select name=garant>
      <option value='nothing'>--Select one--</option>";
       while($row=mysql_fetch_object($result))
       {
         echo "<option value='$row->ID'>$row->NameOfCompany</option>";
       }
       echo "</select>";

echo "</td></tr><tr><td>";
echo  "<input type=submit value=submit></form>";
echo "</td></tr></table>";
if (empty($orderby))
       $orderby="ID";
$where=""; //condition of filter
if (isset($category) && $category!="nothing")
       $where="where CategoryID=$category";
if (isset($garant) && $garant!="nothing")
{
       if (!empty($where))
        $where.=" and GarantID=$garant";
       else
        $where="where GarantID=$garant";
}

$result=mysql_query("select ID,Name,Price,CategoryID from product $where order by $orderby ") or die (mysql_error(). " : ".mysql_errno());

// ------------ end sort by -------------------------
print "<form action=$PHP_SELF method=post name=allproducts>
       <input type=hidden name=deleteme value=delete>";
print "<table border=1 cellpadding=5 cellspacing=0 width=100% bordercolor=#007cf0>";

       print "<tr><td width=10%>Remove</td><td width=50%>Name</td><td width=10%>Price</td><td width=30%>Category</td></tr><tr>";

while($row=mysql_fetch_object($result))
{
       $result1=mysql_query("select CategoryName from category where id=$row->CategoryID ") or die (mysql_error(). " : ".mysql_errno());
       $row1=mysql_fetch_object($result1);
       print "<td><input type=checkbox name=cremove[] value=$row->ID></td> ";
       print "<td><a href=\"javascript:showuserdata('one_product.php','$row->ID')\" title=\"Show about $row->Name\"><b>$row->Name</a></b></td>";
       print "<td>$row->Price</td>";
       print "<td>$row1->CategoryName</td>";
       print "</tr>";
}
print "</table>";
print "<br><input type=reset name=reset value='Reset'> ";
print "<input type=submit value=Delete onclick='return del()'></form>";

?>