使用多维会话的php订单表

发布于 2025-01-07 00:01:19 字数 1606 浏览 0 评论 0原文

例如,我在主页上有此代码。

<?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>

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

即在 e.php 文件上

<?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 eg I have this code on the main page.

<?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.'">

I have a multidimensional Session array, $_SESSION['order'] and I'm trying to save this order form of 6 product items, along with its price and quantity, so it can be retrieved on the following page after POST method is actioned.

ie on e.php file

<?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>";
    }
}
?>

But the result I'm getting on e.php is I only get the last item of the order page, but not the other previous five. Did I do something wrong here? What are your thoughts?

.$price.'</td>'; echo '<td><input type=text name="quantity"></td>'; echo '<tr>'; } ?> </table> <br> <input type="submit" name="submit" value="submit"> </form>

I have a multidimensional Session array, $_SESSION['order'] and I'm trying to save this order form of 6 product items, along with its price and quantity, so it can be retrieved on the following page after POST method is actioned.

ie on e.php file

But the result I'm getting on e.php is I only get the last item of the order page, but not the other previous five. Did I do something wrong here? What are your thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2025-01-14 00:01:19

您正在使用相同的 name 属性_POST处理多个字段,因此 $_POST 变量将仅包含您所指定的最终唯一名称发布。您可以为每个输入创建唯一的名称,也可以将字段作为数组发布。唯一的名称将如下所示:

<?
    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.'">

然后您需要循环遍历 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.'">

并像这样取出它:

<?php
foreach($_POST['product'] as $key => $value){
   $_SESSION['order'][] = array('product'=>$_POST['product'][$key],
                                'price'=>$_POST['price'][$key],
                                'quantity'=>$_POST['quantity'][$key]);
} 
?>
.$price.'</td>'; echo '<td><input type=text name="quantity'.$i.'"></td>'; echo '<tr>'; } ?>

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


您还可以将其作为数组发送:


并像这样取出它:


.$price.'</td>';
        echo '<td><input type=text name="quantity[$i]"></td>';
        echo '<tr>';        
    }
?>

并像这样取出它:

.$price.'</td>'; echo '<td><input type=text name="quantity'.$i.'"></td>'; echo '<tr>'; } ?>

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

您还可以将其作为数组发送:

并像这样取出它:

You're _POSTing multiple fields with the same name property, so the $_POST variable will contain only the final unique names that you're posting up. You can either create unique names for each input, or you can post up the fields as an array. Unique names would look like this:

<?
    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.'">

And then you'd need to loop through the post array and add it to session array:

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

You can also send it up as an array:

<?
    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.'">

And get it out like this:

<?php
foreach($_POST['product'] as $key => $value){
   $_SESSION['order'][] = array('product'=>$_POST['product'][$key],
                                'price'=>$_POST['price'][$key],
                                'quantity'=>$_POST['quantity'][$key]);
} 
?>
.$price.'</td>'; echo '<td><input type=text name="quantity'.$i.'"></td>'; echo '<tr>'; } ?>

And then you'd need to loop through the post array and add it to session array:


You can also send it up as an array:


And get it out like this:


.$price.'</td>';
        echo '<td><input type=text name="quantity[$i]"></td>';
        echo '<tr>';        
    }
?>

And get it out like this:

.$price.'</td>'; echo '<td><input type=text name="quantity'.$i.'"></td>'; echo '<tr>'; } ?>

And then you'd need to loop through the post array and add it to session array:

You can also send it up as an array:

And get it out like this:

你在看孤独的风景 2025-01-14 00:01:19

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

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 < count($_POST['product']; $i++) {
    $_SESSION['order'][] = array('product'=>$_POST['product'][$i],
                                 'price'=>$_POST['price'][$i],
                                 'quantity'=>$_POST['quantity'][$i]);

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

The problem is you have multiple form fields with the same name. You'd need something like this:

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);
}

and

for ($i = 0; $i < count($_POST['product']; $i++) {
    $_SESSION['order'][] = array('product'=>$_POST['product'][$i],
                                 'price'=>$_POST['price'][$i],
                                 'quantity'=>$_POST['quantity'][$i]);

(For production you should also check the POST variables if they are really present and arrays in correct size.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文