大家好! 我是新來的編碼,將在明年九月上課。期待那.... :-)PHP Myqsl PDO代碼效率 - foreach循環內的Foreach循環
我正在爲我的數據庫做一個修改頁面。它由輸入和下拉列表組成,用於修改數據庫的內容。至少現在......直到我學到更多。
我爲自己編碼,想知道以下是否是正確的方法?在我看來,它不是,因爲每當外層循環經過時內層查詢都會被執行......但是它工作!?!?
這是我能找到使內部foreach循環($ familylist)與MySQL查詢工作的唯一途徑。如果內循環的查詢在外循環之外($ plantList)...它不起作用。第一個下拉列表會填充內容,但以下行不會,至少只有第一個選項,它不會填充查詢中的內容。
任何幫助,歡迎和讚賞!
<?php //more code here....
$plantQuery = "SELECT id, genre, espece, famille FROM plante ORDER BY genre";
$plantList = $dbconnect->query ($plantQuery);
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>GENRE</th>
<th>ESPÈCE</th>
<th>FAMILLE</th>
</tr>
</thead>
<tbody>
<?php foreach ($plantList->fetchAll(PDO::FETCH_ASSOC) as $plant) : ?>
<form method="post">
<tr>
<td><input name="id" value="<?php echo $plant["id"] ;?>" readonly></td>
<td><input name="genre" value="<?php echo $plant["genre"] ;?>"></td>
<td><input name="espece" value="<?php echo $plant["espece"] ;?>"></td>
<td><select name="familleList" >
<option value="0" >Choisir une famille</option>
<?php
$familyQuery = "SELECT id, famille FROM famille ORDER BY id ASC";
$familyList = $dbconnect->query ($familyQuery);
?>
<?php foreach ($familyList->fetchAll(PDO::FETCH_ASSOC) as $family):?>
<option value="<?php echo $family["id"] ;?>" <?php if ($plant["famille"] <> 0 && $plant["famille"] == $family["id"]) {echo "selected"; }?>><?php echo $family["id"] . " - " . $family["famille"] ;?></option>
<?php endforeach ;?>
</select>
</td>
<td><button name="modifier" type="submit">Modifier</button></td>
<td><button name="supprimer" type="submit">Supprimer</button></td>
</tr>
</form>
<?php endforeach ;?>
</tbody>
</table>
顯然我不記得基礎!我需要拔下代碼並取消代碼。Merci! –