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

时间:2016-06-27 10:05:07

标签: php arrays multidimensional-array

创建一个表,其值从二维数组中填充,如下所示,使用php' s for循环。但无法根据数组中的值填充下拉列表,因为我无法找出在echo中插入以下php代码的正确方法,该代码根据条件选择选项,

Function testDupes(strInput As String, strDelimiter As String) As String

Dim intRow As Integer
Dim intCol As Integer
Dim arrSplit() As String
Dim dicCompare As New Scripting.Dictionary
Dim intCounter As Integer

arrSplit = Split(strInput, strDelimiter)

For intCounter = 0 To UBound(arrSplit)

    If Not dicCompare.Exists(CStr(arrSplit(intCounter))) Then
        dicCompare.Add CStr(arrSplit(intCounter)), CStr(arrSplit(intCounter))
    End If

Next intCounter

For intCounter = 0 To dicCompare.Count - 1
    testDupes = testDupes & dicCompare.Keys()(intCounter) & _
        IIf(intCounter < dicCompare.Count, strDelimiter, "")
Next intCounter

Set dicCompare = Nothing

End Function

2 个答案:

答案 0 :(得分:0)

您的代码中存在多个语法错误:

第1行 - 在“if”之前插入:“<?php

Bellow,替换你的“IF”:

if($result[$row]['discount'] == '0') echo 'selected = "selected"'

使用此构造函数:

($result[$row]['discount'] == '0' ? 'selected = "selected" : '')

希望这有帮助。

答案 1 :(得分:0)

为什么要将echo放在每个HTML行的前面。您可以使用PHP结束和打开括号来轻松实现它。

您可以像这样更新您的PHP代码,

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