大家好! 我是新来的编码,将在明年九月上课。期待那.... :-)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! –