简体   繁体   English

Php 使用会话更新购物车数量

[英]Php Shopping Cart Update quantity using Sessions

Another solution I came up with after posting this question instead of using a for loop to add one each time I could have just said:我在发布这个问题后想出了另一种解决方案,而不是每次使用 for 循环添加一个我可以说的:

$session->cart[$params->id] => $qty;

I found this to be a better way because you can update the cart this way instead of adding the desired number onto what is already in the cart.我发现这是一种更好的方法,因为您可以通过这种方式更新购物车,而不是将所需数量添加到购物车中已有的商品中。

For all reading this post I came up with a solution to update the cart using the handler.对于所有阅读这篇文章的人,我提出了一个使用处理程序更新购物车的解决方案。 it is as follows.它如下。 . . . . first in the form part of details.php首先在详细信息的表格部分.php

<form method="post"> //should be added to retrieve the qty data from the text field.

next in the handler.接下来在处理程序中。 . . .add the follwing loop and variable .添加以下循环和变量

$qty = $_POST['qty']; or $qty = $_REQUEST['qty'];

then然后

for($i =0; $i < $qty ; $i++){
  ++$session->cart[$params->id];
}

I am creating a shopping cart website using php to handle some tasks.我正在使用 php 创建一个购物车网站来处理一些任务。 I am having difficulty changing the quantity of an item in the cart.我很难更改购物车中商品的数量。 here is my code that i use to get the input handle the submission and display the quantity in a cart view这是我用来获取输入处理提交并在购物车视图中显示数量的代码

details.php:详情.php:

    <form id="cart_form" action="handler-add-cart.php">
     <input type="hidden" name="id" value="<?php echo $product->id ?>" />
     <input type="submit" value="add to cart"/>
    **Quantity:<input type="text" name="qty" />**
    </form>

handler_add_cart.php: handler_add_cart.php:

<?php
require_once "include/Session.php";
$session = new Session();
**$params = (object) $_REQUEST;
++$session->cart[$params->id];**
header("location: cart.php");

cart.php:购物车.php:

 <?php
    require_once "include/Session.php";
    $session = new Session();
    require_once "include/db.php";

    // The $cart array simplifies the view generation below, keeping 
    // computations and database accesses in this controller section.
    $cart = array();
    if (isset($session->cart)) {
    $total = 0;
    foreach ($session->cart as $prod_id => $qty) {
    $product = R::load("products", $prod_id);
    $total += $qty * $product->price;

    $entry = new stdClass();  // entry will contain info for table
    $entry->id = $prod_id;
    $entry->price = $product->price;
    $entry->name = $product->name;
    **$entry->qty =  $qty ;**
    $cart[] = $entry;
    }
    }
    ?>

// here i have removed some html to focus in on my issue i have all the tags in the files so that is not the issue // 在这里我删除了一些 html 以专注于我的问题我在文件中有所有标签所以这不是问题

    <h2>Cart</h2>

    <?php if (count($cart)): ?>

     <table id="display">
      <tr>
       <th>product</th><th>id</th><th>quantity</th><th class='price'>price</th>
      </tr>
      <?php foreach ($cart as $entry): ?>
       <tr>
        <td><a href="details.php?id=<?php echo $entry->id ?>"
            ><?php echo $entry->name ?></a></td>
        <td><?php echo $entry->id ?></td>
        **<td class='qty'><?php echo $entry->qty ?></td>**

             i cleared these fields below to not distract from the issue im having
        <td >
         </td>
       </tr>
      <?php endforeach ?>
      <tr>
       <th >

       </th>
      </tr>
     </table>


    </body>
    </html>

To increment the quantity with the value entered in the form do:-要使用在表单中输入的值来增加数量,请执行以下操作:-

$entry->qty =  $qty + $_POST['qty'];

Although you probably want to verify that the user has entered a number and that the form has been posted, so you probably want something like:-尽管您可能想要验证用户是否输入了一个数字并且表单已经发布,所以您可能想要这样的东西:-

if (isset($_POST['qty']) && is_numeric($_POST['qty])) {
   $entry->qty =  $qty + $_POST['qty'];
}
else
{
   $entry->qty =  $qty;
}

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

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