• 沒有找到結果。

購物車程式碼

在文檔中 FU網路商店行銷 (頁 23-30)

來網站逛的顧客可因喜歡的產品放置購物車裡,可在挑選 其他有興趣的產品 ,不讓因產品位置不同而尋找很久,

讓費您寶貴的時間。

購物車程式碼

<?php

session_start();

//header("Content-Type: text/html; charset=big5");

include("../connMysql.php");

$seldb = @mysql_select_db($DB);

if (!$seldb) die("資料庫選擇失敗!");

// 會員帳號

if (isset($_SESSION["login_id"])) {

$member_acc = $_SESSION["login_id"];

} else {

// 尚未登入 (訪客)

$member_acc = "visiter";

}

// 刪除購物車的商品 by terry0412 if (isset($_POST["remove-item"])) { // 流水號

$key_id = $_POST["remove-item"];

$sql_query = "DELETE FROM shoppingcart where

member_acc = '$member_acc' and key_id = '$key_id'";

mysql_query($sql_query);

}

// 加入購物車 by terry0412

else if (isset($_POST["item_id"])) {

$item_id = $_POST["item_id"];

// 檢查是否已經有重複的資料

$sql_query2 = "SELECT * FROM shoppingcart where

member_acc = '$member_acc' and item_id = '$item_id'";

if (mysql_num_rows(mysql_query($sql_query2)) > 0) { // 加入商品數量

mysql_query("UPDATE shoppingcart SET item_count = item_count + 1 where member_acc = '$member_acc' and item_id = '$item_id'");

} else {

// 新增購物車商品

mysql_query("INSERT INTO shoppingcart(member_acc, item_id, item_count) values('$member_acc', '$item_id', '1')");

}

echo "<script>window.alert('已加入到購物車

~~~');</script>";

}

// 更新購物車 by terry0412

else if (isset($_POST["update-item"])) { // 商品流水號

$key_id = $_POST["update-item"];

// 商品數量

$item_count = $_POST["$key_id"];

// 刪除資料

if ($item_count <= 0) {

$sql_query = "DELETE FROM shoppingcart where

member_acc = '$member_acc' and key_id = '$key_id'";

mysql_query($sql_query);

}

// 更新資料 else {

$sql_query = "UPDATE shoppingcart SET item_count = '$item_count' where member_acc = '$member_acc' and key_id = '$key_id'";

mysql_query($sql_query);

} }

// 確認結帳 by terry0412

else if (isset($_POST["B2"])) { // 讀取使用者資料

$sql_query = "SELECT * FROM members WHERE username

= '" . $member_acc . "'";

$result_user =

mysql_fetch_assoc(mysql_query($sql_query));

}

// else => 最後顯示購物車清單 // SQL 敘述句 (將關聯資料加進來)

$sql_query = "SELECT * FROM shoppingcart JOIN items ON item_id = id and member_acc = '" . $member_acc . "'";

// TODO 排序功能 (ASC:由小排到大)

$sql_query .= " ORDER BY key_id";

// 將 SQL 敘述句查詢資料加到 $result 中

$result = mysql_query($sql_query);

// 計算總筆數

$total_records = mysql_num_rows($result);

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html;

charset=utf8">

<title>查看購物車</title>

</head>

<body background="000002.gif">

<?php

//mysql_data_seek($result, 0); // 將資料指標索引回到最前面第 0 筆

if ($total_records <= 0) {

echo "<form method='POST' action='cart.php'>";

echo "<table border='1' width='60%'>";

echo "我的購物車:<br><br>";

echo "(購物車內目前是空的)";

echo "</table>";

} else {

if (!isset($result_user)) { // 購物車

echo "<form method='POST' action='cart.php'>";

echo "<table border='1' width='60%'>";

echo "我的購物車:<br><br>";

// 表頭內容 echo "<tr>";

echo " <th width='5%'> </th>";

echo " <th width='9%'> </th>";

echo " <th width='60%'>商品名稱</th>";

echo " <th width='6%'>單價</th>";

echo " <th width='7%'>數量</th>";

echo " <th width='10%'>小計</th>";

echo "</tr>";

// 累計金額

$total_price = 0;

// 表格內容

while ($row_result = mysql_fetch_assoc($result)) { echo "<tr>";

echo " <td>";

echo " <p align='center'>";

// 刪除購物車內的商品

echo " <button name='remove-item' value='" .

$row_result["key_id"] . "'>移除</button></td>";

echo " <td>";

echo " <p align='center'>";

echo " <img border='0' src='../images/" .

$row_result["item_id"] . ".jpg' width='150' height='150'></td>";

echo " <td>" . $row_result["name"] . "</td>";

echo " <td>" . $row_result["price"] . "</td>";

echo " <td>";

echo " <input type='text' name='" .

$row_result["key_id"] . "' size='4' value='" .

$row_result["item_count"] . "' style='border-style: solid;

border-width: 1px; padding-left: 4px; padding-right: 4px;

padding-top: 1px; padding-bottom: 1px'>";

echo " <button name='update-item' value='" .

$row_result["key_id"] . "'>更新</button></td>";

// 將小計金額計算出來

$calc_count = $row_result["price"] *

$row_result["item_count"];

echo " <td>NT " . $calc_count . "</td>";

echo "</tr>";

// 最後加入總價中

$total_price += $calc_count;

}

// 顯示總計金額 echo "<tr>";

echo " <td colspan='6'><p align='right' size='14'>總 計:&nbsp; NT " . $total_price . " </td>";

echo "</tr>";

// 結帳選項

echo " <table border='0' width='60%'>";

echo " <tr>";

echo " <td align='center' width='107'>";

echo " <input type='submit' value='確認結帳' name='B2' style='float: right'></td>";

echo " </tr>";

echo " </table>";

echo "</table>";

echo "</form>";

} else { // 結帳

echo " <p>=== 您的訂單===</p>";

echo "<table border='1' width='60%'>";

// 表頭內容 echo " <tr>";

echo " <th width='9%'> </th>";

echo " <th width='60%'>商品名稱</th>";

echo " <th width='6%'>單價</th>";

echo " <th width='7%'>數量</th>";

echo " <th width='10%'>小計</th>";

echo " </tr>";

// 累計金額

$total_price = 0;

// 表格內容

while ($row_result = mysql_fetch_assoc($result)) { echo " <tr>";

echo " <td>";

echo " <p align='center'>";

echo " <img border='0' src='../images/" .

$row_result["item_id"] . ".jpg' width='50' height='50'></td>";

echo " <td>" . $row_result["name"] . "</td>";

echo " <td>" . $row_result["price"] . "</td>";

echo " <td>" . $row_result["item_count"] . "</td>";

// 將小計金額計算出來

$calc_count = $row_result["price"] *

$row_result["item_count"];

echo " <td>NT " . $calc_count . "</td>";

echo " </tr>";

// 最後加入總價中

$total_price += $calc_count;

}

echo "</table>";

// 表單加註 name 是為了接收至 javascript 動作

echo "<form method='POST' action='check_out.php' name='cart'>";

echo " <p>===請確認收件人資訊===</p>";

echo " <p>姓名 : <input type='text' name='T2' size='15' value='" . $result_user["name"] . "'></p>";

echo " <p>送貨地址 : <input type='text' name='T3' size='30' value='" . $result_user["address"] . "'></p>";

echo " <p>電話 : <input type='text' name='T4' size='10' value='" . $result_user["tel"] . "'></p>";

echo " <p>電子信箱 : <input type='text' name='T6' size='15' value='" . $result_user["email"] . "'></p>";

echo " <p> </p>";

echo " <p>付款方式:";

echo " <select size='1' name='D1'>";

echo " <option value='1'>臨櫃匯款</option>";

echo " <option value='2'>ATM 匯款</option>";

echo " <option value='3'>貨到付款</option>";

echo " </select></p>";

echo " <p><input type='submit' value='送出訂單' name='B3'></p>";

echo "</form>";

} }

?>

</body>

<script type="text/javascript">

function check_price() {

var total_price = <?php echo $total_price; ?>;

// 強制轉型十進位整數

document.cart.price.value =

parseInt(document.cart.R1.value, 10) + parseInt(total_price, 10);

}

function show_alert() {

window.alert("已成功送出訂單!");

}

</script>

</html>

在文檔中 FU網路商店行銷 (頁 23-30)

相關文件