我想从sql server获取值到php中作为选择的下拉列表。首先,我在数据库上具有空值,当我在下拉列表中选择值时,单击提交按钮时它将在数据库上更新。现在,我想将从db存储的数据设置为每个下拉列表中的选定选项。如果我单击提交按钮,则可以选择再次更新。我试图在选项标签中放入if条件,但出现错误。
<?php
foreach ($records as $key => $data) {
$dates[] = $data[0];
$dates1[] = $data[3];
$dates2[] = $data[2];
$circuit[] = $data[1];
$circuitNo[] = $FeederData-> getCircuitNumber($data[1]);
$parsed = date_parse($key[2]);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
$tripdate[] = date("m/d/Y", strtotime($data[0]) - $seconds);
$triptime[] = date("H:i:s", strtotime($data[3]) - strtotime($data[2]));
$selectedCause[] = $data[4];
$selectedWeather[] = $data[5];
$selectedDevice[] = $data[6];
$selectedEquipment[] = $data[7];
}
for ($i=0; $i < count($dates); $i++) {
echo " <tr>
<form method='POST' name='forms1' class='form-inline'>
<input type='hidden' name='selectedMonth' value='$date1'>
<input type='hidden' name='selectedYear' value='$year'>
<td><input type='hidden' name='tripdate[]' value='$tripdate[$i]'/> $tripdate[$i] </td>
<td><input type='text' name='description1[]' class='form-control'/></td>
<td><input type='hidden' name='circuit[]' value='$circuit[$i]'>
<input type='hidden' name='tripdate1[]' value='$circuitNo[$i]'/> $circuitNo[$i] </td>
<td><input type='hidden' name='triptime[]' value='$triptime[$i]'/>$triptime[$i]</td>
<td><input type='hidden' name='dates[]' value='$dates[$i]'/>$dates[$i]</td>
<td><input type='hidden' name='dates1[]' value='$dates1[$i]'/>$dates1[$i]</td>
<td><input type='hidden' name='dates2[]' value='$dates2[$i]'/>$dates2[$i]</td>
<td><input type='text' name='description2[]' class='form-control'></td>
<td>
<div class='form-group'>
<select class='form-control' name='InterruptionCause[]'>
<option value='001'" if ($selectedCause[$i] == '1') {
echo "selected";
}"> 001 </option>
<option value='002'>002 </option>
<option value='003'>003 </option>
<option value='004'>004 </option>
</select>
</div>
</td>
}
?>
<input type='submit' class='btn btn-success' name='interruptionreport' value='Submit'>
</form>
</tbody>
</table>
答案 0 :(得分:0)
您的尝试会产生语法错误。您只需结束echo
语句,而无需使用;
。
echo "
<div class='form-group'>
<select class='form-control' name='InterruptionCause[]'>
<option value='001' " . $selectedCause[$i] == '1' ? "selected" : "" . "> 001 </option>
<option value='002' " . $selectedCause[$i] == '2' ? "selected" : "" . ">002 </option>
<option value='003' " . $selectedCause[$i] == '3' ? "selected" : "" . ">003 </option>
<option value='004' " . $selectedCause[$i] == '4' ? "selected" : "" . ">004 </option>
</select>
</div>";
这将检查每个选项的selectedCause[$i]
是否等于您的选项的值。如果是这样,它将打印“选定”,否则将不打印(空字符串)。