繁体   English   中英

简单的PHP购物车数量

[英]Simple PHP Shopping Cart Quantity

我是PHP的新秀,我正在尝试制作一个大学模块的购物网站。

这是我的产品页面,其中列出了所有产品:

//Get the category from the URL
$category = $_GET['category'];

echo("<h1>Products: $category</h1>");
echo("<FORM METHOD='LINK' ACTION='products.php'>");
echo("<INPUT TYPE='submit' VALUE='Back'>");
echo("</FORM>");
echo("<hr>");

//Include database file with settings im
require "db.inc";

//Store the connection in a variable for later use
$connection = mysql_connect($hostname, $username, $password);

//Check for connection
if(! $connection )
{
  die('Could not connect: ' . mysql_error());
}

//If the category is all then
if($category == "All")
{
    $query = "SELECT * FROM products";//Select everything
}
//If it's not then..
else
{
    $query = "SELECT * FROM products WHERE category = '$category'";
}

//Open the Database
mysql_select_db($dbname);

//Start the query
$result = mysql_query($query, $connection);

if(!$result )
{
  die('Could not retrieve data: ' . mysql_error());
}

//Loop through the rows and display each object
while($row = mysql_fetch_array($result))
{
    //Define all variables we will use
    $productid = $row['id'];
    $productname = $row['name'];
    $productdescription = $row['description'];
    $productimage = $row['image'];
    $productprice = $row['price'];
    $productstock = $row['stock'];

    //Display each product and it's info
    echo("<h3>$productname</h3>");
    echo("<a href=$productimage><img border='0' alt=$productname src=$productimage width='100' height='100'></a><br>");
    echo("$productdescription<br>");
    echo("<b>Price:</b> £$productprice (ex VAT)<br>");
    echo("<b>Stock:</b> $productstock<br>");

    //Create a link for each product, that goes to the add to cart script with the product id in the URL
    //Only if youre logged in can you see this button
    if( isset($_SESSION['login']))
    {

        echo("<FORM METHOD='POST' ACTION='process_addtocart.php?id=$productid&quantity=1'>");
        echo("<INPUT TYPE='submit' VALUE='Add to Cart'>");
        echo("</FORM>");
    }
    echo("<hr>");
}

如您所见,它从数据库获取所有产品,并添加一个按钮,该按钮链接到具有要添加的ID和数量的addcart php文件。

这是我的添加到购物车功能:

<?php
    //Make sure user is logged in first
    if( isset($_SESSION['login']))
    {

        //Setup our shopping cart if it isnt already setup into an array table to hold item info
        if( empty($_SESSION['shoppingcart']))
        {
            $_SESSION['shoppingcart'] = array();

        }

        $id = $_GET['id'];
        $quantitytoadd = $_GET['quantity'];

        if( isset( $_SESSION['shoppingcart'][$id]))
        {
            //CODE TO ADD TO QUANTITY????

        }

        array_push($_SESSION['shoppingcart'], $id);
        echo("Item has been added to your cart!");


    }
    else
    {
        echo("<p>Please login to add items to your shopping cart!</P>");
    }

?>

我如何使它每次递增数组表中该特定ID的数量值?

会是这样吗?

array_push($_SESSION['shoppingcart'], $id, $quantitytoadd++);

或者,您可以将现有产品增加一个,然后将产品ID设置为密钥。 还要保持输入内容整洁。

<?php
//Make sure user is logged in first
if( isset($_SESSION['login']))
{

    //Setup our shopping cart if it isnt already setup into an array table to hold item info
    if( empty($_SESSION['shoppingcart']))
    {
        $_SESSION['shoppingcart'] = array();

    }

    $id = $_GET['id'];
    $quantitytoadd = $_GET['quantity'];

    if( isset( $_SESSION['shoppingcart'][$id]))
    {
        //CODE TO ADD TO QUANTITY????
         $_SESSION['shoppingcart'][$id]++;

    }
    else
    {
        $_SESSION['shoppingcart'][$id] = 1;
    }
    echo("Item has been added to your cart!");


}
else
{
    echo("<p>Please login to add items to your shopping cart!</P>");
}
?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM