Location: PHPKode > projects > eCanteen > ecanteen/maintain/menuitem.php
<?
//Copyright David Byrne 2007

//This file is part of eCanteen.

//    eCanteen is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    eCanteen is distributed in the hope that it will be useful
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with eCanteen.  If not, see <http://www.gnu.org/licenses/>.
session_start(); 
if ($_SESSION['userlevel']>=3)
{
	include("../include/constants.php");      
	mysql_free_result();
	$filter = "";
  $filterfield = "";
  $wholeonly = "";
  $order = "";
  $ordtype = "";
	?>
<html>
<head>
<title>ecanteen -- menuitem</title>
<meta name="generator" content="text/html">
<LINK REL="stylesheet" TYPE="text/css" HREF="maintain.css">

<script type="text/javascript" src="../yuiutils/build/yahoo/yahoo.js"></script> 
<script type="text/javascript" src="../yuiutils/build/event/event.js" ></script> 
<script type="text/javascript" src="../yuiutils/build/dom/dom.js" ></script>  
<script type="text/javascript" src="../yuiutils/build/calendar/calendar.js"></script> 

<link rel="stylesheet" type="text/css" href="../yuiutils/build/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="../yuiutils/build/calendar/assets/calendar.css" />

</head>
<body>

<table class="bd" width="50%"><tr><td class="hr"><h2>Menu Items</h2></td><td class="bd" width="50%"><button id="Return2Cal" onclick="window.location.href='../index.php';return true;" style="width: 190px">Return to Main Menu</button></td></tr></table>
<?
  $conn = connect();
  $showrecs = 20;
  $pagerange = 10;

  $a = @$_GET["a"];
  $recid = @$_GET["recid"];
  if (isset($_GET["order"])) $order = @$_GET["order"];
  if (isset($_GET["type"])) $ordtype = @$_GET["type"];
  
  
  if (isset($_POST["order"])) $order = @$_POST["order"];
  if (isset($_POST["type"])) $ordtype = @$_POST["type"];

  if (isset($_POST["filter"])) $filter = @$_POST["filter"];
  if (isset($_POST["filter_field"])) $filterfield = @$_POST["filter_field"];
  
  $wholeonly = false;
  if (isset($_POST["wholeonly"])) $wholeonly = @$_POST["wholeonly"];

  if (!isset($order) && isset($_SESSION["order"])) $order = $_SESSION["order"];
  if (!isset($ordtype) && isset($_SESSION["type"])) $ordtype = $_SESSION["type"];
  if (!isset($filter) && isset($_SESSION["filter"])) $filter = $_SESSION["filter"];
  if (!isset($filterfield) && isset($_SESSION["filter_field"])) $filterfield = $_SESSION["filter_field"];

  $page = @$_GET["page"];
  if (!isset($page)) $page = 1;

  $sql = @$_POST["sql"];

  switch ($sql) {
    case "insert":
      sql_insert();
      break;
    case "update":
      sql_update();
      break;
    case "delete":
      sql_delete();
      break;
  }

  switch ($a) {
    case "add":
      addrec();
      break;
    case "view":
      viewrec($recid);
      break;
    case "edit":
      editrec($recid);
      break;
    case "del":
      deleterec($recid);
      break;
    default:
      select();
      break;
  }

  if (isset($order)) $_SESSION["order"] = $order;
  if (isset($ordtype)) $_SESSION["type"] = $ordtype;
  if (isset($filter)) $_SESSION["filter"] = $filter;
  if (isset($filterfield)) $_SESSION["filter_field"] = $filterfield;
  if (isset($wholeonly)) $_SESSION["wholeonly"] = $wholeonly;
	
	
	


  //mysql_close($conn);
	
	
	
?>
<table class="bd" width="100%"><tr><td class="hr"></td></tr></table>
</body>
</html>
<?}?>
<? function select()
  {
  global $a;
  global $showrecs;
  global $page;
  global $filter;
  global $filterfield;
  global $wholeonly;
  global $order;
  global $ordtype;


  if ($a == "reset") {
    $filter = "";
    $filterfield = "";
    $wholeonly = "";
    $order = "";
    $ordtype = "";
  }
	
  $checkstr = "";
  if ($wholeonly) $checkstr = " checked";
  if ($ordtype == "asc") { $ordtypestr = "desc"; } else { $ordtypestr = "asc"; }
  $res = sql_select();
  $count = sql_getrecordcount();
  if ($count % $showrecs != 0) {
    $pagecount = intval($count / $showrecs) + 1;
  }
  else {
    $pagecount = intval($count / $showrecs);
  }
  $startrec = $showrecs * ($page - 1);
  if ($startrec < $count) {mysql_data_seek($res, $startrec);}
  $reccount = min($showrecs * $page, $count);
  $fields = array(
    "MenuItemID" => "ID",
    "Item" => "Item",
    "Description" => "Description",
		"Abbreviation"=> "Abbrv.",
    "ImagePath" => "ImagePath",
    "ItemType\$" => "ItemType",
    "Barcode" => "Barcode",
    "Break\$" => "Break",
    "Cost" => "Cost",
    "FromDate" => "FromDate",
		
    "ToDate" => "ToDate");
		
		$conn2= mysql_connect(DB_SERVER, DB_USER, DB_PASS);
     mysql_select_db(DB_NAME);
		$sql = "select `ItemTypeID`, `ItemType` from `itemtype` WHERE `~SiteID$`=".$_SESSION['SiteID'].";";
		$arrayres = mysql_query($sql,$conn2) or die(mysql_error());
		$resnum=mysql_numrows($arrayres);
		for ($ux=0; $ux<=40; $ux=$ux+1)
			{
	  	$ItemTypeArray[$ux][0]=0;
			}
		$i=0;
		while ($i < $resnum) 
 	 {
			$ItemTypeArray[mysql_result($arrayres,$i,"ItemTypeID")][1]=mysql_result($arrayres,$i,"ItemType")." (".mysql_result($arrayres,$i,"ItemTypeID").")";
			$i++;
		}
		
		$sql = "select `BreakID`, `Break` from `break` WHERE `~SiteID$`=".$_SESSION['SiteID']." ;";
	$arrayres = mysql_query($sql, $conn2) or die(mysql_error());
	$resnum=mysql_numrows($arrayres);
	for ($ux=0; $ux<=40; $ux=$ux+1)
	{
  	$BreakArray[$ux][0]=0;
	}
	$i=0;
	while ($i < $resnum) 
  {
		$BreakArray[mysql_result($arrayres,$i,"BreakID")][1]=mysql_result($arrayres,$i,"Break")." (".mysql_result($arrayres,$i,"BreakID").")";
		$i++;
	}
		mysql_close($conn2);


?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">

<tr><td>Records shown <? echo $startrec + 1 ?> - <? echo $reccount ?> of <? echo $count ?></td></tr>
</table>
<hr size="1" noshade>
<form action="menuitem.php" method="post">
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><b>Custom Filter</b>&nbsp;</td>
<td><input type="text" name="filter" value="<? echo $filter ?>"></td>
<td><select name="filter_field">
<option value="">All Fields</option>
<?
  reset($fields);
  foreach($fields as $val => $caption) {
    if ($val == $filterfield) {$selstr = " selected"; } else {$selstr = ""; }
?>
<option value="<? echo $val ?>"<? echo $selstr ?>><? echo htmlspecialchars($caption) ?></option>
<? } ?>
</select></td>
<td><input type="checkbox" name="wholeonly"<? echo $checkstr ?>>Whole words only</td>
</td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="action" value="Apply Filter"></td>
<td><a href="menuitem.php?a=reset">Reset Filter</a></td>
</tr>
</table>
</form>
<hr size="1" noshade>
<? showpagenav($page, $pagecount,$order,$ordtype); ?>
<br>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="100%">
<tr>
<?
	
  reset($fields);
  foreach($fields as $val => $caption) {
?>
<td class="hr"><a class="hr" href="menuitem.php?order=<? echo $val ?>&type=<? echo $ordtypestr ?>"><? echo htmlspecialchars($caption) ?></a></td>
<? } ?>
<td class="hr">&nbsp;</td>

<td class="hr">&nbsp;</td>
</tr>
<?

  for ($i = $startrec; $i < $reccount; $i++)
  {
    $row = mysql_fetch_assoc($res);
    $style = "dr";
    if ($i % 2 != 0) {
      $style = "sr";
    }
?>
<tr>
<?

  reset($fields);
  foreach($fields as $val => $caption) 
	{
		print "<td class=".$style.">";
  	if($caption=="Break"||$caption=="ItemType")
		{
			if($caption=="Break")
			{
				print $BreakArray[$row[$val]][1];
			}else{
				print $ItemTypeArray[$row[$val]][1];
			}
  	}elseif (stristr($caption,"date"))
			{
				print substr($row[$val],0,10);
			}
			elseif ($caption=="Active")
			{
			if ($row[$val]=1)
			{
			print "Yes";
			}else{
			print "No";
			} 
			
			}else{
			print htmlspecialchars($row[$val]);
			}
		}
	
print "</td>";
 ?>

<td class="<? echo $style ?>"><a href="menuitem.php?a=edit&recid=<? echo $i ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>">Edit</a></td>
<td class="<? echo $style ?>"><a href="menuitem.php?a=del&recid=<? echo $i ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>">Delete</a></td>

</tr>
<?
  }
  mysql_free_result($res);
?>
</table>
<br>
<? showpagenav($page, $pagecount,$order,$ordtype); ?>
<? } ?>

<? function showrow($row)
  {
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><? echo htmlspecialchars("MenuItemID")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["MenuItemID"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Item")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["Item"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Abbrv.")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["Abbreviation"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Description")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["Description"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("ImagePath")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["ImagePath"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("ItemType\$")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["ItemType\$"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Barcode")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["Barcode"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Break\$")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["Break\$"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Cost")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["Cost"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("FromDate")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["FromDate"]) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("ToDate")."&nbsp;" ?></td>
<td class="dr"><? echo htmlspecialchars($row["ToDate"]) ?></td>
</tr>
</table>
<? } ?>

<? function showroweditor($row)
  {
  global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%"  cols="2">
<tr >
<td class="hr"><? echo htmlspecialchars("ID")."&nbsp;" ?></td>
<td class="dr"><? echo str_replace('"', '&quot;', trim($row["MenuItemID"])) ?></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Item")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Item" maxlength="50" value="<? echo str_replace('"', '&quot;', trim($row["Item"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Abbrv.")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Abbreviation" maxlength="20" value="<? echo str_replace('"', '&quot;', trim($row["Abbreviation"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Description")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="Description" maxlength="100"><? echo str_replace('"', '&quot;', trim($row["Description"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("ImagePath")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="ImagePath" maxlength="60"><? echo str_replace('"', '&quot;', trim($row["ImagePath"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("ItemType")."&nbsp;" ?></td>
<td class="dr"><select name="ItemType$">
<?
  $sql = "select `ItemTypeID`, `ItemType` from `itemtype` WHERE `~SiteID$`=".$_SESSION['SiteID']." ORDER BY `ItemType`" ;
  $res = mysql_query($sql, $conn) or die(mysql_error());

  while ($lp_row = mysql_fetch_assoc($res)){
  $val = $lp_row["ItemTypeID"];
  $caption = $lp_row["ItemType"];
  if ($row["ItemType\$"] == $lp_row["ItemTypeID"]) {$selstr = " selected"; } else {$selstr = ""; }
 ?><option value="<? echo $val ?>"<? echo $selstr ?>><? echo $caption ?></option>
<? } ?></select>
</td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Barcode")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Barcode" maxlength="15" value="<? echo str_replace('"', '&quot;', trim($row["Barcode"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Break")."&nbsp;" ?></td>
<td class="dr"><select name="Break$">
<?
  $sql = "select `BreakID`, `Break` from `break` WHERE `~SiteID$`=".$_SESSION['SiteID']." ORDER BY `Break`";
  $res = mysql_query($sql, $conn) or die(mysql_error());

  while ($lp_row = mysql_fetch_assoc($res)){
  $val = $lp_row["BreakID"];
  $caption = $lp_row["Break"];
  if ($row["Break\$"] == $lp_row["BreakID"]) {$selstr = " selected"; } else {$selstr = ""; }
 ?><option value="<? echo $val ?>"<? echo $selstr ?>><? echo $caption ?></option>
<? } ?></select>
</td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Cost")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Cost" value="<? echo str_replace('"', '&quot;', trim($row["Cost"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("FromDate")."&nbsp;" ?></td>

<td class="dr"><input type="hidden" name="FromDate" id="FromDate" value="<? echo str_replace('"', '&quot;', trim($row["FromDate"])) ?>"><div id="cal1Container"></div> 

<script type="text/javascript">
	YAHOO.namespace("example.calendar");

	YAHOO.example.calendar.init = function() {

		function handleSelect(type,args,obj) {
			var dates = args[0]; 
			var date = dates[0];
			var year = date[0], month = date[1], day = date[2];
			
			var txtDate1 = document.getElementById("FromDate");
			//txtDate1.value = month + "/" + day + "/" + year;
			txtDate1.value = year + "-" + month + "-" +day ;
		}
   YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1Container", 
																	{ mindate:"1/1/2006",
																	  maxdate:"12/31/2008" });
			var txtDate1 = document.getElementById("FromDate");
			if (txtDate1.value != "") {			
				var str=txtDate1.value;
				var tDate=str.split(" ");
			  //window.alert(tDate[0]);
				var xDates=tDate[0].split("-");			
				var yuiDate=xDates[1]+"/"+xDates[2]+"/"+xDates[0]
				//window.alert(yuiDate);		
				YAHOO.example.calendar.cal1.select(yuiDate);
				var selectedDates = YAHOO.example.calendar.cal1.getSelectedDates();
				if (selectedDates.length > 0) {
					var firstDate = selectedDates[0];
					YAHOO.example.calendar.cal1.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
					YAHOO.example.calendar.cal1.render();
				} else {
					alert("Cannot select a date before 1/1/2006 or after 12/31/2008");
				}			
			}
		YAHOO.example.calendar.cal1.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.cal1, true);
		YAHOO.example.calendar.cal1.render();
	}
	YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);
</script>

<div style="clear:both" ></div>


</td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("ToDate")."&nbsp;" ?></td>
<td class="dr"><input type="hidden" name="ToDate" id="ToDate" value="<? echo str_replace('"', '&quot;', trim($row["ToDate"])) ?>">
<div id="cal2Container"></div> 
<script type="text/javascript">
	YAHOO.namespace("example.calendar");

	YAHOO.example.calendar.init = function() {

		function handleSelect(type,args,obj) {
			var dates = args[0]; 
			var date = dates[0];
			var year = date[0], month = date[1], day = date[2];
			
			var txtDate1 = document.getElementById("ToDate");
			//txtDate1.value = month + "/" + day + "/" + year;
			txtDate1.value = year + "-" + month + "-" +day ;
		}
   YAHOO.example.calendar.cal2 = new YAHOO.widget.Calendar("cal2","cal2Container", 
																	{ mindate:"1/1/2006",
																	  maxdate:"12/31/2008" });
			var txtDate1 = document.getElementById("ToDate");
			if (txtDate1.value != "") {			
				var str=txtDate1.value;
				var tDate=str.split(" ");
			  //window.alert(tDate[0]);
				var xDates=tDate[0].split("-");			
				var yuiDate=xDates[1]+"/"+xDates[2]+"/"+xDates[0]
				//window.alert(yuiDate);		
				YAHOO.example.calendar.cal2.select(yuiDate);
				var selectedDates = YAHOO.example.calendar.cal2.getSelectedDates();
				if (selectedDates.length > 0) {
					var firstDate = selectedDates[0];
					YAHOO.example.calendar.cal2.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
					YAHOO.example.calendar.cal2.render();
				} else {
					alert("Cannot select a date before 1/1/2006 or after 12/31/2008");
				}			
			}
		YAHOO.example.calendar.cal2.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.cal2, true);
		YAHOO.example.calendar.cal2.render();
	}
	YAHOO.util.Event.onDOMReady(YAHOO.example.calendar.init);
</script>

<div style="clear:both" ></div>


</td>
</tr>
</table>

<input type="hidden" name="order" value="<? echo $_GET["order"] ?>">
<input type="hidden" name="type" value="<? echo $_GET["type"] ?>">

<? } ?>





<? function showpagenav($page, $pagecount,$order,$ordtype)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="menuitem.php?a=add&order=<? echo $order ?>&type=<? echo $ordtype ?>">Add Record</a>&nbsp;</td>
<? if ($page > 1) { ?>
<td><a href="menuitem.php?page=<? echo $page - 1 ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>">&lt;&lt;&nbsp;Prev</a>&nbsp;</td>
<? } ?>
<?
  global $pagerange;

  if ($pagecount > 1) {

  if ($pagecount % $pagerange != 0) {
    $rangecount = intval($pagecount / $pagerange) + 1;
  }
  else {
    $rangecount = intval($pagecount / $pagerange);
  }
  for ($i = 1; $i < $rangecount + 1; $i++) {
    $startpage = (($i - 1) * $pagerange) + 1;
    $count = min($i * $pagerange, $pagecount);

    if ((($page >= $startpage) && ($page <= ($i * $pagerange)))) {
      for ($j = $startpage; $j < $count + 1; $j++) {
        if ($j == $page) {
?>
<td><b><? echo $j ?></b></td>
<? } else { ?>
<td><a href="menuitem.php?page=<? echo $j ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>"><? echo $j ?></a></td>
<? } } } else { ?>
<td><a href="menuitem.php?page=<? echo $startpage ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>"><? echo $startpage ."..." .$count ?></a></td>
<? } } } ?>
<? if ($page < $pagecount) { ?>
<td>&nbsp;<a href="menuitem.php?page=<? echo $page + 1 ?>&order=<? echo $order ?>&type=<? echo $ordtype ?>">Next&nbsp;&gt;&gt;</a>&nbsp;</td>
<? } ?>
</tr>
</table>
<? } ?>

<? function showrecnav($a, $recid, $count)
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="menuitem.php?order=<? echo $_GET["order"] ?>&type=<? echo $_GET["type"] ?>">Index Page</a></td>
<? if ($recid > 0) { ?>
<td><a href="menuitem.php?a=<? echo $a ?>&recid=<? echo $recid - 1 ?>&order=<? echo $_GET["order"] ?>&type=<? echo $_GET["type"]?>">Prior Record</a></td>
<? } if ($recid < $count) { ?>
<td><a href="menuitem.php?a=<? echo $a ?>&recid=<? echo $recid + 1 ?>&order=<? echo $_GET["order"] ?>&type=<? echo $_GET["type"]?>">Next Record</a></td>
<? } ?>
</tr>
</table>
<hr size="1" noshade>
<? } ?>

<? function addrec()
{
?>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="menuitem.php">Index Page</a></td>
</tr>
</table>
<hr size="1" noshade>
<form action="menuitem.php" method="post">
<p><input type="hidden" name="sql" value="insert"></p>
<?
$row = array(
  "MenuItemID" => "",
  "Item" => "",
	"Abbreviation"=>"",
  "Description" => "",
  "ImagePath" => "",
  "ItemType\$" => "",
  "Barcode" => "",
  "Break\$" => "",
  "Cost" => "",
  "FromDate" => "",
  "ToDate" => "");
showroweditor($row)
?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<? } ?>

<? function viewrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("view", $recid, $count);
?>
<br>
<? showrow($row) ?>
<br>
<hr size="1" noshade>
<table class="bd" border="0" cellspacing="1" cellpadding="4">
<tr>
<td><a href="menuitem.php?a=add">Add Record</a></td>
<td><a href="menuitem.php?a=edit&recid=<? echo $recid ?>">Edit Record</a></td>
<td><a href="menuitem.php?a=del&recid=<? echo $recid ?>">Delete Record</a></td>
</tr>
</table>
<?
  mysql_free_result($res);
} ?>

<? function editrec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("edit", $recid, $count);
?>
<br>
<form action="menuitem.php" method="post">
<input type="hidden" name="sql" value="update">
<input type="hidden" name="xMenuItemID" value="<? echo $row["MenuItemID"] ?>">
<? showroweditor($row) ?>
<p><input type="submit" name="action" value="Post"></p>
</form>
<?
  mysql_free_result($res);
} ?>

<? function deleterec($recid)
{
  $res = sql_select();
  $count = sql_getrecordcount();
  mysql_data_seek($res, $recid);
  $row = mysql_fetch_assoc($res);
  showrecnav("del", $recid, $count);
?>
<br>
<form action="menuitem.php" method="post">
<input type="hidden" name="sql" value="delete">
<input type="hidden" name="xMenuItemID" value="<? echo $row["MenuItemID"] ?>">
<? showrow($row) ?>
<p><input type="submit" name="action" value="Confirm"></p>
</form>
<?
  mysql_free_result($res);
} ?>

<? function connect()
{
 $conn = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
  mysql_select_db(DB_NAME);
  return $conn;
}

function sqlvalue($val, $quote)
{
  if ($quote)
    $tmp = sqlstr($val);
  else
    $tmp = $val;
  if ($tmp == "")
    $tmp = "NULL";
  elseif ($quote)
    $tmp = "'".$tmp."'";
  return $tmp;
}

function sqlstr($val)
{
  return str_replace("'", "''", $val);
}

function sql_select()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "select `MenuItemID`, `Item`, `Abbreviation`, `Description`, `ImagePath`, `ItemType\$`, `Barcode`, `Break\$`, `Cost`, `FromDate`, `ToDate`, `~SiteID\$` from `menuitem` where (`~SiteID\$`=".$_SESSION['SiteID'].")";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " and " .sqlstr($filterfield) ." like '" .$filterstr ."' AND (`~SiteID\$`=".$_SESSION['SiteID'].")";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " and (`MenuItemID` like '" .$filterstr ."') or (`Item` like '" .$filterstr ."') or (`Abbreviation` like '" .$filterstr ."') or (`Description` like '" .$filterstr ."') or (`ImagePath` like '" .$filterstr ."') or (`ItemType\$` like '" .$filterstr ."') or (`Barcode` like '" .$filterstr ."') or (`Break\$` like '" .$filterstr ."') or (`Cost` like '" .$filterstr ."') or (`FromDate` like '" .$filterstr ."') or (`ToDate` like '" .$filterstr ."') AND (`~SiteID\$`=".$_SESSION['SiteID'].")";
  }
  if (isset($order) && $order!='') $sql .= " order by `" .sqlstr($order) ."`";
  if (isset($ordtype) && $ordtype!='') $sql .= " " .sqlstr($ordtype);
  $res = mysql_query($sql, $conn) or die(mysql_error());
  return $res;
}

function sql_getrecordcount()
{
  global $conn;
  global $order;
  global $ordtype;
  global $filter;
  global $filterfield;
  global $wholeonly;

  $filterstr = sqlstr($filter);
  if (!$wholeonly && isset($wholeonly) && $filterstr!='') $filterstr = "%" .$filterstr ."%";
  $sql = "select count(*) from `menuitem` where (`~SiteID\$`=".$_SESSION['SiteID'].")";
  if (isset($filterstr) && $filterstr!='' && isset($filterfield) && $filterfield!='') {
    $sql .= " and " .sqlstr($filterfield) ." like '" .$filterstr ."'  AND (`~SiteID\$`=".$_SESSION['SiteID'].")";
  } elseif (isset($filterstr) && $filterstr!='') {
    $sql .= " and (`MenuItemID` like '" .$filterstr ."') or (`Item` like '" .$filterstr ."') or (`Abbreviation` like '" .$filterstr ."') or (`Description` like '" .$filterstr ."') or (`ImagePath` like '" .$filterstr ."') or (`ItemType\$` like '" .$filterstr ."') or (`Barcode` like '" .$filterstr ."') or (`Break\$` like '" .$filterstr ."') or (`Cost` like '" .$filterstr ."') or (`FromDate` like '" .$filterstr ."') or (`ToDate` like '" .$filterstr ."') ";
  }
  $res = mysql_query($sql, $conn) or die(mysql_error());
  $row = mysql_fetch_assoc($res);
  reset($row);
  return current($row);
}

function sql_insert()
{
  global $conn;
  global $_POST;

  $sql = "insert into `menuitem` (`MenuItemID`, `Item`, `Abbreviation`, `Description`, `ImagePath`, `ItemType\$`, `Barcode`, `Break\$`, `Cost`, `FromDate`, `ToDate`,`~SiteID\$`) values (" .sqlvalue(@$_POST["MenuItemID"], false) .", " .sqlvalue(@$_POST["Item"], true) .", " .sqlvalue(@$_POST["Abbreviation"], true) .", " .sqlvalue(@$_POST["Description"], true) .", " .sqlvalue(@$_POST["ImagePath"], true) .", " .sqlvalue(@$_POST["ItemType\$"], false) .", " .sqlvalue(@$_POST["Barcode"], true) .", " .sqlvalue(@$_POST["Break\$"], false) .", " .sqlvalue(@$_POST["Cost"], false) .", " .sqlvalue(@$_POST["FromDate"], true) .", " .sqlvalue(@$_POST["ToDate"], true) .",".$_SESSION['SiteID'].")";
  mysql_query($sql, $conn) or die(mysql_error());
}


function loadItemTypearray()
{
	$conn2= mysql_connect(DB_SERVER, DB_USER, DB_PASS);
  mysql_select_db(DB_NAME);
  return $conn;
	$sql = "select `ItemTypeID`, `ItemType` from `itemtype`;";
	$arrayres = mysql_query($sql,$conn2) or die(mysql_error());
	$resnum=mysql_numrows($arrayres);
	
	for ($ux=0; $ux<=40; $ux=$ux+1)
	{
  	$ItemTypeArray[$ux][0]=0;
	}
	
	$i=0;
	while ($i < $resnum) 
  {
		$ItemTypeArray[mysql_result($arrayres,$i,"ItemTypeID")][1]=mysql_result($arrayres,$i,"ItemType");
		$i++;
	}
	mysql_close($conn2);
	return $ItemTypeArray;

}



function loadBreakarray()

{
	
	$conn2 = mysql_connect("localhost", "root", "pass");
  mysql_select_db("ecanteen");
	$sql = "select `BreakID`, `Break` from `Break`;";
	$arrayres = mysql_query($sql, $conn2) or die(mysql_error());
	
	$resnum=mysql_numrows($arrayres);
	
	for ($ux=0; $ux<=40; $ux=$ux+1)
	{
  	$BreakArray[$ux][0]=0;
	}

	$i=0;
	while ($i < $resnum) 
  {
		$BreakArray[mysql_result($arrayres,$i,"BreakID")][1]=mysql_result($arrayres,$i,"Break");
		$i++;
	}
	mysql_close($conn2);
	return $BreakArray;

}



function sql_update()
{
  global $conn;
  global $_POST;

  $sql = "update `menuitem` set `Item`=" .sqlvalue(@$_POST["Item"], true) .", `Abbreviation`=" .sqlvalue(@$_POST["Abbreviation"], true) .", `Description`=" .sqlvalue(@$_POST["Description"], true) .", `ImagePath`=" .sqlvalue(@$_POST["ImagePath"], true) .", `ItemType\$`=" .sqlvalue(@$_POST["ItemType\$"], false) .", `Barcode`=" .sqlvalue(@$_POST["Barcode"], true) .", `Break\$`=" .sqlvalue(@$_POST["Break\$"], false) .", `Cost`=" .sqlvalue(@$_POST["Cost"], false) .", `FromDate`=" .sqlvalue(@$_POST["FromDate"], true) .", `ToDate`=" .sqlvalue(@$_POST["ToDate"], true) ." where " ."(`MenuItemID`=" .sqlvalue(@$_POST["xMenuItemID"], false) .")";
  mysql_query($sql, $conn) or die(mysql_error());
}

function sql_delete()
{
  global $conn;
  global $_POST;

  $sql = "delete from `menuitem` where " ."(`MenuItemID`=" .sqlvalue(@$_POST["xMenuItemID"], false) .")";
  mysql_query($sql, $conn) or die(mysql_error());
} ?>
Return current item: eCanteen