Browse Source

fleat: update system country

igb 4 weeks ago
parent
commit
b64ff7c4f0
2 changed files with 86 additions and 62 deletions
  1. 80 60
      system/country.php
  2. 6 2
      system/js/Searchproduct.js

+ 80 - 60
system/country.php

@@ -27,27 +27,32 @@ if ($act == "save") {
         $isedit = true;
     }
 
-    $productId = $_POST['productId'] ?? '';
-    $num = $_POST['num'] ?? '';
-    $price = $_POST['price'] ?? '';
+    $productIds = $_POST['productId'] ?? [];
+    $nums = $_POST['num'] ?? [];
+    $prices = $_POST['price'] ?? [];
+    $countryName = $_POST['countryName'] ?? '';
+    $countryCode = $_POST['countryCode'] ?? '';
 
     if ($isedit) {
         $sql = "UPDATE country SET addtime=NOW() WHERE id=$id";
         $conn->query($sql);
         
-        $productIds = explode(',', $productId);
-        $nums = explode(',', $num);
-        $prices = explode(',', $price);
-        
+        // Delete all existing prices for this area
         $conn->query("DELETE FROM price WHERE AreaId=$id");
         
-        for ($i = 0; $i < count($productIds); $i++) {
-            $numVal = empty($nums[$i]) ? 0 : intval($nums[$i]);
-            $priceVal = empty($prices[$i]) ? 0 : $conn->real_escape_string($prices[$i]);
-            $productId = intval($productIds[$i]);
-            if ($productId > 0) {
-                $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES ($productId, $id, $numVal, '$priceVal')";
-                $conn->query($sql);
+        // Only process if we have product IDs
+        if (!empty($productIds) && is_array($productIds)) {
+            for ($i = 0; $i < count($productIds); $i++) {
+                if (isset($nums[$i]) && isset($prices[$i])) {
+                    $numVal = empty($nums[$i]) ? 0 : floatval($nums[$i]);
+                    $priceVal = empty($prices[$i]) ? 0 : $conn->real_escape_string($prices[$i]);
+                    $productId = intval($productIds[$i]);
+                    
+                    if ($productId > 0) {
+                        $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES ($productId, $id, $numVal, '$priceVal')";
+                        $conn->query($sql);
+                    }
+                }
             }
         }
 
@@ -57,21 +62,28 @@ if ($act == "save") {
         header("Location: ?keys=$keys&Ord=$ord&Page=$page");
         exit;
     } else {
-        $sql = "INSERT INTO country (addtime) VALUES (NOW())";
+        // For new country, include countryName and countryCode
+        $sql = "INSERT INTO country (countryName, countryCode, addtime) VALUES (
+            '" . $conn->real_escape_string($countryName) . "',
+            '" . $conn->real_escape_string($countryCode) . "',
+            NOW()
+        )";
         $conn->query($sql);
         $id = $conn->insert_id;
         
-        $productIds = explode(',', $productId);
-        $nums = explode(',', $num);
-        $prices = explode(',', $price);
-        
-        for ($i = 0; $i < count($productIds); $i++) {
-            $numVal = empty($nums[$i]) ? 0 : intval($nums[$i]);
-            $priceVal = empty($prices[$i]) ? 0 : $conn->real_escape_string($prices[$i]);
-            $productId = intval($productIds[$i]);
-            if ($productId > 0) {
-                $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES ($productId, $id, $numVal, '$priceVal')";
-                $conn->query($sql);
+        // Only process if we have product IDs
+        if (!empty($productIds) && is_array($productIds)) {
+            for ($i = 0; $i < count($productIds); $i++) {
+                if (isset($nums[$i]) && isset($prices[$i])) {
+                    $numVal = empty($nums[$i]) ? 0 : floatval($nums[$i]);
+                    $priceVal = empty($prices[$i]) ? 0 : $conn->real_escape_string($prices[$i]);
+                    $productId = intval($productIds[$i]);
+                    
+                    if ($productId > 0) {
+                        $sql = "INSERT INTO price (productId, AreaId, num, price) VALUES ($productId, $id, $numVal, '$priceVal')";
+                        $conn->query($sql);
+                    }
+                }
             }
         }
 
@@ -112,11 +124,11 @@ if ($act == "add" || $act == "edit") {
         <tbody>
             <tr>
                 <th width="8%">国家</th>
-                <td><input type="text" id="countryName" name="countryName" readonly value="<?= htmlspecialcharsFix($countryName) ?>" class="txt1" /><input type="hidden" name="id" value="<?= $id ?>" /></td>
+                <td><input type="text" id="countryName" name="countryName" <?= $isedit ? 'readonly' : '' ?> value="<?= htmlspecialcharsFix($countryName) ?>" class="txt1" /><input type="hidden" name="id" value="<?= $id ?>" /></td>
             </tr>
             <tr>
                 <th width="8%">区号</th>
-                <td><input type="text" id="countryCode" name="countryCode" readonly value="<?= htmlspecialcharsFix($countryCode) ?>" class="txt1" /></td>
+                <td><input type="text" id="countryCode" name="countryCode" <?= $isedit ? 'readonly' : '' ?> value="<?= htmlspecialcharsFix($countryCode) ?>" class="txt1" /></td>
             </tr>
             <tr>
                 <th width="8%">更新日期</th>
@@ -127,38 +139,46 @@ if ($act == "add" || $act == "edit") {
                 <td>
                 <div class="prowapper">             
                     <?php
-                    $sql = "SELECT id, ProductName, ProductImg, unit FROM products WHERE id IN (SELECT productId FROM price WHERE AreaId = $id)";
-                    $result = $conn->query($sql);
-                    while ($row = $result->fetch_assoc()) {
-                    ?>
-                        <div class="proitem">
-                            <div class="prodelet"></div>
-                            <div class="proname"><?= htmlspecialcharsFix($row['ProductName']) ?></div>
-                            <div class="propic"><img src="<?= htmlspecialcharsFix($row['ProductImg']) ?>"></div>      
-                            <div class="proprice">
-                                <?php
-                                $sql2 = "SELECT num, price FROM price WHERE productId = {$row['id']} AND AreaId = $id ORDER BY num ASC";
-                                $result2 = $conn->query($sql2);
-                                while ($row2 = $result2->fetch_assoc()) {
-                                ?>
-                                    <div class="priceitem">
-                                        <input type="hidden" name="productId" value="<?= $row['id'] ?>">
-                                        <label>≥</label>
-                                        <input type="number" autocomplete="off" class="txt3 num" name="num" value="<?= $row2['num'] ?>">
-                                        <label class='unit'><?= htmlspecialcharsFix($row['unit']) ?></label>
-                                        <label>售价</label>
-                                        <input type="text" class="txt3 price" autocomplete="off" name="price" value="<?= $row2['price'] ?>">
-                                        <label>RMB</label>
-                                        <span class="additem"></span>
-                                        <span class="delitem"></span>
-                                        <span class="note"></span>
-                                    </div>
-                                <?php
-                                }
-                                ?>
-                            </div>      
-                        </div>
-                    <?php
+                    if (!empty($id) && $id > 0) {
+                        $sql = "SELECT id, ProductName, ProductImg, unit FROM products WHERE id IN (SELECT productId FROM price WHERE AreaId = $id)";
+                        $result = $conn->query($sql);
+                        if ($result && $result->num_rows > 0) {
+                            while ($row = $result->fetch_assoc()) {
+                            ?>
+                                <div class="proitem">
+                                    <div class="prodelet"></div>
+                                    <div class="proname"><?= htmlspecialcharsFix($row['ProductName']) ?></div>
+                                    <div class="propic"><img src="<?= htmlspecialcharsFix($row['ProductImg']) ?>"></div>      
+                                    <div class="proprice">
+                                        <?php
+                                        $sql2 = "SELECT num, price FROM price WHERE productId = {$row['id']} AND AreaId = $id ORDER BY num ASC";
+                                        $result2 = $conn->query($sql2);
+                                        while ($row2 = $result2->fetch_assoc()) {
+                                        ?>
+                                            <div class="priceitem">
+                                                <input type="hidden" name="productId[]" value="<?= $row['id'] ?>">
+                                                <label>≥</label>
+                                                <input type="number" autocomplete="off" class="txt3 num" name="num[]" value="<?= $row2['num'] ?>">
+                                                <label class='unit'><?= htmlspecialcharsFix($row['unit']) ?></label>
+                                                <label>售价</label>
+                                                <input type="text" class="txt3 price" autocomplete="off" name="price[]" value="<?= $row2['price'] ?>">
+                                                <label>RMB</label>
+                                                <span class="additem"></span>
+                                                <span class="delitem"></span>
+                                                <span class="note"></span>
+                                            </div>
+                                        <?php
+                                        }
+                                        ?>
+                                    </div>      
+                                </div>
+                            <?php
+                            }
+                        } else {
+                            echo "<p>该地区暂无产品价格设置</p>";
+                        }
+                    } else {
+                        echo "<p>添加产品后可管理产品价格</p>";
                     }
                     ?>
                 </div>                  

+ 6 - 2
system/js/Searchproduct.js

@@ -24,7 +24,7 @@ $(document).ready(function() {
 		var unit = $(this).data("unit");
 		var n = "<div class='proname'>" + $(this).find(".name").html() + "</div>";
 		var pic = "<div class='propic'>" + $(this).find(".pic").html() + "</div>";
-		var item = "<div class='proitem'><div class='prodelet'></div>" + n + pic + "<div class='proprice'><div class='priceitem'><input type='hidden' name='productId' value="+id+"><label>≥</label><input type='number' autocomplete='off' class='txt3 num' name='num'><label class='unit'>" + unit + "</label> <label>售价</label><input type='text' class='txt3 price' autocomplete='off' name='price'><label>RMB</label> <span class='additem'></span><span class='delitem'></span><span class='note'></span></div></div></div>";
+		var item = "<div class='proitem'><div class='prodelet'></div>" + n + pic + "<div class='proprice'><div class='priceitem'><input type='hidden' name='productId[]' value="+id+"><label>≥</label><input type='number' autocomplete='off' class='txt3 num' name='num[]'><label class='unit'>" + unit + "</label> <label>售价</label><input type='text' class='txt3 price' autocomplete='off' name='price[]'><label>RMB</label> <span class='additem'></span><span class='delitem'></span><span class='note'></span></div></div></div>";
 		$(".prowapper").append(item);
 		$("#productlist").hide();
 		$("#productSearch").val("");
@@ -41,12 +41,16 @@ $(document).ready(function() {
 		var i = $(this).parent().index();
 		var lastnum = $(".priceitem").eq(i).find(".num").val();
 		var lastprice = $(".priceitem").eq(i).find(".price").val();
+		var productId = $(this).parent().find("input[name='productId[]']").val();
+		
 		if (lastnum == "" || lastprice == "") //未输入无法继续添加
 		{
 			return false
 		} else {
+			// Make sure cloned item has array notation for field names
+			priceitem.find("input[name='productId[]']").val(productId);
 			priceitem.find(".num").val("");
-			priceitem.find(".price").val("");			
+			priceitem.find(".price").val("");
 			$(this).parent().after(priceitem);
 		}
 	})