使用多维会话的php订单

使用多维会话的php订单,php,multidimensional-array,session-state,Php,Multidimensional Array,Session State,例如,我在主页上有这个代码 <?php session_start(); $_SESSION['order']=array(); ?> <form name="orderform" method="post" action="e.php"> Product Catalog <table border="1"> <tr> <td>Product</td> <td>Price</td>

例如,我在主页上有这个代码

<?php
session_start();
$_SESSION['order']=array();

?>
<form name="orderform" method="post" action="e.php">
Product Catalog
<table border="1">
<tr>
    <td>Product</td>
    <td>Price</td>
    <td>Quantity</td>
</tr>
<?
    for($i=0;$i<6;$i++){
        echo '<tr>';
        echo '<td><input type=hidden name="product" value="'.$i.'"> Product     '.$i.'</td>';
        $price=rand(1,10);
        echo '<td><input type=hidden name="price"     value="'.$price.'">$'.$price.'</td>';
        echo '<td><input type=text name="quantity"></td>';
        echo '<tr>';        
    }
?>
</table>
<br>
<input type="submit" name="submit" value="submit">
</form>

产品目录
产品
价格
量

我有一个多维会话数组,$\u Session['order'],我正试图保存6个产品项的订单,以及它的价格和数量,以便在POST方法执行后可以在下一页检索它

e.php文件上的ie

<?php

session_start();

$_SESSION['order'][] = array('product'=>$_POST['product'],
                                'price'=>$_POST['price'],
                                'quantity'=>$_POST['quantity']);
var_dump($_SESSION['order']);

if(count($_SESSION['order'])>0){
    foreach($_SESSION['order'] as $order){
        echo "<p>Product = ".$order['product']."</p>";
        echo "<p>Price = ".$order['price']."</p>";
        echo "<p>Quantity = ".$quantity['quantity']."</p>";
    }
}
?>


但是,我在e.php上得到的结果是,我只得到订单页面的最后一项,而不是前五项。我做错什么了吗?您的想法是什么?

问题是您有多个同名表单字段。你需要这样的东西:

for($i=0; $i<6; $i++){
    $price = rand(1,10);
    printf('<tr>'.
             '<td><input type=hidden name="product[%1$d]" value="%1$d" />Product %1$d</td>'.
             '<td><input type=hidden name="price[%1$d]" value="%2$f" />$ %2$f</td>'.
             '<td><input type=text name="quantity[%1$d]" /></td>'.
           '</tr>',
           $i, $price);
}
<?
    for($i=0;$i<6;$i++){
        echo '<tr>';
        echo '<td><input type=hidden name="product'.$i.'" value="'.$i.'"> Product     '.$i.'</td>';
        $price=rand(1,10);
        echo '<td><input type=hidden name="price'.$i.'"     value="'.$price.'">$'.$price.'</td>';
        echo '<td><input type=text name="quantity'.$i.'"></td>';
        echo '<tr>';        
    }
?>
<?php
foreach($_POST['product'] as $key => $value){
   $_SESSION['order'][] = array('product'=>$_POST['product'][$key],
                                'price'=>$_POST['price'][$key],
                                'quantity'=>$_POST['quantity'][$key]);
} 
?>
对于($i=0;$i$_POST['product'][$i],
'price'=>$\u POST['price'][$i],
“数量”=>$_POST['quantity'][$i]);

(对于生产,您还应该检查POST变量是否确实存在以及数组大小是否正确。)

您正在使用相同的
名称
属性对多个字段进行
$\u POST
操作,因此
$\u POST
变量将只包含您正在发布的最终唯一名称。您可以为每个输入创建唯一的名称,也可以将字段发布为数组。唯一名称如下所示:

for($i=0; $i<6; $i++){
    $price = rand(1,10);
    printf('<tr>'.
             '<td><input type=hidden name="product[%1$d]" value="%1$d" />Product %1$d</td>'.
             '<td><input type=hidden name="price[%1$d]" value="%2$f" />$ %2$f</td>'.
             '<td><input type=text name="quantity[%1$d]" /></td>'.
           '</tr>',
           $i, $price);
}
<?
    for($i=0;$i<6;$i++){
        echo '<tr>';
        echo '<td><input type=hidden name="product'.$i.'" value="'.$i.'"> Product     '.$i.'</td>';
        $price=rand(1,10);
        echo '<td><input type=hidden name="price'.$i.'"     value="'.$price.'">$'.$price.'</td>';
        echo '<td><input type=text name="quantity'.$i.'"></td>';
        echo '<tr>';        
    }
?>
<?php
foreach($_POST['product'] as $key => $value){
   $_SESSION['order'][] = array('product'=>$_POST['product'][$key],
                                'price'=>$_POST['price'][$key],
                                'quantity'=>$_POST['quantity'][$key]);
} 
?>

然后需要循环post数组并将其添加到会话数组:

<?php
$i=0;
while(isset($_POST['product'.$i])){
   $_SESSION['order'][] = array('product'=>$_POST['product'],
                                'price'=>$_POST['price'],
                                'quantity'=>$_POST['quantity']);
   $i++;
}
?>

您还可以将其作为阵列发送:

<?
    for($i=0;$i<6;$i++){
        echo '<tr>';
        echo '<td><input type=hidden name="product[$i]" value="'.$i.'"> Product     '.$i.'</td>';
        $price=rand(1,10);
        echo '<td><input type=hidden name="price[$i]"     value="'.$price.'">$'.$price.'</td>';
        echo '<td><input type=text name="quantity[$i]"></td>';
        echo '<tr>';        
    }
?>

然后像这样把它拿出来:

for($i=0; $i<6; $i++){
    $price = rand(1,10);
    printf('<tr>'.
             '<td><input type=hidden name="product[%1$d]" value="%1$d" />Product %1$d</td>'.
             '<td><input type=hidden name="price[%1$d]" value="%2$f" />$ %2$f</td>'.
             '<td><input type=text name="quantity[%1$d]" /></td>'.
           '</tr>',
           $i, $price);
}
<?
    for($i=0;$i<6;$i++){
        echo '<tr>';
        echo '<td><input type=hidden name="product'.$i.'" value="'.$i.'"> Product     '.$i.'</td>';
        $price=rand(1,10);
        echo '<td><input type=hidden name="price'.$i.'"     value="'.$price.'">$'.$price.'</td>';
        echo '<td><input type=text name="quantity'.$i.'"></td>';
        echo '<tr>';        
    }
?>
<?php
foreach($_POST['product'] as $key => $value){
   $_SESSION['order'][] = array('product'=>$_POST['product'][$key],
                                'price'=>$_POST['price'][$key],
                                'quantity'=>$_POST['quantity'][$key]);
} 
?>


一些SIDENOTES:您可能需要考虑使用<代码>(s)Primtf<代码>来格式化较长的输出(参见我的编辑代码);在空白元素的末尾添加斜杠,例如
;循环中有一个小错误,您没有关闭表行,而是打开一个新的表行(添加斜杠)。谢谢@cztechnology。这真的很有帮助。每天学习新的东西。