【问题标题】:Simple PHP Shopping Cart Quantity简单的 PHP 购物车数量
【发布时间】:2016-03-01 12:39:03
【问题描述】:

我是 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++);

【问题讨论】:

  • 在通过添加到购物车表单发送请求之前,您应该使用 AJAX 调用来更新值或执行一些 javascript。正如代码所示,每次点击表单时,您都会向 'process_addtocart.php?id=$productid&quantity=1' 发送一个请求...
  • 顺便说一句:这是一个很好的例子,说明蜜蜂准备好注入错误的 SQL。摆脱 mysql_ 函数并使用 PDO。
  • 我知道是这样,但我被指示使用 mysql。这是为了大学目的,所以它永远不会在公共场合等

标签: php cart product shopping


【解决方案1】:

或者您可以使用已有的产品将产品加一,并将产品 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>");
}
?>

【讨论】:

  • 不会 $_SESSION['shoppingcart'][$id]++;增加产品的实际ID?我的每个产品都有一个唯一的 ID 号
  • 已编辑。您还可以使用产品 ID 作为键,数量是各自的值。
  • 我该怎么做?这就是上面发生的事情吗?
  • 是的 if( isset( $_SESSION['shoppingcart'][$id])) 使用产品 ID 检查键是否存在并将值增加 1,如果不存在 $_SESSION['shoppingcart'][$id] = 1; 则添加以产品 ID 为键的项目,并且值为 1。
  • 谢谢 - 我将如何循环查看每个 id 数量是多少??
猜你喜欢
  • 2012-03-11
  • 1970-01-01
  • 2011-01-06
  • 2023-03-25
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
相关资源
最近更新 更多