|
|
|
รบกวนผู้รู้หน่อยครับ ทำยังไงให้มันตัดสต๊อก ขอโค้ด+วิธีทำ ครับ |
|
|
|
|
|
|
|
connect.php (PHP)
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "shop";
$link = mysqli_connect($host,$user,$pass,$db);
mysqli_set_charset($link,'utf8');
?>
form.php (PHP)
<?php
require 'connect.php';
?>
<form action="process.php" method="post">
<br>
<fieldset>
<legend>สั่งซื้อสินคา</legend>
<?php
$sql="select * from product order by pro_id desc";
$result = mysqli_query($link,$sql);
?>
<table width="650">
<tr>
<th width="90">ลําดับ</th>
<th width="165">สินคา</th>
<th width="104">ราคา</th>
<th width="82">คงเหลือ</th>
<th width="81">สั่งซื้อ</th>
</tr>
<?php
$i=1;
while($row=mysqli_fetch_array($result)){
?>
<input type="hidden" name="line" value="<?=$i?>">
<tr>
<td align="center"><?php echo $i;?></td>
<td align="center"><?php echo
$row["pro_name"];?></td>
<td align="center"><?php echo
number_format($row["pro_price"],2,".",",");?></td>
<td align="center"><?php echo
$row["pro_num"];?></td>
<td align="center"><input type="checkbox"
name="pro_order[]" value="<?php echo
$row["pro_id"];?>"></td>
</tr>
<?php
$i++;
}
?>
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td colspan="6 " align="right">
<input type="submit" name="ok" value="สั่งซื้อสินคา"
id="bottom">
<input type="hidden" name="c" value="0" />
</td>
</tr>
</table>
</fieldset>
</form>
process.php (PHP)
<meta charset="utf-8">
<script language="javascript">
function checkform()
{
if(document.orderconfirm.order.value=="")
alert('กรุณากรอกตัวเลข');
}
</script>
<?php
require "connect.php";
$pro_id = $_REQUEST["line"];
$pro_order = $_REQUEST["pro_order"];
$c = $_REQUEST["c"];
$count=count($pro_order);
if($count==0){
echo "<script>alert('คุณยังไมไดเลือก
สินคา');history.back();</script>";
echo "<script>window.location='form.php';</script>";
}else{
?>
<form action="confirm.php" method="post"
name="orderconfirm">
<fieldset>
<legend>ตระกราสินคา</legend>
<table width="650">
<tr>
<th width="92">ลําดับ</th>
<th width="145">สินคา</th>
<th width="130">ราคา</th>
<th width="84">คงเหลือ</th>
<th width="175">จํานวน</th>
</tr>
<?php
for($i=0;$i<$count;$i++){
$sql="select * from product where
pro_id='".$_REQUEST["pro_order"][$i]."' order by pro_id
desc";
$result=mysqli_query($link,$sql) or
die(mysql_error());
$row=mysqli_fetch_array($result);
?>
<tr>
<td align="center"><?php echo $i+1;?></td>
<td align="center"><?php echo
$row["pro_name"];?></td>
<td align="center"><?php echo
number_format($row["pro_price"],2,".",",");?></td>
<td align="center"><?php echo
$row["pro_num"];?></td>
<td align="center">
<input type="text" name="order[]" id="check<?php
echo $i;?>" value="" placeholder="กรุณากรอกจํานวน" size="10"
onkeypress="return checkform('check<?php echo
$i;?>')"> ชิ้น
<input type="hidden" name="line"
value="<?php echo $i+1;?>">
<input type="hidden" name="pro_id[]" value="<?php
echo $_REQUEST["pro_order"][$i];?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td colspan="6 " align="right"><input
type="submit" name="ok" value="ยืนยันการสั่งซื้อ" id="bottom">
</td>
</tr>
</table>
</fieldset>
</form>
<?php } ?>
confirm.php (PHP)
<meta charset="utf-8">
<?php
require "connect.php";
$pro_id = $_REQUEST["pro_id"];
$total_order = $_REQUEST["order"];
$count=count($total_order);
?>
<fieldset>
<legend>ตระกราสินคา</legend>
<table width="650">
<tr>
<th width="92">ลําดับ</th>
<th width="145">สินคา</th>
<th width="130">ราคา</th>
<th width="175">จํานวน</th>
<th width="175">ราคารวม</th>
</tr>
<?php
$total2 = 0;
for($i=0;$i<$count;$i++){
$sql="select * from product where
pro_id='".$_REQUEST["pro_id"][$i]."' order by pro_id desc";
$result=mysqli_query($link,$sql) or
die(mysqli_error());
$row=mysqli_fetch_array($result);
if($_REQUEST["order"][$i]==""){
echo "<script>alert('กรุณากรอกจํานวนสินคา');</script>";
echo "<script>window.location='form.php';</script>";
}else if(!is_numeric($_REQUEST["order"][$i])){
echo "<script>alert('กรุณากรอกสินคาเปนตัวเลข');</script>";
echo "<script>window.location='form.php';</script>";
}else if($_REQUEST["order"][$i]>$row["pro_num"]){
echo "<script>alert('จํานวนสินคาไมพอ');</script>";
echo "<script>window.location='form.php';</script>";
}else{
$total=$row["pro_price"]*$_REQUEST["order"][$i];
$total2=$total2+$total;
?>
<tr>
<td align="center"><?php echo $i+1;?></td>
<td align="center"><?php echo
$row["pro_name"];?></td>
<td align="center"><?php echo
number_format($row["pro_price"],2,".",",");?></td>
<td align="center"><?php echo
$_REQUEST["order"][$i];?> ชิ้น</td>
<td align="center"><?php echo
number_format($total,2,".",",");?> บาท</td>
</tr>
<?php
$member="1111111";
$sql2="insert into
`order`(order_date,order_qty,mem_id,status)
values(now(),$total2,$member,'complete')";
$query2=mysqli_query($link,$sql2);
$old=mysqli_insert_id($link);
$sql3="insert into
`order_detail`(order_id,pro_id,qty)
values('$old','".$_REQUEST["pro_id"][$i]."','".$_REQUEST["o
rder"][$i]."')";
$query3=mysqli_query($link,$sql3);
$sql4="update product set pro_num=pro_num-
'".$_REQUEST["order"][$i]."' where
pro_id='".$_REQUEST["pro_id"][$i]."'";
$query4=mysqli_query($link,$sql4);
if($query3&&$query4){
echo "<script>alert('สั่งซื้อสําเร็จแลว');</script>";
echo
"<script>window.location='form.php';</script>";
}else{
echo mysqli_error();
}
}
?>
<tr>
<td colspan="6" align="right"><br></td>
</tr>
<tr>
<td colspan="6" align="center">รวมทั้งสิ้น <?php
echo number_format($total2,2,".",",");?> บาท</td>
</tr>
<?php
}
?>
</table>
</fieldset>
Tag : PHP
|
|
|
|
|
|
Date :
2016-05-04 18:36:18 |
By :
iyabest |
View :
771 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (PHP)
$sql = "select * from product order by pro_id desc";
$query = mysqli_query($link, $sql);
$row = mysqli_fetch_array($query);
foreach($row as $data) {
echo $data['pro_num'] - $count;
}
ตอบแบบคร่าวๆ นะครับ อ่านโค้ดแล้วตาลายมากๆ :D
|
|
|
|
|
Date :
2016-05-04 22:49:15 |
By :
ALTELMA |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|