Răsfoiți Sursa

fleat: products

igb 3 săptămâni în urmă
părinte
comite
4cc3d0b5af
2 a modificat fișierele cu 194 adăugiri și 36 ștergeri
  1. 1 1
      system/js/SearchArea.js
  2. 193 35
      system/products.php

+ 1 - 1
system/js/SearchArea.js

@@ -22,7 +22,7 @@ $(document).ready(function() {
 	function() {
 		var newcon = $(this).find(".cname").html();
 		var id = $(this).data("id");
-		$(".areadd").append("<li><input type='hidden' name='nosale' value='" + id + "'><span class='cname'>" + newcon + "</span><span class='close'></span></li>");
+		$(".areadd").append("<li><input type='hidden' name='nosale[]' value='" + id + "'><span class='cname'>" + newcon + "</span><span class='close'></span></li>");
 		$("#arealist").hide();
 		$("#AreaSearch").val("");
 	})

+ 193 - 35
system/products.php

@@ -11,6 +11,7 @@ $product_name = isset($_POST['ProductName']) ? htmlspecialcharsFix($_POST['Produ
 $product_img = isset($_POST['ProductImg']) ? htmlspecialcharsFix($_POST['ProductImg']) : '';
 $unit = isset($_POST['unit']) ? htmlspecialcharsFix($_POST['unit']) : '';
 $moq = isset($_POST['moq']) ? htmlspecialcharsFix($_POST['moq']) : '';
+$category_id = isset($_POST['category_id']) ? intval($_POST['category_id']) : (isset($_GET['category_id']) ? intval($_GET['category_id']) : 0);
 $nosale = isset($_POST['nosale']) ? $_POST['nosale'] : array();
 $num = isset($_POST['num']) ? $_POST['num'] : array();
 $price = isset($_POST['price']) ? $_POST['price'] : array();
@@ -42,7 +43,8 @@ if ($act == 'save') {
                 unit = '" . mysqli_real_escape_string($conn, $unit) . "',
                 nosale = '" . $nosale_str . "',
                 note = '" . mysqli_real_escape_string($conn, $note) . "',
-                tips = '" . mysqli_real_escape_string($conn, $tips) . "'
+                tips = '" . mysqli_real_escape_string($conn, $tips) . "',
+                category_id = " . $category_id . "
                 WHERE id = " . (int)$id;
         mysqli_query($conn, $sql);
         
@@ -62,16 +64,17 @@ if ($act == 'save') {
         }
     } else {
         // Insert new product
-        $sql = "INSERT INTO products (ProductName, ProductImg, Addtime, unit, moq, nosale, note, tips) 
+        $sql = "INSERT INTO products (ProductName, ProductImg, Addtime, moq, unit, nosale, note, tips, category_id) 
                 VALUES (
                     '" . mysqli_real_escape_string($conn, $product_name) . "',
                     '" . mysqli_real_escape_string($conn, $product_img) . "',
                     NOW(),
-                    '" . mysqli_real_escape_string($conn, $unit) . "',
                     '" . mysqli_real_escape_string($conn, $moq) . "',
+                    '" . mysqli_real_escape_string($conn, $unit) . "',
                     '" . $nosale_str . "',
                     '" . mysqli_real_escape_string($conn, $note) . "',
-                    '" . mysqli_real_escape_string($conn, $tips) . "'
+                    '" . mysqli_real_escape_string($conn, $tips) . "',
+                    " . $category_id . "
                 )";
         mysqli_query($conn, $sql);
         $id = mysqli_insert_id($conn);
@@ -134,6 +137,54 @@ if ($act == 'postchk') {
 <script type="text/javascript" src="js/js.js"></script>
 <script type="text/javascript" src="js/SearchArea.js"></script>
 <script type="text/javascript" src="xheditor-1.1.9/xheditor-1.1.9-zh-cn.min.js"></script>
+<style>
+    .add_btn {
+        background-color: #5cb85c;
+        color: white;
+        padding: 6px 15px;
+        border: none;
+        border-radius: 3px;
+        cursor: pointer;
+        font-weight: bold;
+    }
+    .add_btn:hover {
+        background-color: #449d44;
+    }
+    .search_panel {
+        padding: 15px;
+        border-radius: 4px;
+        border: 1px solid #ddd;
+        margin-bottom: 20px;
+        background-color: #f9f9f9;
+    }
+    .search_panel label {
+        font-weight: bold;
+        margin-right: 5px;
+    }
+    .search_panel .inputTxt {
+        width: 200px;
+        padding: 5px;
+        border: 1px solid #ccc;
+        border-radius: 3px;
+    }
+    .search_panel .searchgo {
+        padding: 5px 15px;
+        background-color: #337ab7;
+        color: white;
+        border: none;
+        border-radius: 3px;
+        cursor: pointer;
+    }
+    .search_panel .searchgo:hover {
+        background-color: #286090;
+    }
+    .select1 {
+        padding: 5px;
+        border: 1px solid #ccc;
+        border-radius: 3px;
+        min-width: 200px;
+    }
+</style>
 </head>
 
 <body>
@@ -145,11 +196,11 @@ if ($act == 'add' || $act == 'edit') {
     $is_edit = ($id > 0);
     
     if ($is_edit) {
-        $sql = "SELECT ProductName, ProductImg, unit, moq, nosale, note, tips 
-                FROM products WHERE id = " . $id;
+        $sql = "SELECT * FROM products WHERE id = " . (int)$id;
         $result = mysqli_query($conn, $sql);
-        if ($row = mysqli_fetch_assoc($result)) {
-
+        
+        if (mysqli_num_rows($result) > 0) {
+            $row = mysqli_fetch_assoc($result);
             $product_name = htmlspecialcharsFix($row['ProductName']);
             $product_img = htmlspecialcharsFix($row['ProductImg']);
             $unit = htmlspecialcharsFix($row['unit']);
@@ -157,6 +208,11 @@ if ($act == 'add' || $act == 'edit') {
             $nosale = $row['nosale'];
             $note = htmlspecialcharsFix($row['note']);
             $tips = htmlspecialcharsFix($row['tips']);
+            $category_id = $row['category_id'] ? intval($row['category_id']) : 0;
+        } else {
+            // Product not found, redirect
+            header("Location: ?Keys=" . $keys . "&Page=" . $page);
+            exit();
         }
     }
     
@@ -170,6 +226,25 @@ if ($act == 'add' || $act == 'edit') {
                     <td><input type="text" id="ProductName" name="ProductName" value="<?php echo $product_name; ?>" class="txt1" />
                     <input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
                 </tr>
+                <tr>
+                    <th width="8%">产品分类</th>
+                    <td>
+                        <select name="category_id" id="category_id" class="select1">
+                            <option value="0">-- 请选择分类 --</option>
+                            <?php
+                            // Include the category display functions
+                            require_once 'functions.php';
+                            
+                            // Build category tree
+                            $category_data = buildCategoryTree($conn);
+                            $cat_tree = $category_data['tree'];
+                            
+                            // Output options
+                            outputCategoryOptions($cat_tree, $category_id);
+                            ?>
+                        </select>
+                    </td>
+                </tr>
                 <tr>
                     <th width="8%">产品图片</th>
                     <td><input type="text" id="ProductImg" name="ProductImg" placeholder="186x*186px" value="<?php echo $product_img; ?>" class="txt1" style="width:390px;float:left;" />
@@ -234,7 +309,9 @@ if ($act == 'add' || $act == 'edit') {
                         <ul class="areadd">
                         <?php
                         if (!empty($nosale)) {
-                            $area_sql = "SELECT id, countryName FROM country WHERE id IN(" . $nosale . ")";
+                            // 确保nosale是逗号分隔的字符串格式
+                            $nosale_str = is_array($nosale) ? implode(',', $nosale) : $nosale;
+                            $area_sql = "SELECT id, countryName FROM country WHERE id IN(" . $nosale_str . ")";
                             $area_result = mysqli_query($conn, $area_sql);
                             while ($area_row = mysqli_fetch_assoc($area_result)) {
                                 ?>
@@ -274,36 +351,105 @@ if ($act == 'add' || $act == 'edit') {
     <?php
 } else {
     // Display product list
-    $sql = "SELECT id, ProductName, ProductImg FROM products ORDER BY id DESC";
-    $result = mysqli_query($conn, $sql);
-    $total_records = mysqli_num_rows($result);
-    $records_per_page = 18;
-    $total_pages = ceil($total_records / $records_per_page);
+    require_once 'functions.php';
+    
+    // Get filter category id
+    $filter_category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;
+    
+    // Prepare SQL condition for category filtering
+    $category_condition = '';
+    if ($filter_category_id > 0) {
+        // Get all subcategories of the selected category
+        $category_data = buildCategoryTree($conn);
+        $all_categories = $category_data['all_categories'];
+        
+        $category_ids = array($filter_category_id);
+        getSubcategoryIds($all_categories, $filter_category_id, $category_ids);
+        
+        $category_condition = " WHERE category_id IN (" . implode(',', $category_ids) . ")";
+    }
+    
+    // Search condition
+    $search_condition = '';
+    if (!empty($keyscode)) {
+        $search_condition = ($category_condition ? " AND " : " WHERE ") . 
+                           "ProductName LIKE '%" . mysqli_real_escape_string($conn, $keyscode) . "%'";
+    }
+    
+    // Get total records count using a COUNT query instead of fetching all records
+    $count_sql = "SELECT COUNT(*) as total FROM products" . $category_condition . $search_condition;
+    $count_result = mysqli_query($conn, $count_sql);
+    $count_row = mysqli_fetch_assoc($count_result);
+    $total_records = $count_row['total'];
+    
+    // 固定每页显示18条记录(与 customers.php 保持一致)
+    $pageSize = 18;
+    $total_pages = ceil($total_records / $pageSize);
+    if ($total_pages < 1) $total_pages = 1; // 确保至少有一页,即使没有结果
     
     // Validate page number
+    if (empty($page)) $page = 1;
     if ($page == 'end') $page = $total_pages;
     if (!is_numeric($page) || $page < 1) $page = 1;
+    $page = (int)$page;
     if ($page > $total_pages) $page = $total_pages;
     
-    $offset = ($page - 1) * $records_per_page;
-    $sql .= " LIMIT $offset, $records_per_page";
+    // Apply pagination
+    $offset = ($page - 1) * $pageSize;
+    if ($offset < 0) $offset = 0; // 确保偏移量不为负数
+    
+    // Fetch only the records for the current page
+    $sql = "SELECT id, ProductName, ProductImg, category_id FROM products" . 
+           $category_condition . $search_condition . " ORDER BY id DESC LIMIT $offset, $pageSize";
     $result = mysqli_query($conn, $sql);
+    
+    $temp_num = $pageSize * ($page - 1);
     ?>
-    <form id="form1" method="post" action="?act=postchk&Keys=<?php echo $keys; ?>&Page=<?php echo $page; ?>" onSubmit="return false">
+    <div class="search_panel">
+        <form method="get" action="">
+            <input type="hidden" name="Page" value="1">
+            <div style="display: flex; margin-bottom: 10px;">
+                <div style="margin-right: 20px;">
+                    <label>按分类筛选:</label>
+                    <select name="category_id" class="select1" onchange="this.form.submit()">
+                        <option value="0">-- 所有分类 --</option>
+                        <?php
+                        // Build category tree for filter dropdown
+                        $category_data = buildCategoryTree($conn);
+                        $cat_tree = $category_data['tree'];
+                        
+                        // Output options
+                        outputCategoryOptions($cat_tree, $filter_category_id);
+                        ?>
+                    </select>
+                </div>
+                <div>
+                    <label>关键词搜索:</label>
+                    <input type="text" name="Keys" value="<?php echo $keyscode; ?>" class="inputTxt" placeholder="请输入产品名称">
+                    <input type="submit" value="搜索" class="searchgo">
+                </div>
+                <div style="margin-left: 20px;">
+                    <input type="button" value="+ 新增产品" onClick="location.href='?act=add<?php echo $filter_category_id ? '&category_id='.$filter_category_id : ''; ?>'" class="add_btn" />
+                </div>
+            </div>
+        </form>
+    </div>
+    <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 : ''; ?>" onSubmit="return false">
         <table width="100%" border="0" cellpadding="3" cellspacing="1" class="table1">
             <thead>
                 <tr>
                     <th width="4%"><input type="checkbox" name="chkall" id="chkall" onClick="chkboxall(this,'chkbox')" /></th>
                     <th width="6%">序号</th>
-                    <th width="30%">产品名称</th>
-                    <th width="40%">图片</th>
+                    <th width="25%">产品名称</th>
+                    <th width="15%">产品分类</th>
+                    <th width="30%">图片</th>
                     <th width="20%">操作</th>
                 </tr>
             </thead>
             <tbody>
             <?php
             if (mysqli_num_rows($result) > 0) {
-                $temp_num = $offset;
+                $temp_num = $pageSize * ($page - 1);
                 while ($row = mysqli_fetch_assoc($result)) {
                     $temp_num++;
                     ?>
@@ -311,7 +457,13 @@ if ($act == 'add' || $act == 'edit') {
                         <td align="center"><input type="checkbox" name="chkbox[]" value="<?php echo $row['id']; ?>" /></td>
                         <td align="center"><?php echo $temp_num; ?></td>
                         <td align="center"><?php echo htmlspecialcharsFix($row['ProductName']); ?></td>
-                        <td align="center"><img src="<?php echo htmlspecialcharsFix($row['ProductImg']); ?>" width="80px"></td>
+                        <td align="center">
+                            <?php 
+                            require_once 'functions.php';
+                            echo getCategoryPath($conn, $row['category_id']); 
+                            ?>
+                        </td>
+                        <td align="center"><img src="<?php echo htmlspecialcharsFix($row['ProductImg']); ?>" width="30px"></td>
                         <td align="center">
                             <a href="?Keys=<?php echo $keys; ?>&Page=<?php echo $page; ?>&act=edit&id=<?php echo $row['id']; ?>" class="ico_edit ico">修改</a>
                         </td>
@@ -335,13 +487,18 @@ if ($act == 'add' || $act == 'edit') {
                         <div class="showpagebox">
                         <?php
                         if ($total_pages > 1) {
-                            $page_name = "?Keys=" . $keys . "&";
+                            // Build page URL with all parameters
+                            $page_params = array();
+                            if (!empty($keys)) $page_params[] = "Keys=" . urlencode($keys);
+                            if ($filter_category_id > 0) $page_params[] = "category_id=" . $filter_category_id;
+                            
+                            $page_name = "?" . implode("&", $page_params) . ($page_params ? "&" : "");
                             $page_len = 3;
                             
                             // Previous page links
                             if ($page > 1) {
                                 echo "<a href=\"{$page_name}Page=1\">首页</a>";
-                                echo "<a href=\"{$page_name}Page=" . ($page - 1) . "\">上一页</a>";
+                                echo "<a href=\"{$page_name}Page=" . ($page-1) . "\">上一页</a>";
                             }
                             
                             // Calculate page range
@@ -365,30 +522,31 @@ if ($act == 'add' || $act == 'edit') {
                             // Page numbers
                             for ($i = $start_page; $i <= $end_page; $i++) {
                                 if ($i == $page) {
-                                    echo "<a class=\"current\">{$i}</a>";
+                                    echo "<a class=\"current\">$i</a>";
                                 } else {
-                                    echo "<a href=\"{$page_name}Page={$i}\">{$i}</a>";
+                                    echo "<a href=\"{$page_name}Page=$i\">$i</a>";
                                 }
                             }
                             
                             // Next page links
                             if ($page < $total_pages) {
                                 if ($total_pages - $page > $page_len) {
-                                    echo "<a href=\"{$page_name}Page={$total_pages}\">...{$total_pages}</a>";
+                                    echo "<a href=\"{$page_name}Page=$total_pages\">...$total_pages</a>";
                                 }
-                                echo "<a href=\"{$page_name}Page=" . ($page + 1) . "\">下一页</a>";
-                                echo "<a href=\"{$page_name}Page={$total_pages}\">尾页</a>";
+                                echo "<a href=\"{$page_name}Page=" . ($page+1) . "\">下一页</a>";
+                                echo "<a href=\"{$page_name}Page=$total_pages\">尾页</a>";
                             }
-                            ?>
-                            <input type="text" id="Pagego" value="<?php echo $page; ?>" 
-                                onFocus="if(this.value == '<?php echo $page; ?>'){this.value='';}" 
-                                onBlur="if(this.value == ''){this.value='<?php echo $page; ?>';}" 
-                                onKeyUp="this.value=this.value.replace(/\D/g,'')" 
-                                onKeyDown="if(event.keyCode==13){location.href='<?php echo $page_name; ?>Page='+document.getElementById('Pagego').value}" />
-                            <?php
+                            
+                            // Jump to page input
+                            echo "<input type=\"text\" id=\"Pagego\" value=\"$page\" 
+                                  onFocus=\"if(this.value == '$page'){this.value='';}\" 
+                                  onBlur=\"if(this.value == ''){this.value='$page';}\" 
+                                  onKeyUp=\"this.value=this.value.replace(/\D/g,'')\" 
+                                  onKeyDown=\"if(event.keyCode==13){location.href='{$page_name}Page='+document.getElementById('Pagego').value}\" />";
                         }
                         ?>
                         </div>
+                        
                         <div class="postchkbox">
                             <select id="chkact" name="chkact">
                                 <option value="1">显示</option>