douweicheng5532 2012-02-20 04:18
浏览 10
已采纳

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

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

  • 写回答

2条回答 默认 最新

  • dongqiao5573 2012-02-20 04:27
    关注

    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.'">$'.$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:

    <?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.'">$'.$price.'</td>';
            echo '<td><input type=text name="quantity[$i]"></td>';
            echo '<tr>';        
        }
    ?>
    

    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]);
    } 
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?