duanshang3230 2016-06-27 10:05 采纳率: 100%
浏览 46
已采纳

PHP从多维数组预填充下拉列表

Am creating a table whose values are populated from a two dimensional array as shown below using php's for loop. But unable to populate the drop-down based on the value from the array because I couldn't figure out a correct way to insert the below php code inside the echo which selects the option based on a condition,

  <option value='0' if ($result[$row]['discount'] == '0') echo 'selected = "selected"'; ?> >0</option>

<?php

$result = array (

    array (
      'item' => 'Dextrose', 
      'price' => '10.00',
      'quantity' => '10',
      'discount' => '5.00',
      'total' => '95.00'
      ),

    array (
      'item' => 'Nescaine',
      'price' => '20.00',
      'quantity' => '30',
      'discount' => '10.00',
      'total' => '540.00'
      ),

    array (
      'item' => 'Anticavity',
      'price' => '30.00',
      'quantity' => '10',
      'discount' => '25.00',
      'total' => '225.00'
     )
  );


for ($row = 0; $row < sizeof($result); $row++)

{
    echo "<tr>";
    echo "<td><input type='text' name='item[]' value=".$result[$row]['item']." required></td>";
    echo "<td><input type='text' name='price[]' value=".$result[$row]['price']." required></td>";
    echo "<td><input type='text' name='qty[]' value=".$result[$row]['quantity']." required></td>";
    echo "<td><select name='discount[]' >
              <option value='0'".if($result[$row]['discount'] == '0') echo 'selected = "selected"'.">0</option>
              <option value='5'".if($result[$row]['discount'] == '5') echo 'selected = "selected"'.">5</option>
              <option value='10'".if($result[$row]['discount'] == '10') echo 'selected = "selected"'.">10</option>
              <option value='25'".if($result[$row]['discount'] == '25') echo 'selected = "selected"'.">25</option></td>";
    echo "<td><input name='total[]' value=".$result[$row]['total']." ></td>";
    echo "</tr>";
    echo "<br>";        
}
  • 写回答

2条回答 默认 最新

  • 普通网友 2016-06-27 10:16
    关注

    Why are you putting echo in-front of each HTML line. You can use PHP closing and opening braces to achieve it easily.

    You can update your PHP code like this,

    <?php
    for ($row = 0; $row < sizeof($result); $row++)
    {
    ?> 
    <tr>
       <td><input type='text' name='item[]' value="<?php echo $result[$row]['item']; ?>" required></td>
       <td><input type='text' name='price[]' value="<?php echo $result[$row]['price']; ?>" required></td>
       <td><input type='text' name='qty[]' value="<?php echo $result[$row]['quantity']; ?>" required></td>
       <td><select name='discount[]' >
                  <option value='0' <?php if($result[$row]['discount'] == '0') { ?> selected <?php } ?>>0</option>
                  <option value='5' <?php if($result[$row]['discount'] == '5') { ?> selected <?php } ?>>5</option>
                  <option value='10' <?php if($result[$row]['discount'] == '10') { ?> selected <?php } ?>>10</option>
                  <option value='25' <?php if($result[$row]['discount'] == '25') { ?> selected <?php } ?>>25</option></td>
       <td><input name='total[]' value= <?php echo $result[$row]['total']; ?>></td>";
       </tr><br>
    <?php } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端