|
 |
|
ต้องการ insert โดยใส่จำนวนเงินต่อ กิโลกรัม แล้วนำไปคูณน้ำหนักของสินค้าทั้งกลุ่ม เขียน php อย่างไรค่ะ |
|
 |
|
|
 |
 |
|
ขอ code คำนวณทั้งกลุ่ม ตอนนี้ยังเขียนไม่ได้คือการคำนวณ การ update ทำได้แล้วค่ะ
showcat.php แสดงรายการสินค้ากลุ่มเดียวกัน เมื่อใส่ราคาต่อกิโลกรัม จะไปที่ไฟล์ insert.php เพื่อคูณกับน้ำหนัก
Code (PHP)
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "aaaa";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM product \n"." WHERE cat_id=2;";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table>
<tr>
<th>Group</th>
<th>Code</th>
<th>Name</th>
<th>Price</th>
<th>Weight</th>
</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["cat_id"]. "</td><td>"
. $row["pro_code"]. "</td><td>"
. $row["pro_name"]. "</td><td>"
. $row["pro_price"]. "</td><td>"
. $row["pro_weight"]. "</td></tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
$conn->close();
?>
<form action="insert.php" method="post">
Price /KG: <input type="text" name="pro_pricenew"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

ไฟล์ insert.php คำนวณ insert ลงตาราง pricechange และแสดงรายการสินค้ากลุ่มเดียวกัน ที่คำนวณแล้ว และจะไปที่ไฟล์ update.php ซึ่งเขียนได้แล้ว
Code (PHP)
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "aaaa";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO pricechange (adjust_no, pro_code, pro_name, pro_price, adjust_price, adjust_status, adjust_date)
VALUES ('$adjust_no', '$pro_code', '$pro_name', '$pro_price', '$adjust_price', '$adjust_status', '$adjust_date')";
$sql = "SELECT * FROM pricechange \n"." WHERE adjust_id;";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
echo "<table>
<tr>
<th>ID</th>
<th>NO.</th>
<th>Product Code</th>
<th>Name</th>
<th>Price</th>
<th>New price</th>
<th>Status</th>
<th>Date</th>
</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr><td>" . $row["adjust_id"]. "</td><td>"
. $row["adjust_no"]. "</td><td>"
. $row["pro_code"]. "</td><td>"
. $row["pro_name"]. "</td><td>"
. $row["pro_price"]. "</td><td>"
. $row["adjust_price"]. "</td><td>"
. $row["adjust_status"]. "</td><td>"
. $row["adjust_date"]. "</td>";
}
echo "</table>";
}
else
{
echo "0 results";
}
$conn->close();
?>
<input type="submit" name="submit" value="Submit">
</body>
</html>


Tag : PHP, MySQL

|
ประวัติการแก้ไข 2022-04-15 18:29:43 2022-04-15 18:30:23 2022-04-15 18:31:55 2022-04-15 18:34:29
|
 |
 |
 |
 |
Date :
2022-04-15 18:25:37 |
By :
Vipada147 |
View :
625 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$row = $result->fetch_assoc();
$total = array_reduce(
$row,
function ($prev, $item) {
return $prev + $item['pro_price'] * $item['pro_weight'];
}
);
|
ประวัติการแก้ไข 2022-04-18 09:33:20
 |
 |
 |
 |
Date :
2022-04-18 09:32:51 |
By :
newalway |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|