<html>
<head><title>EZ Shopper - Edit a stock record</title>
<body>
<?php
/***************************************************************************
 *			stocks_edit.php                                    *                             	 *                                                                         *
 *   File Info:		  Stocks Edit/Delete                               *
 *   Copyright:       Copyright 2003 http://www.128concepts.com            *
 *						                           *
 ***************************************************************************/

include_once("./header.inc");

echo "<center><img src=images/searching.gif border=0></center><br><center><font face=verdana size=3 color= navy>List all  products and edit/delete stock record(s)</font></center>";
$page = getenv("QUERY_STRING");

$cando = "false";
$ina = strpos($page,'=');
$inb = strpos($page,'+');
$ins = strpos($page,'*');

if ($ina != NULL or $inb != NULL or $ins != NULL)
	$cando = "true";

if (strlen($page) > 0 && $cando == "true" )
{
	$str = $page;
	$pos = strpos($str,'=');
	$pos1 = strpos($str,'+');
	$pos2 = strpos($str,'*');
	$poss1 = strpos($str,'?');
	$poss = strpos($str,'?');
	$page = substr($str,0,$pos);
	if ($ina != NULL)
	{
		$search = substr($str,$poss+1,strlen($str));
		$search = str_replace("%20"," ",$search);
	}
	$ordercat = substr($str,$poss1+1,strlen($str));
	$ordercat = str_replace("%20"," ",$ordercat);
	if ($pos1 >= 1)
		$page = substr($str,0,$pos1);
	if ($pos2 >= 1)
	{
		$page = substr($str,0,$pos2);
		$limit = substr($str,$poss1+1,strlen($str));
		$cando = "false";
		$ordercat = "";
	}	
}

if ($search == $ordercat and $search != NULL and $ordercat != NULL) // Set order category field to NULL
{
	$ordercat = "";
}

if ($_SERVER['REQUEST_METHOD'] == "POST") 
  {
        # escape data and set variables
        $limit= addslashes($_POST["limit"]);
        $search = addslashes($_POST["search"]);
	$ordercat = addslashes($_POST["ordercat"]);
	if ($ordercat != NULL)
	{
		$cando = "true";
		$search = "";
	}
	$searching = addslashes($_POST["searching"]);
}
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 stock records you wish to view:- ";
$form = "<form action =stocks_edits.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=submit value = 'View Records'></font></form>";
echo $form;


echo "<font face = Verdana size = 2>Please input the stock record(s) you wish to search by:-";
$form = "<form action = stocks_edits.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;

########################################################
$table = "stocks";
$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.");

$forms = "Select the category you wish to sort records by:-";
$forms.="<form action = stocks_edits.php method=POST>";

$table = "category";
$querys = "SELECT * from $table";
$rest = mysql_query($querys,$conn);
$forms.="<select name=ordercat>";
$prods = array();
while($row = mysql_fetch_array($rest))
{
	$category = $row["category"];
	$forms.="<option>$category</option>";
}
$forms.="</select>";
$forms.= "  <input type=submit value = 'Order Records'></font></form>";
echo $forms;

$table = "stocks";

######################### display information order only ##############################

if ($ordercat != NULL)
{
if (empty($page))
$page = 1;
$limitv = $page * $limit - ($limit);
$query_count = "SELECT * from $table where scat = '$ordercat' order by pid";
$res_count = mysql_query($query_count,$conn);
$totalrows = mysql_num_rows($res_count);
$query = "SELECT * from $table  where scat = '$ordercat' 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 = '$ordercat' - Listing $limit stock record(s) 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+?$ordercat\"><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
echo("&nbsp;<a href=\".\stocks_edits.php?$i+?$ordercat\">$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+?$ordercat\"><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>Stock 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>Category</b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b>Stock</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"];
	$sitem = $row["sitem"];
	$scat = $row["scat"];
	$stock = $row["stock"];

	#### truncate description if greater than 25 characters
	if (strlen($sitem) >= 25)
	{
		$sitem1 = substr($sitem,0,25);
		$sitem = $sitem1;
		$sitem.= "...";
	}
	echo"<tr bgcolor=\"#FFFFFF\">
            <td><font face=verdana size=2>$pid</font></td>
            <td><font face=verdana size=2>$sitem</font></td>
           <td><font face=verdana size=2>$scat</font></td>
	<td><font face=verdana size=2>$stock</font></td>
            <td><img src =\"images\web1.gif\" width = 32 height = 24><a href=./edit_stocks.php?$pid><font face=verdana size=2>[Edit]</font></a> | <a href=./del_stock.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 stock record. Please click on the DELETE button to delete a stock record.<br></p></font><br>";
echo "<center><font face=verdana size=2>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></b></p></font></center><br>";
include("./your-footer.inc");
}

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

if ($search != NULL)
{
if (empty($page))
$page = 1;
$limitv = $page * $limit - ($limit);
$query_count = "SELECT * from $table where (sitem like '%$search%') order by pid";
$res_count = mysql_query($query_count,$conn);
$totalrows = mysql_num_rows($res_count);
$query = "SELECT * from $table  where (sitem 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 stock record(s) 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;"); 

} 

// 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=\".\stocks_edits.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>Stock 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>Category</b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b>Stock</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"];
	$sitem = $row["sitem"];
	$scat = $row["scat"];
	$stock = $row["stock"];

	#### truncate description if greater than 25 characters
	if (strlen($sitem) >= 25)
	{
		$sitem1 = substr($sitem,0,25);
		$sitem = $sitem1;
		$sitem.= "...";
	}
	echo"<tr bgcolor=\"#FFFFFF\">
            <td><font face=verdana size=2>$pid</font></td>
            <td><font face=verdana size=2>$sitem</font></td>
           <td><font face=verdana size=2>$scat</font></td>
	<td><font face=verdana size=2>$stock</font></td>
            <td><img src =\"images\web1.gif\" width = 32 height = 24><a href=./edit_stocks.php?$pid><font face=verdana size=2>[Edit]</font></a> | <a href=./del_stock.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 stock record. Please click on the DELETE button to delete a stock record.<br></p></font><br>";
echo "<center><font face=verdana size=2>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></b></p></font></center><br>";
include("./your-footer.inc");
}

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

if ($search == NULL && $cando == "false")
{
if (empty($page))
$page = 1;
$limitv = $page * $limit - ($limit);
$query_count = "SELECT * from $table order by pid";
$res_count = mysql_query($query_count,$conn);
$totalrows = mysql_num_rows($res_count);

$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><font face=verdana size=2>Found $totalrows</b> result(s)  - Listing $limit stock record(s) 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*$limit\"><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
echo("&nbsp;<a href=\".\stocks_edits.php?$i*?$limit\">$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*?$limit\"><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>Stock ID</font></b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b><font face=verdana size=2>Stock Item</font></b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b>Category</b></td><td background=\"images/bevel_bg.gif\" height=\"20\"><b><font face=verdana size=2>Stock</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"];
	$sitem = $row["sitem"];
	$scat = $row["scat"];
	$stock = $row["stock"];
	#### truncate stock item if greater than 25 characters
	if (strlen($sitem) >= 25)
	{
		$sitem1 = substr($sitem,0,25);
		$sitem = $sitem1;
		$sitem.= "...";
	}
	echo"<tr bgcolor=\"#FFFFFF\">
            <td><font face=verdana size=2>$pid</font></td>
            <td><font face=verdana size=2>$sitem</font></td>
           <td><font face=verdana size=2>$scat</font></td>
            <td><font face=verdana size=2>$stock</font></td>
    <td><img src =\"images\web1.gif\" width = 32 height = 24><a href=./edit_stocks.php?$pid><font face=verdana size=2>[Edit]</font></a> | <a href=./del_stock.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 stock record. Please click on the DELETE button to delete a stock record.<br></p></font><br>";
echo "<center><font face=verdana size=2>Return back to <b><a href=./prodstocks.php>products/stocks menu</a></b></p></font></center><br>";
include_once("./footer.inc");
}
?>
</body>
</html>