0

この質問を投稿した後に思いついた別の解決策は、 for ループを使用して毎回追加する代わりに、次のように言うことができました。

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

すでにカートに入っているものに希望の番号を追加するのではなく、この方法でカートを更新できるため、これがより良い方法であることがわかりました.

この投稿を読んでいるすべての人にとって、ハンドラーを使用してカートを更新するソリューションを思いつきました。以下の通りです。. . 最初に details.php のフォーム部分に

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

ハンドラの next 。. .次のループと変数を追加します

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

それから

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

いくつかのタスクを処理するために、php を使用してショッピング カートの Web サイトを作成しています。カート内の商品の数量を変更できません。入力を取得して送信を処理し、カートビューに数量を表示するために使用するコードは次のとおりです

details.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:

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

// ここで、私の問題に焦点を当てるためにいくつかの 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>
4

1 に答える 1

0

フォームに入力された値で数量を増やすには:-

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

おそらく、ユーザーが番号を入力したこととフォームが投稿されたことを確認したいので、次のようなものが必要になるでしょう:-

if (isset($_POST['qty']) && is_numeric($_POST['qty])) {
   $entry->qty =  $qty + $_POST['qty'];
}
else
{
   $entry->qty =  $qty;
}
于 2012-04-05T21:31:43.603 に答える