是否可以通过pdo查询的结果循环两次? 第一个函数是获取查询结果
function querySelectBrand($pdo)
{
$table01 = 'brand';
$table02 = 'text';
$querySelectBrand="SELECT $table01.*,
textDef.$table02 as defText,
textUsr.$table02 as usrText
FROM $table01
LEFT JOIN $table02 as textDef on $table01.textId = textDef.textId And textDef.languageId = $_SESSION[defaultLanguage]
LEFT JOIN $table02 as textUsr on $table01.textId = textUsr.textId And textUsr.languageId = $_SESSION[defaultLanguage];";
// echo $querySelectBrand;
$stmt = $pdo->prepare($querySelectBrand);
$stmt->execute([]);
return $stmt;
}
然后我将$ stmt作为$ result传递给一个函数来构建一个下拉框。
function dropDownBox($result, $name, $size, $selected)
{
$x=1;
echo'<select name="'.$name.'" size="'.$size.'">';
foreach($result AS $row01){
$id=$row01['id'];
if(isset($row01['usrText'])){$text=$row01['usrText']; }else{$text=$row01['defText']; }
if(isset($selected)and $id==$selected){
echo '<option value="'.$id.'" selected>';
echo $text;
echo '</option>';
}else{
echo '<option value="'.$id.'">';
echo $text;
echo '</option>';
}
}
echo'</select>';
reset($result);
foreach($result AS $row01){
$x++;
}
?>
<input type="text" name="testresult" value="<?php echo $x; ?>">
<?php
}
如何让第二个foreach循环再次通过$ result? $ x的输出现在是1.
reset($result);
不起作用。