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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!