0

我一直在向结帐处发布一个很好的表单,但我需要我的表单来发布多个重复的字段。

我的数组现在显示

Array
(
    [0] => Array
        (
            [id] => 65983c1c16d925e9a8cc4c4b3bdda1f5
            [item] => Array
                (
                    [1] => Brent New Product
                    [2] => Crazy Product
                )

            [unitprice] => Array
                (
                    [1] =>  100
                    [2] =>  125
                )

            [quantity] => Array
                (
                    [1] => 1
                    [2] => 1
                )

        )

)

但它需要显示为

Array
(
    [0] => Array
        (
            [id] => 65983c1c16d925e9a8cc4c4b3bdda1f5
                (
                    [product] => Brent New Product
                    [unitprice] => 100
                    [quantity] => 1
                )
                [id] => 4326526662262
                (
                    [product] => Brent New Product2
                    [unitprice] => 140
                    [quantity] => 5
                )



        )

)

当我的表单从这个页面发布时,它会转到这个收集所有值的 php 脚本。

session_start();
$price = $_POST['price'];
$id = $_POST['id'];
$item = $_POST['item'];
$quantity = $_POST['quantity'];

echo '<h1>Add to cart</h1>';

echo "<p>Thank you for wanting a <strong>$item</strong>!</p>";

//We define an associative array with the details of our new item
$cart_row = array(
    'id' => md5(rand()),
    'item' => $item,
    'unitprice' => $price,
    'quantity' => $quantity
);

然后在购物车页面上,我希望显示购物车中的物品,但我没有得到任何填充框,它以前使用单个数组工作。

 session_start();

 foreach($_SESSION['cart'] as $item) { ?>
<tr>
  <td><?php echo $item['item']; ?></td>
  <td><?php echo $item['unitprice'];?></td>
  <td><?php echo $item['quantity']; ?></td>
  <td></td>
  <td><form id="id" name="id" method="post" action="cart.php?remove=<?php echo $item["id"] ?>">
      <input name="id" type="hidden" id="id" value="<? echo $item["id"]; ; ?>" />
      <input name="Submit" type="submit" class="formbox" value="remove" />
    </form></td>
</tr>
<?php   } ?>
4

1 回答 1

0

我相信你不能有这种类型的数组替代品你可以这样做

Array
(
            [65983c1c16d925e9a8cc4c4b3bdda1f5] => (
                [product] => Brent New Product
                [unitprice] => 100
                [quantity] => 1
            )
            [4326526662262] => (
                [product] => Brent New Product2
                [unitprice] => 140
                [quantity] => 5
            )
 )

并像这样打印

session_start();
foreach($_SESSION['cart'] as $id => $item) { ?>
<tr>
<td><?php echo $item['item']; ?></td>
<td><?php echo $item['unitprice'];?></td>
<td><?php echo $item['quantity']; ?></td>
<td></td>
<td><form id="id" name="id" method="post" action="cart.php?remove=<?php echo $id ?>">
  <input name="id" type="hidden" id="id" value="<? echo $id; ?>" />
  <input name="Submit" type="submit" class="formbox" value="remove" />
  </form></td>
 </tr>
 <?php   } ?>

您可以使用此代码填充会话

session_start();
$cart = array();

$cart[] = array(
    $id => array(
      'product' => $product_name,
      'unitprice' => $unit_price,
      'quantity' => $quantity
    )
);

$_SESSION['cart'] = $cart;
于 2013-06-06T15:11:39.200 回答