有没有办法通过foreach语句传递变量/ $ _POST?

时间:2014-01-25 23:56:08

标签: php mysql

嘿,我正在用PHP弄湿我的脚,并想知道如果我能放的话是否可能 $id = $row['id'];通过$_POST语句,然后获取每个$_POST变量,然后通过foreach打印它们。

提前致谢!

order.php:

            foreach ($rows as $row) {
            $food = $row["food"];
            $price = $row["price"];
            $picture = $row["picture"];
            $id = $row['id'];
            echo "<tr>
                    <td><img src='$picture' width='120px' /></td>
                    <td>$food</td>
                    <td>$$price</td>
                    <td><input type='number' min='0' max='10' placeholder='#' name='$id' maxlength='1'></td>
                </tr>";
        }

action.php的:

            foreach ($rows as $row){
              $food = $row['food'];
              $price = $row['price'];
              $id = $row['id'];

              if(isset($_POST[$id])){
                $qty = $_POST[$id];
                echo 'set';
                echo $qty;
              }else{
                  echo 'unset';
                  $qty = '';

              }
              echo "<tr>
            <td>$food</td>
            <td>$qty</td>
            <td>$price</td>
        </tr>";      
        }

1 个答案:

答案 0 :(得分:1)

将你的回声改为:

echo "<tr>
       <td><img src='$picture' width='120px' /></td>
       <td>$food</td>
       <td>$$price</td>
       <td><input type='number' min='0' max='10' placeholder='#' name='qty[".$id."]' maxlength='1'></td>
      </tr>";

然后在你的action.php中:

foreach ($rows as $row){
          $food = $row['food'];
          $price = $row['price'];
          $id = $row['id'];

          if(isset($_POST['qty'][$id])){
            $qty = $_POST['qty'][$id];
            echo 'set';
            echo $qty;
          }else{
              echo 'unset';
              $qty = '';

          }
          echo "<tr>
        <td>$food</td>
        <td>$qty</td>
        <td>$price</td>
    </tr>";      
}