products.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. require_once('conn.php');
  3. // Check login status (assuming you have a similar function in PHP)
  4. checkLogin("信息管理");
  5. // Initialize all variables to avoid undefined warnings
  6. $act = isset($_GET['act']) ? $_GET['act'] : '';
  7. $page = isset($_GET['Page']) ? $_GET['Page'] : 1;
  8. $keys = isset($_GET['Keys']) ? urlencode($_GET['Keys']) : '';
  9. $keyscode = isset($_GET['Keys']) ? htmlspecialcharsFix($_GET['Keys']) : '';
  10. $category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;
  11. // Handle form submissions and redirects before any output
  12. if ($act == 'add') {
  13. // Redirect to the add product page
  14. header("Location: add_product.php?Keys=" . $keys . "&Page=" . $page . ($category_id ? "&category_id=" . $category_id : ""));
  15. exit();
  16. } else if ($act == 'edit') {
  17. $id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
  18. if ($id > 0) {
  19. // Redirect to the edit product page
  20. header("Location: edit_product.php?id=" . $id . "&Keys=" . $keys . "&Page=" . $page . ($category_id ? "&category_id=" . $category_id : ""));
  21. exit();
  22. }
  23. }
  24. // Handle bulk actions
  25. if ($act == 'postchk') {
  26. if (isset($_POST['chkbox']) && isset($_POST['chkact'])) {
  27. $chk_ids = array_map('intval', $_POST['chkbox']);
  28. $chk_act = (int)$_POST['chkact'];
  29. if (!empty($chk_ids)) {
  30. $ids_str = implode(',', $chk_ids);
  31. switch ($chk_act) {
  32. case 0:
  33. case 1:
  34. $sql = "UPDATE customer SET cs_state = " . $chk_act . " WHERE id IN (" . $ids_str . ")";
  35. break;
  36. case -1:
  37. $sql = "DELETE FROM products WHERE id IN (" . $ids_str . ")";
  38. break;
  39. }
  40. if (isset($sql)) {
  41. mysqli_query($conn, $sql);
  42. }
  43. }
  44. header("Location: ?Keys=" . $keys . "&Page=" . $page . ($category_id ? "&category_id=" . $category_id : ""));
  45. exit();
  46. }
  47. }
  48. ?>
  49. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  50. <html xmlns="http://www.w3.org/1999/xhtml">
  51. <head>
  52. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  53. <title>产品信息管理</title>
  54. <link rel="stylesheet" href="css/common.css" type="text/css" />
  55. <script language="javascript" src="js/jquery-1.7.2.min.js"></script>
  56. <script type="text/javascript" src="js/js.js"></script>
  57. <script type="text/javascript" src="js/SearchArea.js"></script>
  58. <script type="text/javascript" src="xheditor-1.1.9/xheditor-1.1.9-zh-cn.min.js"></script>
  59. <style>
  60. .add_btn {
  61. background-color: #5cb85c;
  62. color: white;
  63. padding: 6px 15px;
  64. border: none;
  65. border-radius: 3px;
  66. cursor: pointer;
  67. font-weight: bold;
  68. }
  69. .add_btn:hover {
  70. background-color: #449d44;
  71. }
  72. .search_panel {
  73. padding: 15px;
  74. border-radius: 4px;
  75. border: 1px solid #ddd;
  76. margin-bottom: 20px;
  77. }
  78. .search_panel label {
  79. font-weight: bold;
  80. margin-right: 5px;
  81. }
  82. .search_panel .inputTxt {
  83. width: 200px;
  84. padding: 5px;
  85. border: 1px solid #ccc;
  86. border-radius: 3px;
  87. }
  88. .search_panel .searchgo {
  89. padding: 5px 15px;
  90. background-color: #337ab7;
  91. color: white;
  92. border: none;
  93. border-radius: 3px;
  94. cursor: pointer;
  95. }
  96. .search_panel .searchgo:hover {
  97. background-color: #286090;
  98. }
  99. .select1 {
  100. padding: 5px;
  101. border: 1px solid #ccc;
  102. border-radius: 3px;
  103. min-width: 200px;
  104. }
  105. </style>
  106. </head>
  107. <body>
  108. <div id="man_zone">
  109. <?php
  110. // Display product list
  111. require_once 'functions.php';
  112. // Get filter category id
  113. $filter_category_id = $category_id;
  114. // Prepare SQL condition for category filtering
  115. $category_condition = '';
  116. if ($filter_category_id > 0) {
  117. // Get all subcategories of the selected category
  118. $category_data = buildCategoryTree($conn);
  119. $all_categories = $category_data['all_categories'];
  120. $category_ids = array($filter_category_id);
  121. getSubcategoryIds($all_categories, $filter_category_id, $category_ids);
  122. $category_condition = " WHERE category_id IN (" . implode(',', $category_ids) . ")";
  123. }
  124. // Search condition
  125. $search_condition = '';
  126. if (!empty($keyscode)) {
  127. $search_condition = ($category_condition ? " AND " : " WHERE ") .
  128. "ProductName LIKE '%" . mysqli_real_escape_string($conn, $keyscode) . "%'";
  129. }
  130. // Get total records count using a COUNT query instead of fetching all records
  131. $count_sql = "SELECT COUNT(*) as total FROM products" . $category_condition . $search_condition;
  132. $count_result = mysqli_query($conn, $count_sql);
  133. $count_row = mysqli_fetch_assoc($count_result);
  134. $total_records = $count_row['total'];
  135. // 固定每页显示18条记录(与 customers.php 保持一致)
  136. $pageSize = 18;
  137. $total_pages = ceil($total_records / $pageSize);
  138. if ($total_pages < 1) $total_pages = 1; // 确保至少有一页,即使没有结果
  139. // Validate page number
  140. if (empty($page)) $page = 1;
  141. if ($page == 'end') $page = $total_pages;
  142. if (!is_numeric($page) || $page < 1) $page = 1;
  143. $page = (int)$page;
  144. if ($page > $total_pages) $page = $total_pages;
  145. // Apply pagination
  146. $offset = ($page - 1) * $pageSize;
  147. if ($offset < 0) $offset = 0; // 确保偏移量不为负数
  148. // Fetch only the records for the current page
  149. $sql = "SELECT id, ProductName, ProductImg, category_id FROM products" .
  150. $category_condition . $search_condition . " ORDER BY id DESC LIMIT $offset, $pageSize";
  151. $result = mysqli_query($conn, $sql);
  152. $temp_num = $pageSize * ($page - 1);
  153. ?>
  154. <div class="search_panel">
  155. <form method="get" action="">
  156. <input type="hidden" name="Page" value="1">
  157. <div style="display: flex; margin-bottom: 10px;style="margin-left: 20px;"">
  158. <div style="margin-right: 20px;">
  159. <input type="button" value="+ 新增产品" onClick="location.href='?act=add<?php echo $filter_category_id ? '&category_id='.$filter_category_id : ''; ?>'" class="add_btn" />
  160. </div>
  161. <div style="margin-right: 20px;">
  162. <label>按分类筛选:</label>
  163. <select name="category_id" class="select1" onchange="this.form.submit()">
  164. <option value="0">-- 所有分类 --</option>
  165. <?php
  166. // Build category tree for filter dropdown
  167. $category_data = buildCategoryTree($conn);
  168. $cat_tree = $category_data['tree'];
  169. // Output options
  170. outputCategoryOptions($cat_tree, $filter_category_id);
  171. ?>
  172. </select>
  173. </div>
  174. <div>
  175. <label>关键词搜索:</label>
  176. <input type="text" name="Keys" value="<?php echo $keyscode; ?>" class="inputTxt" placeholder="请输入产品名称">
  177. <input type="submit" value="搜索" class="searchgo">
  178. </div>
  179. </div>
  180. </form>
  181. </div>
  182. <form id="form1" method="post" action="?act=postchk&Keys=<?php echo $keys; ?>&Page=<?php echo $page; ?><?php echo $filter_category_id ? '&category_id='.$filter_category_id : ''; ?>">
  183. <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
  184. <thead>
  185. <tr>
  186. <th width="4%"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox')" /></th>
  187. <th width="6%">序号</th>
  188. <th width="25%">产品名称</th>
  189. <th width="15%">产品分类</th>
  190. <th width="30%">图片</th>
  191. <th width="20%">操作</th>
  192. </tr>
  193. </thead>
  194. <tbody>
  195. <?php
  196. if (mysqli_num_rows($result) > 0) {
  197. $temp_num = $pageSize * ($page - 1);
  198. while ($row = mysqli_fetch_assoc($result)) {
  199. $temp_num++;
  200. ?>
  201. <tr onMouseOver="this.style.background='#F7FCFF'" onMouseOut="this.style.background='#FFFFFF'">
  202. <td align="center"><input type="checkbox" name="chkbox[]" value="<?php echo $row['id']; ?>" /></td>
  203. <td align="center"><?php echo $temp_num; ?></td>
  204. <td align="center"><?php echo htmlspecialcharsFix($row['ProductName']); ?></td>
  205. <td align="center">
  206. <?php
  207. require_once 'functions.php';
  208. echo getCategoryPath($conn, $row['category_id']);
  209. ?>
  210. </td>
  211. <td align="center"><img src="<?php echo htmlspecialcharsFix($row['ProductImg']); ?>" width="30px"></td>
  212. <td align="center">
  213. <a href="?Keys=<?php echo $keys; ?>&Page=<?php echo $page; ?>&act=edit&id=<?php echo $row['id']; ?><?php echo $filter_category_id ? '&category_id='.$filter_category_id : ''; ?>" class="ico_edit ico">修改</a>
  214. </td>
  215. </tr>
  216. <?php
  217. }
  218. } else {
  219. ?>
  220. <tr>
  221. <td colspan="9" align="center">
  222. <?php echo empty($keys) ? 'Sorry,当前暂无信息' : '<a href="?">Sorry,没有找到"' . htmlspecialcharsFix($keyscode) . '"相关的信息,点击返回</a>'; ?>
  223. </td>
  224. </tr>
  225. <?php
  226. }
  227. ?>
  228. </tbody>
  229. <tfoot>
  230. <tr>
  231. <td colspan="9">
  232. <div class="showpagebox">
  233. <?php
  234. if ($total_pages > 1) {
  235. // Build page URL with all parameters
  236. $page_params = array();
  237. if (!empty($keys)) $page_params[] = "Keys=" . urlencode($keys);
  238. if ($filter_category_id > 0) $page_params[] = "category_id=" . $filter_category_id;
  239. $page_name = "?" . implode("&", $page_params) . ($page_params ? "&" : "");
  240. $page_len = 3;
  241. // Previous page links
  242. if ($page > 1) {
  243. echo "<a href=\"{$page_name}Page=1\">首页</a>";
  244. echo "<a href=\"{$page_name}Page=" . ($page-1) . "\">上一页</a>";
  245. }
  246. // Calculate page range
  247. if ($page_len * 2 + 1 >= $total_pages) {
  248. $start_page = 1;
  249. $end_page = $total_pages;
  250. } else {
  251. if ($page <= $page_len + 1) {
  252. $start_page = 1;
  253. $end_page = $page_len * 2 + 1;
  254. } else {
  255. $start_page = $page - $page_len;
  256. $end_page = $page + $page_len;
  257. }
  258. if ($page + $page_len > $total_pages) {
  259. $start_page = $total_pages - $page_len * 2;
  260. $end_page = $total_pages;
  261. }
  262. }
  263. // Page numbers
  264. for ($i = $start_page; $i <= $end_page; $i++) {
  265. if ($i == $page) {
  266. echo "<a class=\"current\">$i</a>";
  267. } else {
  268. echo "<a href=\"{$page_name}Page=$i\">$i</a>";
  269. }
  270. }
  271. // Next page links
  272. if ($page < $total_pages) {
  273. if ($total_pages - $page > $page_len) {
  274. echo "<a href=\"{$page_name}Page=$total_pages\">...$total_pages</a>";
  275. }
  276. echo "<a href=\"{$page_name}Page=" . ($page+1) . "\">下一页</a>";
  277. echo "<a href=\"{$page_name}Page=$total_pages\">尾页</a>";
  278. }
  279. // Jump to page input
  280. echo "<input type=\"text\" id=\"Pagego\" value=\"$page\"
  281. onFocus=\"if(this.value == '$page'){this.value='';}\"
  282. onBlur=\"if(this.value == ''){this.value='$page';}\"
  283. onKeyUp=\"this.value=this.value.replace(/\D/g,'')\"
  284. onKeyDown=\"if(event.keyCode==13){location.href='{$page_name}Page='+document.getElementById('Pagego').value}\" />";
  285. }
  286. ?>
  287. </div>
  288. <div class="postchkbox">
  289. <select id="chkact" name="chkact">
  290. <option value="-1">删除</option>
  291. </select>
  292. <input type="button" value="执行" onClick="postchk_products(1)" class="btn1" />
  293. <input type="button" value="新增" onClick="location.href='?act=add<?php echo $filter_category_id ? '&category_id='.$filter_category_id : ''; ?>'" class="btn1" />
  294. </div>
  295. </td>
  296. </tr>
  297. </tfoot>
  298. </table>
  299. </form>
  300. </div>
  301. <script type="application/javascript">
  302. function postchk_products(formBtn) {
  303. // Check if at least one checkbox is selected
  304. var checkboxes = document.getElementsByName('chkbox[]');
  305. var isChecked = false;
  306. for (var i = 0; i < checkboxes.length; i++) {
  307. if (checkboxes[i].checked) {
  308. isChecked = true;
  309. break;
  310. }
  311. }
  312. if (!isChecked) {
  313. alert('请至少选择一条数据');
  314. return false;
  315. }
  316. // If at least one checkbox is selected, submit the form
  317. document.getElementById('form1').submit();
  318. return true;
  319. }
  320. </script>
  321. </body>
  322. </html>
  323. <?php mysqli_close($conn); ?>