可以让这段代码变得更好。这个购物车数据发布了如何插入到数据库它运作良好,但有限和庞大的代码谁可以缩短? 我认为这段代码将超过5 $ x ++。智能和短代码如何做更多的功能。
$ina_1 = $_POST['item_name_1']; $iq_1 = $_POST['quantity_1']; $ipr_1 = $_POST['amount_1'];$qp_1= $iq_1*$ipr_1;
$ina_2 = $_POST['item_name_2']; $iq_2 = $_POST['quantity_2']; $ipr_2 = $_POST['amount_2'];$qp_2= $iq_2*$ipr_2;
$ina_3 = $_POST['item_name_3']; $iq_3 = $_POST['quantity_3']; $ipr_3 = $_POST['amount_3'];$qp_3= $iq_3*$ipr_3;
$ina_4 = $_POST['item_name_4']; $iq_4 = $_POST['quantity_4']; $ipr_4 = $_POST['amount_4'];$qp_4= $iq_4*$ipr_4;
$ina_5 = $_POST['item_name_5']; $iq_5 = $_POST['quantity_5']; $ipr_5 = $_POST['amount_5'];$qp_5= $iq_5*$ipr_5;
$shipping = $_POST['shipping'];
$tprice = $qp_1+$qp_2+$qp_3+$qp_4+$qp_5+$qp_6+$qp_7+$qp_8+$qp_9+$shipping;
if(empty($idu)){
$guestid = rand(111111,999999);
}
$orderid = rand(111111,999999);
preg_match_all('!\d+!', $ina_1, $matches); $look1= $matches[0]; $sl_1 = $look1[0];
preg_match_all('!\d+!', $ina_2, $matches); $look2= $matches[0]; $sl_2 = $look2[0];
preg_match_all('!\d+!', $ina_3, $matches); $look3= $matches[0]; $sl_3 = $look3[0];
preg_match_all('!\d+!', $ina_4, $matches); $look4= $matches[0]; $sl_4 = $look4[0];
preg_match_all('!\d+!', $ina_5, $matches); $look5= $matches[0]; $sl_5 = $look5[0];
mysql_query("INSERT INTO d_order
(`userid`,`orderid`,`sellerid`,`item`,`quantity`,`amount`,`total`,`tprice`,`date`,`status`)
VALUES ('$idu','$orderid','$sl_1','$ina_1','$iq_1','$ipr_1','$qp_1','$tprice','$now','0')") or die(mysql_error());
if(!empty($ina_2)){
mysql_query("INSERT INTO d_order
(`userid`,`orderid`,`sellerid`,`item`,`quantity`,`amount`,`total`,`tprice`,`date`,`status`)
VALUES ('$idu','$orderid','$sl_2','$ina_2','$iq_2','$ipr_2','$qp_2','$tprice','$now','0')") or die(mysql_error());
}
if(!empty($ina_3)){
mysql_query("INSERT INTO d_order
(`userid`,`orderid`,`sellerid`,`item`,`quantity`,`amount`,`total`,`tprice`,`date`,`status`)
VALUES ('$idu','$orderid','$sl_3','$ina_3','$iq_3','$ipr_3','$qp_3','$tprice','$now','0')") or die(mysql_error());
}
if(!empty($ina_4)){
mysql_query("INSERT INTO d_order
(`userid`,`orderid`,`sellerid`,`item`,`quantity`,`amount`,`total`,`tprice`,`date`,`status`)
VALUES ('$idu','$orderid','$sl_4','$ina_4','$iq_4','$ipr_4','$qp_4','$tprice','$now','0')") or die(mysql_error());
}
if(!empty($ina_5)){
mysql_query("INSERT INTO d_order
(`userid`,`orderid`,`sellerid`,`item`,`quantity`,`amount`,`total`,`tprice`,`date`,`status`)
VALUES ('$idu','$orderid','$sl_5','$ina_5','$iq_5','$ipr_5','$qp_5','$tprice','$now','0')") or die(mysql_error());
}
答案 0 :(得分:0)
完全未经测试但我认为,通过一些调整,以下内容可能会为您简化。
<?php
$max_number_products=5;
$shipping = $_POST['shipping'];
$cart = array();
for( $i=1; $i <= $max_number_products; $i++ ){
$name=$_POST[ 'item_name_'.$i ];
$qty=$_POST[ 'quantity_'.$i ];
$amount=$_POST[ 'amount_'.$i ];
$price = abs( $qty * $amount );
$cart[]=array(
'name' => $name,
'amount' => $amount,
'quantity' => $qty,
'price' => $price
);
$cart['total']+=$price;
}
$tprice = $cart['total'] + $shipping;
/*
what is $idu? where is it used?
Should it not be $guestid in the
sql query not $idu which MAY be empty!
*/
if( empty( $idu ) ) $guestid = rand( 111111, 999999 );
$orderid = rand( 111111, 999999 );
foreach( $cart as $key => $cartitem ){
preg_match_all( '!\d+!', $cartitem['name'], $matches );
$look1 = $matches[0];
$sl_1 = $look1[0];
$sql="INSERT INTO `d_order`
( `userid`, `orderid`, `sellerid`, `item`, `quantity`, `amount`, `total`, `tprice`, `date`, `status`) VALUES
( '$idu', '$orderid', '$sl_1', '{$cartitem['name']}', '{$cartitem['quantity']}', '{$cartitem['amount']}', '{$cartitem['price']}', '$tprice', now(), '0')";
echo '<pre>',$sql,'</pre>';
/*
mysql_query( $sql );
*/
}
?>
答案 1 :(得分:-1)
你应该使用循环语句。如果您可以获得购物车中的物品数量,那么做这样的事情可能有所帮助。
$curr_item = 0; // and $tot_item is the total number of items.
while($curr_item<$tot_item)
{
$itemlist['curr_item'.$curr_item] = $_POST['item_name_'.$curr_item];
$curr_item++;
}
我认为,您也可以将相同的概念用于其他条件语句。