2014-11-20 61 views
0

我在加入購物車時遇到了一些麻煩。我需要購物車能夠添加物品數量的總和,然後在訂單中添加總計數量,然後最後單擊一個地方訂單按鈕,以便能夠訂購購物車內的所有物品。如何在購物車中添加總量並在點擊購物車的地方下單?

目前我可以更改數量,但必須選擇產品編號,然後選擇一個數量,而不是在購物車的每個商品附近應該有一個小框,以便我可以輸入數量。

然後,我需要在底部添加所有項目的總計總數和點擊時的訂單按鈕按鈕應該一次訂購購物車中的所有物品。

目前我必須點擊每個項目的下訂單才能下訂單,所以我必須前後一步,這不是正確的做法。

請幫助我,我仍然是一個學習者。

在此先感謝 我將提供代碼。

<?php 
//Start session 
session_start(); 

$totalAll = 0; 

//Include session details 
require_once('auth.php'); 

//Include database connection details 
require_once('connection/config.php'); 

//Connect to mysql server 
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
if(!$link) { 
    die('Failed to connect to server: ' . mysql_error()); 
} 

//Select database 
$db = mysql_select_db(DB_DATABASE); 
if(!$db) { 
    die("Unable to select database"); 
} 

//Function to sanitize values received from the form. Prevents SQL injection 
function clean($str) { 
    $str = @trim($str); 
    if(get_magic_quotes_gpc()) { 
     $str = stripslashes($str); 
    } 
    return mysql_real_escape_string($str); 
} 

//checks if id is set in the url 
if(isset($_GET['id'])){ 
    //retrive the first quantity from the quantities table 
    $quantities=mysql_query("SELECT * FROM quantities") 
    or die("Something is wrong ... \n" . mysql_error()); 
    $row=mysql_fetch_assoc($quantities); 
    $quantity_value = $row['quantity_value']; 

    //get id value 
    $food_id = $_GET['id']; 

    //retrive food_price from food_details based on $food_id 
    $result=mysql_query("SELECT * FROM food_details WHERE food_id='$food_id'") or die("A problem has occured ... \n" . "Our team is working on it at the moment ... \n" . "Please check back after few hours."); 
    $food_row=mysql_fetch_assoc($result); 
    $food_price=$food_row['food_price']; 

    //get member_id from session 
    $member_id = $_SESSION['SESS_MEMBER_ID']; 

    //define default values for quantity(got from $row), total($food_price*$quantity_value), and flag_0 
    $quantity_id = $row['quantity_id']; 
    $total = $food_price*$quantity_value; 
    $flag_0 = 0; 


    //Create INSERT query 
    $qry = "INSERT INTO cart_details(member_id, food_id, quantity_id, total, flag) VALUES('$member_id','$food_id','$quantity_id','$total','$flag_0')"; 
    $result = @mysql_query($qry); 

    //Check whether the query was successful or not 
    if($result) { 
     header("location: cart.php"); 
     exit(); 
    }else { 
     die("A problem has occured with the system " . mysql_error()); 
    } 
} 
?> 
+0

我不能完全確定你想要什麼,但您的第一句話我會猜測您需要在代碼中總結數量和價格的循環。在你的代碼示例中沒有這樣的循環。這樣的循環看起來像這樣:while($ row = mysql_fetch_array($ result)){/ *添加一個變量* /} – CodingYourLife 2014-11-20 20:46:35

+0

我的意思是例如:£2.50然後選擇一個選項,如2這將使該物品的總價值爲5.00英鎊,然後我需要購物車中所有物品的總計 – WebProgrammer 2014-11-20 20:59:58

回答

0

您的代碼目前只從網址中獲取一個ID,但爲了計算整個購物車的總計,您需要購物車中的所有ID。通常他們會被保存到$ _SESSION。您可以使用以下代碼查看會話內部:var_dump($ _ SESSION);也有可能將ID保存到數據庫中某種購物車表格。在你的代碼中,我不明白爲什麼你只是從數量表中獲得第一項。我很確定桌子連接到食物表(我想通過food_id)。

如果您從會議獲得的ID代碼看起來應該是這樣的(不是因爲SQL語句的testet):

<?php 
$member_id = $_SESSION['SESS_MEMBER_ID']; //get member_id from session before the loop 

$food_ids = array($id1, $id2, $id3); //you need all $food_id added to cart instead of just 1 from url, usally saved to $_SESSION 

$grandTotal = 0; 
foreach ($food_ids as $food_id) { //LOOP THROUGH IDs 
    //retrive quantity of food in cart from the quantities table 
    $quantities=mysql_query("SELECT * FROM quantities WHERE food_id='$food_id'") //there must be a connection between quantities and food so I added food_id="xxx" 
    or die("Something is wrong ... \n" . mysql_error()); 
    $row=mysql_fetch_assoc($quantities); 
    $quantity_value = $row['quantity_value']; 

    //retrive food_price from food_details based on $food_id 
    $result=mysql_query("SELECT * FROM food_details WHERE food_id='$food_id'") 
    or die("A problem has occured ... \n" . "Our team is working on it at the moment ... \n" . "Please check back after few hours."); 
    $food_row=mysql_fetch_assoc($result); 
    $food_price=$food_row['food_price']; 


    //define default values for quantity(got from $row), total($food_price*$quantity_value), and flag_0 
    $quantity_id = $row['quantity_id']; 
    $total = $food_price*$quantity_value; 
    $grandTotal = $grandTotal + $total; //add total of this product to the $grandTotal from last time inside this loop 
    $flag_0 = 0; 


    //Create INSERT query 
    $qry = "INSERT INTO cart_details(member_id, food_id, quantity_id, total, flag) VALUES('$member_id','$food_id','$quantity_id','$total','$flag_0')"; 
    $result = @mysql_query($qry); 

    //Check whether the query was successful or not 
    if($result) { 
     header("location: cart.php"); 
     exit(); 
    }else { 
     die("A problem has occured with the system " . mysql_error()); 
    } 
} /*Now you have $grandTotal calculated*/?>