<html>
<head><title>EZ Shopper - Edit a product</title>
<body>
<?php
/***************************************************************************
 *			products_edit.php                                  *          
 *                     	          				           *
 *   File Info:		  Products Edit/Delete                             *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/

include_once("./header.inc");

echo "<center><img src=images/searching.gif border=0 border=0></center><br><center><font face=verdana size=3 color = navy>List all  products and edit/delete product.</font></center>";
$page = getenv("QUERY_STRING");
$test = substr($str,'-');
if ($test >= 1)
	$sort = "true";
else
	$sort = "false";


if (strlen($page) > 0)
{
	$str = $page;
	$pos = strpos($str,'=');
	$poss = strpos($str,'?');
	$pos1 = strpos($str,'*');
	$pos2 = strpos($str,'-');
	if ($pos >=1 )
	{
	$page = substr($str,0,$pos);
	$search = substr($str,$poss+1,strlen($str));
	$search = str_replace("%20"," ",$search);
	}
	if ($pos1 >=1 && $pos2 ==0)
	{
	$page = substr($str,0,$pos1);
	$limit = substr($str,$poss+1,strlen($str));
	}
	if ($pos2 >= 1)
	{
		$limit = substr($str,$poss+1,$pos2-3);
	}
	if ($pos2 >= 1)
		$sort = "true";
	else
		$sort = "false";
	$pos3 = strpos($str,'>');
	if ($pos3 >= 1)
		$display = "Ascending Order";
	$pos4 = strpos($str,'<');
	if ($pos4 >= 1)
		$display = "Descending Order";
	$pos5 = substr($str,':');
	if ($pos5 >= 1)
	{
		$srt = substr($str,strlen($str)-1);
		if ($srt == "1")
		$sortorder = "Item";
		if ($srt == "2")
		$sortorder = "Description";
		if ($srt == "3")
		$sortorder = "Price";
		if ($srt == "4")
		$sortorder = "All";
	} 
}  



if ($_SERVER['REQUEST_METHOD'] == "POST") 
 {
        # escape data and set variables
        $limit= addslashes($_POST["limit"]);
        $search = addslashes($_POST["search"]);
	$sort = addslashes($_POST["sort"]);
	$sortorder = addslashes($_POST["sortorder"]);
	$display = addslashes($_POST["display"]);
}
if ($limit == NULL)
	$limit = 10;

echo "<img src=images/line.gif border=0 width=100%><br><br><font face = Verdana size = 2>Please select number of products you wish to view:- ";
$form = "<form action =products_edit.php method = POST>";
$form.= "<select name = limit><option value = 5>5 records</option><option value = 10>10 records</option><option value = 25>25 records</option><option value = 50>50 records</option></select> ";
$form.="<input type=hidden name=sort value='false'>";
$form.= "<input type=submit value = 'View Records'></font></form>";
echo $form;

echo "<font face = Verdana size = 2>Please input the product you wish to search by:-";
$form = "<form action = products_edit.php method = POST>";
$form.= "Search by Product: <input type = text name = search size = 30 maxlength = 50> ";
$form.= "<input type =submit value='Search'></font></form>";
echo $form;

$forms = "Select how you wish to sort and order records by:-";
$forms.="<form action = products_edit.php method=POST>";
$forms.="Sort By: <select name = sortorder>";
$forms.="<option>Item</option>";
$forms.="<option>Description</option>";
$forms.="<option>Price</option>";
$forms.="<option>All</option></select>";
$forms.=" View Records in:- ";
$forms.="<select name=display>";
$forms.="<option>Ascending Order</option>";
$forms.="<option>Descending Order</option></select> ";
$forms.="<input type=hidden name=sort value='true'>";
$forms.="<input type=hidden name=limit value='$limit'>";
$forms.= "<input type=submit value = 'Sort Records'></form>";
echo $forms;

########################################################
$table = "products";
$conn = @mysql_connect($server,$user,$password)
	or die("Unable to connect to SQL database...");
$rs = @mysql_select_db($database,$conn) 
	or die("Unable to select the database...Please press 'Create Database' to create new database.");

######################### display information search only ###################################

if ($search != NULL)
{
if (empty($page))
$page = 1;
$limitv = $page * $limit - ($limit);
$query_count = "SELECT * from $table where (pitem like '%$search%') order by pid";
$res_count = mysql_query($query_count,$conn);
$totalrows = mysql_num_rows($res_count);
$query = "SELECT * from $table  where (pitem like '%$search%')  order by pid LIMIT $limitv,$limit";
$result = mysql_query($query,$conn)
	or die("SQL command fail ".mysql_error());
$count_result = mysql_num_rows($result);
$numpages = ceil($totalrows / $limit);

echo "<p><center><b><font face=verdana size=2>Found $totalrows</b> result(s) for product item = '$search' - Listing $limit products per page.</font></center></p>";
$from = $limit*$page-$limit+1;
$to = $from+$count_result-1;
if ($style == 1) #style 1
{
echo "<table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr><td width=\"50%\" bgcolor=\"$backcolor\" align=\"left\">";if($numpages>1){echo"<font face=verdana size=2>Showing item(s): $from to $to</font></td><td width=\"50%\" bgcolor=\"$backcolor\" align=\"right\"><b><font face=verdana size=2>Go to page:</font></b> ";} 
}
if ($style == 2) #style 2
{
echo "<table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr><td width=\"50%\" bgcolor=\"$backcolor\" align=\"left\">";if($numpages>1){echo"<font face=verdana size=2>Showing item(s): $from to $to</font></td><td width=\"50%\" bgcolor=\"$backcolor\" align=\"right\"><b><font face=verdana size=2>Go to page:</font></b> ";} 
}
if($page != 1){ 
$pageprev = $page - 1; 
echo("<a href=\"$PHP_SELF?$pageprev=?$search\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
$oldsearch = $search;
} 

// display page nos if not 1 
for($i = 1; $i <= $numpages; $i++){ 
if($numpages>1){
if($i == $page) 
echo("&nbsp;".$i."&nbsp;"); 
else
echo("&nbsp;<a href=\".\products_edit.php?$i=?$search\">$i</a>&nbsp;"); 
}}

// display next page link if there is more than one
if(($totalrows - ($limit * $page)) > 0){ 
$pagenext = $page + 1; 
echo("<a href=\"$PHP_SELF?$pagenext=?$search\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
}
echo "</td><tr></table><br>";

######################### display part ##############################

echo"<table align=\"center\" valign=\"top\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr bgcolor=\"$backcolor\"><td>
			<table cellpadding=\"2\" border=\"0\" cellspacing=\"1\" valign=\"top\" width=\"100%\" align=\"center\">
				<tr bgcolor=\"#FFFFFF\">
					<td background=\"images/bevel_bg.gif\" height=\"20\"><b><font face=verdana size=2>Product ID</font></b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b><font face=verdana size=2>Item</font></b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b><font face=verdana size=2>Description</font></b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b><font face=verdana size=2>Price</font></b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b><font face=verdana size=2>Information</font></b></td></tr>";
					

	while($row = mysql_fetch_array($result))
	{
	$pid = $row["pid"];
	$pitem = $row["pitem"];
	$pdes = $row["pdes"];
	$price = $row["price"];
	$price = number_format($price,2);
	############ truncate product description ##########
	if (strlen($pdes) >= 30)
	{
		$pdes1 = substr($pdes,0,30);
		$pdes = $pdes1;
		$pdes.="...";
		$pitem1 = substr($pitem,0,25);
		$pitem = $pitem1;
		$pitem.="...";
	}
	echo"<tr bgcolor=\"#FFFFFF\">
            <td>$pid</td>
            <td>$pitem</td>
           <td>$pdes</td>
            <td>$price</td>
            <td><img src =\"images\web1.gif\" width = 32 height = 24><a href=./edit_product.php?$pid><font face=verdana size=2>[Edit]</font></a> | <a href=./del_product.php?$pid><font face=verdana size=2>[Delete]</font></a></td>
             </tr>";
	}
 echo "</table></tr></td></table><br><img src=images/line.gif border=0 width=100%>";
 echo "<br><font face = Verdana size = 2><p align = justify>NOTE TO USERS: Please click on the EDIT button button to edit details regarding a product. Please click on the DELETE button to delete a product<br></p></font><br>";
echo "<center>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></b></p></font><br>";
include("./your-footer.inc");
}

######################## display information no search #####################################

if ($search == NULL)
{


if ($sort == "true")
{
	if ($display == "Ascending Order")
	{
		#sort by item
		if ($sortorder == "Item")
		$query_count = "SELECT * from $table order by pitem asc";
		#sort by description
		if ($sortorder == "Description")
		$query_count = "SELECT * from $table order by pdes asc";
		#sort by price
		if ($sortorder == "Price")
		$query_count = "SELECT * from $table order by price asc";
		#sort by all
		if ($sortorder == "All")
		$query_count = "SELECT * from $table order by pid asc";
	}
	
	if ($display == "Descending Order")
	{
		#sort by item
		if ($sortorder == "Item")
		$query_count = "SELECT * from $table order by pitem desc";
		#sort by description
		if ($sortorder == "Description")
		$query_count = "SELECT * from $table order by pdes desc";
		#sort by price
		if ($sortorder == "Price")
		$query_count = "SELECT * from $table order by price desc";
		#sort by all
		if ($sortorder == "All")
		$query_count = "SELECT * from $table order by pid desc";
	}
	
}

if (empty($page))
$page = 1;
$limitv = $page * $limit - ($limit);

if ($sort == "true")
{
	$query = "";
	$query = $query_count;
	$query.= " LIMIT $limitv,$limit";
}

if ($sort != "true")
$query_count = "SELECT * from $table order by pid";
$res_count = mysql_query($query_count,$conn);
$totalrows = mysql_num_rows($res_count);
if ($sort != "true")
$query = "SELECT * from $table order by pid LIMIT $limitv,$limit";
$result = mysql_query($query,$conn)
	or die("SQL command fail ".mysql_error());
$count_result = mysql_num_rows($result);

$numpages = ceil($totalrows / $limit);

echo "<p><center><b>Found $totalrows</b> result(s)  - Listing $limit products per page.</center></p>";
$from = $limit*$page-$limit+1;
$to = $from+$count_result-1;
if ($style == 1) #style 1
{
echo "<table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr><td width=\"50%\" bgcolor=\"$backcolor\" align=\"left\">";if($numpages>1){echo"Showing item(s): $from to $to</td><td width=\"50%\" bgcolor=\"$backcolor\" align=\"right\"><b>Go to page:</b> ";} 
}
if ($style == 2) #style 2
{
echo "<table align=\"center\" width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
            <tr><td width=\"50%\" bgcolor=\"$backcolor\" align=\"left\">";if($numpages>1){echo"Showing item(s): $from to $to</td><td width=\"50%\" bgcolor=\"$backcolor\" align=\"right\"><b>Go to page:</b> ";} 
}
if($page != 1){ 
$pageprev = $page - 1; 
if ($sort == "false")
echo("<a href=\"$PHP_SELF?$pageprev*?$limit\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
else
{
if ($sort == "false")
echo("<a href=\"$PHP_SELF?$pageprev*?$limit@\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
else {
	#ascending order
	
	#> = ascending order 
	#:1 = item :2 = description :3 = price :4 = all
	if ($display =="Ascending Order")
	{
		if ($sortorder == "Item")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit->:1\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
		if ($sortorder == "Description")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit->:2\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
		if ($sortorder == "Price")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit->:3\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
		if ($sortorder == "All")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit->:4\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;");  
	}

	#descending order
	
	#< = ascending order 
	#:1 = item :2 = description :3 = price :4 = all

	if ($display =="Descending Order")
	{
		if ($sortorder == "Item")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit-<:1\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
		if ($sortorder == "Description")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit-<:1\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
		if ($sortorder == "Price")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit-<:1\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
		if ($sortorder == "All")
		echo("<a href=\"$PHP_SELF?$pageprev*?$limit-<:1\"><img src = ./images/back.gif  width = 16 height = 20 border = 0></a>&nbsp;"); 
	}

}
}

} 

// display page nos if not 1 
for($i = 1; $i <= $numpages; $i++){ 
if($numpages>1){
if($i == $page) 
echo("&nbsp;".$i."&nbsp;"); 
else
{
if ($sort == "false")
echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit\">$i</a>&nbsp;"); 
else
{
	#ascending order
	
	#> = ascending order 
	#:1 = item :2 = description :3 = price :4 = all
	if ($display =="Ascending Order")
	{
		if ($sortorder == "Item")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit->:1\">$i</a>&nbsp;"); 
		if ($sortorder == "Description")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit->:2\">$i</a>&nbsp;"); 
		if ($sortorder == "Price")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit->:3\">$i</a>&nbsp;"); 
		if ($sortorder == "All")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit->:4\">$i</a>&nbsp;"); 
	}

	#descending order
	
	#< = ascending order 
	#:1 = item :2 = description :3 = price :4 = all

	if ($display =="Descending Order")
	{
		if ($sortorder == "Item")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit-<:1\">$i</a>&nbsp;"); 
		if ($sortorder == "Description")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit-<:2\">$i</a>&nbsp;"); 
		if ($sortorder == "Price")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit-<:3\">$i</a>&nbsp;"); 
		if ($sortorder == "All")
		echo("&nbsp;<a href=\".\products_edit.php?$i*?$limit-<:4\">$i</a>&nbsp;"); 
	}
}

}
}}

// display next page link if there is more than one
if(($totalrows - ($limit * $page)) > 0){ 
$pagenext = $page + 1; 
if ($sort == "false")
echo("<a href=\"$PHP_SELF?$pagenext*?$limit\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
else
{
if ($sort == "false")
echo("<a href=\"$PHP_SELF?$pagenext*?$limit@\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
else {
	#ascending order
	
	#> = ascending order 
	#:1 = item :2 = description :3 = price :4 = all
	if ($display =="Ascending Order")
	{
		if ($sortorder == "Item")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit->:1\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
		if ($sortorder == "Description")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit->:2\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
		if ($sortorder == "Price")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit->:3\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
		if ($sortorder == "All")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit->:4\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
	}

	#descending order
	
	#< = ascending order 
	#:1 = item :2 = description :3 = price :4 = all

	if ($display =="Descending Order")
	{
		if ($sortorder == "Item")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit-<:4\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
		if ($sortorder == "Description")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit-<:4\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
		if ($sortorder == "Price")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit-<:4\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
		if ($sortorder == "All")
		echo("<a href=\"$PHP_SELF?$pagenext*?$limit-<:4\"><img src = ./images/next.gif width = 16 height = 20 border = 0></a>"); 
	}
}
}
}
echo "</td><tr></table><br>";

######################### display part ##############################

echo"<table align=\"center\" valign=\"top\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr bgcolor=\"$backcolor\"><td>
			<table cellpadding=\"2\" border=\"0\" cellspacing=\"1\" valign=\"top\" width=\"100%\" align=\"center\">
				<tr bgcolor=\"#FFFFFF\">
					<td background=\"images/bevel_bg.gif\" height=\"20\"><b>Product ID</b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b>Item</b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b>Description</b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b>Price</b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b>Information</b></td></tr>";

	while($row = mysql_fetch_array($result))
	{
	$pid = $row["pid"];
	$pitem = $row["pitem"];
	$pdes = $row["pdes"];
	$price = $row["price"];
	$price = number_format($price,2);
	############ truncate product description ##########
	if (strlen($pdes) >= 30)
	{
		$pdes1 = substr($pdes,0,30);
		$pdes = $pdes1;
		$pdes.="...";
		$pitem1 = substr($pitem,0,25);
		$pitem = $pitem1;
		$pitem.="...";
	}
	echo"<tr bgcolor=\"#FFFFFF\">
            <td>$pid</td>
            <td>$pitem</td>
           <td>$pdes</td>
            <td>$price</td>
    <td><img src =\"images\web1.gif\" width = 32 height = 24><a href=./edit_product.php?$pid>[Edit]</a> | <a href=./del_product.php?$pid>[Delete]</a></td>
             </tr>";
	}
 echo "</table></tr></td></table><br><hr>";
 echo "<br><font face = Verdana size = 2><p align = justify>NOTE TO USERS: Please click on the EDIT button button to edit details regarding a product. Please click on the DELETE button to delete a product<br></p></font><br>";
echo "<center>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></b></p></font><br>";
include_once("./footer.inc");
}
?>
</body>
</html>