<?php require_once('Connections/food.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO typefood (t_name) VALUES (%s)",
                       GetSQLValueString($_POST['t_name'], "text"));

  mysql_select_db($database_food, $food);
  $Result1 = mysql_query($insertSQL, $food) or die(mysql_error());
}

$maxRows_type = 5;
$pageNum_type = 0;
if (isset($_GET['pageNum_type'])) {
  $pageNum_type = $_GET['pageNum_type'];
}
$startRow_type = $pageNum_type * $maxRows_type;

mysql_select_db($database_food, $food);
$query_type = "SELECT * FROM typefood";
$query_limit_type = sprintf("%s LIMIT %d, %d", $query_type, $startRow_type, $maxRows_type);
$type = mysql_query($query_limit_type, $food) or die(mysql_error());
$row_type = mysql_fetch_assoc($type);

if (isset($_GET['totalRows_type'])) {
  $totalRows_type = $_GET['totalRows_type'];
} else {
  $all_type = mysql_query($query_type);
  $totalRows_type = mysql_num_rows($all_type);
}
$totalPages_type = ceil($totalRows_type/$maxRows_type)-1;

$queryString_type = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_type") == false && 
        stristr($param, "totalRows_type") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_type = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_type = sprintf("&totalRows_type=%d%s", $totalRows_type, $queryString_type);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>บันทึกประเภทอาหาร</title>
<style type="text/css">
body,td,th {
	font-family: "Angsana New";
	font-size: 16pt;
}
body {
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
}
</style>
<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />
</head>

<body>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center" valign="middle"><img src="pic/banner.png" width="1000" height="274" /></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><ul id="MenuBar1" class="MenuBarHorizontal">
      <li><a href="index.php">หน้าแรก</a>        </li>
      <li><a href="#" class="MenuBarItemSubmenu">บันทึกข้อมูล</a>
        <ul>
          <li><a href="add_typefood.php">บันทึกประเภทอาหาร</a></li>
          <li><a href="add_food.php">บันทึกอาหาร</a></li>
        </ul>
      </li>
      <li><a class="MenuBarItemSubmenu" href="#">ค้นหาข้อมูล</a>
        <ul>
<li><a href="search_typefood.php">ค้นหาประเภทอาหาร</a></li>
          <li><a href="search_food.php">ค้นหาอาหาร</a></li>
        </ul>
      </li>
</ul></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><p><img src="pic/add.png" alt="" width="119" height="119" /></p>
      <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
        <table width="600" align="center">
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">ประเภทอาหาร :</td>
            <td><input type="text" name="t_name" value="" size="32" /></td>
          </tr>
          <tr valign="baseline">
            <td nowrap="nowrap" align="right">&nbsp;</td>
            <td><input type="submit" value="บันทึกข้อมูล" onclick="return confirm('คุณต้องการบันทึกข้อมูลหรือไม่')"/></td>
          </tr>
        </table>
        <input type="hidden" name="MM_insert" value="form1" />
      </form>
      <p>&nbsp;</p>
      <table width="700" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="169" align="center" valign="middle" bgcolor="#99FFCC"><strong>รหัสประเภทอาหาร</strong></td>
          <td width="341" align="center" valign="middle" bgcolor="#99FFCC"><strong>ประเภทอาหาร</strong></td>
          <td colspan="2" align="center" valign="middle" bgcolor="#99FFCC"><strong>จัดการระบบ</strong></td>
        </tr>
        <?php do { ?>
          <tr>
            <td align="center" valign="middle"><?php echo $row_type['t_id']; ?></td>
            <td align="center" valign="middle"><?php echo $row_type['t_name']; ?></td>
            <td width="95" align="center" valign="middle"><a href="edit_typefood.php?t_id=<?php echo $row_type['t_id']; ?>"><img src="pic/edit.png" width="35" height="35" /></a></td>
            <td width="95" align="center" valign="middle"><a href="delete_typefood.php?t_id=<?php echo $row_type['t_id']; ?>"><img src="pic/delete.png" width="29" height="35" /></a></td>
          </tr>
          <?php } while ($row_type = mysql_fetch_assoc($type)); ?>
      </table>
      <p>| <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, 0, $queryString_type); ?>">หน้าแรก</a> | <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, max(0, $pageNum_type - 1), $queryString_type); ?>">ก่อนหน้า</a> | <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, min($totalPages_type, $pageNum_type + 1), $queryString_type); ?>">หน้าถัดไป</a> | <a href="<?php printf("%s?pageNum_type=%d%s", $currentPage, $totalPages_type, $queryString_type); ?>">หน้าสุดท้าย</a> |</p>
    <p>&nbsp;</p></td>
  </tr>
  <tr>
    <td align="center" valign="middle"><img src="pic/footer.png" width="1000" height="135" /></td>
  </tr>
</table>
<script type="text/javascript">
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
</script>
</body>
</html>
<?php
mysql_free_result($type);
?>
