将商品添加到购物车

时间:2014-04-18 05:26:31

标签: javascript php mysql

我是php的新手,所以请承认我缺乏知识。 我正在开发一个简单的购物车。我的屏幕分为三个framset。左边是imagemap,用户可以在其中单击所需的项目。 topRightphp页面,用于检索并在表格中显示leftFrame中点击的项目的详细信息。此表格的最后一个单元格有textfieldbutton来提交订单。 bottomRight框架的目标是显示购物车,向用户显示已选择的产品数量。 我的问题是到目前为止我只能显示提交的当前订单。换句话说,我只显示topRigt帧中“有序”的最后一项的详细信息。 如何继续添加第二项而不是替换它?

以下是topLeft

的代码
<html>
<head>
<title>Top Right</title>
<style type="text/css">
<!--
    @import url("style.css");
-->
</style>
</head>
<body bgcolor = "#E0E6F8">
    <h2>ITEMS</h2>
<?php
session_start();

// get the id from the left frame
$id = $_GET['var'];

// variable to connect to db
$user_name = "name";
$password = "password";
$server = "server";
$link = new mysqli($server,$user_name,$password,'poti');

if(!$link)
    die("Could not connect to databse");

$result = $link->query("SELECT * FROM products WHERE product_id = '$id';");

while($row = $result->fetch_array())
{
    $id = $row['product_id'];
    $name = $row['product_name'];
    $unit_price = $row['unit_price'];
    $unit_quantity = $row['unit_quantity'];
    $in_stock = $row['in_stock'];
    $arrayName = array('id' => $id, 'name' => $name);
    // store values in session
    $_SESSION['cart'] = $arrayName;

            // display details in a table (code needs to be refactored)
    if(!empty($row)){
        print "";
        print "<form method='post' name='add' id='add' action='' target=''>";
        print "<table  id='hor-minimalist-a' border = '0' width = '100%'>";
        print "<thead>
                    <tr>
                        <th scope='col'>ID</th>
                        <th scope='col'>Name</th>
                        <th scope='col'>Price</th>
                        <th scope='col'>QTY</th>
                        <th scope='col'>In Stock</th>
                        <th scope='col'>Order</th>
                    </tr>
                </thead>";
                print "<tr>\n";
                print "<td>";
                print($id);
                print "</td>";
                print "<td>";
                print($name);
                print "</td>";
                print "<td>";
                print($unit_price);
                print "</td>";
                print "<td>";
                print($unit_quantity);
                print "</td>";
                print "<td>";
                print($in_stock);
                print "</td>";

                    print "<td>
                                <input type='text' name='qty_ordered' size='4'><input type='submit' name='submit' value='Submit' onclick='checkQty()'>
                            </td>";
                print "</tr>";

                }
                print"</table>"; 
                print "</form>";
    }
?>


<script type="text/javascript">

function checkQty()
{
if(document.add.qty_ordered.value > 0 && document.add.qty_ordered.value <= 20)
{
    document.add.action = "bottomRight.php";
    document.add.target = "BottomRight";
    document.add.submit();
}
else
{
    //document.add.action = "topRight.php";
    //document.add.target = "TopRight";
    alert("Quantity must be between 1 and 20, please enter a value between 1 to 20"); 
    //document.add.submit();
}
}
</script>
</body>
</html>

bottomRight

<html>
<head>
<title>Bottom Right</title>
<style type="text/css">
<!--
    @import url("style.css");
-->
</style>
</head>
<body>
    <h2>Shopping Cart</h2>
<?php 
session_start();
    // doing this I keep on displaying only the last item "ordered"
if (isset($_POST['submit'])===true) {
      if(isset($_SESSION['cart'])===true) {
    $array = array($_SESSION['cart']);
    print_r($array);

     // not working
/*foreach($array as $a)
{
    echo "<table><tr>";
    echo "<td width='150px'><span style='color:white'>" . $a['id'] ."</span></td>";
    echo "</tr></table>";}*/
    }
}   
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

查看Adam Khoury在youtube上发布的视频教程。他使用了mysql_*(我知道它已弃用)功能,但您可以使用自己的代码解决问题。该视频仅供您参考。

以下是youtube播放列表链接:https://www.youtube.com/playlist?list=PL442E340A42191003

以下是解决查询的具体视频链接:https://www.youtube.com/watch?v=WXqbQy9fOp8

希望这会对你有所帮助。