您好我正在尝试处理下面的mysql_fetch_array查询并简化代码,因此只为这两个集合运行了1个查询,这是可能的
<select name=[set1]>
<?php
$set1 = mysql_fetch_array(mysql_query("SELECT `Locale` FROM `language` WHERE `Setting` = '1' ORDER BY FormatSet"));
while($row = $set1){
echo "<option value=\"$set1\">$set1</option>\n";
}
?>
</select>
<select name=[set2]>
<?php
$set2 = mysql_fetch_array(mysql_query("SELECT `Locale` FROM `language` WHERE `Setting` = '2' ORDER BY FormatSet"));
while($row = $set2){
echo "<option value=\"$set2\">$set2</option>\n";
}
?>
</select>
答案 0 :(得分:1)
<?php
$textSet1 = '<select name=[set1]>';
$textSet2 = '<select name=[set2]>';
$set = mysql_query("SELECT `Locale`, `Setting` FROM `language` WHERE `Setting` in ('1','2') ORDER BY FormatSet");
while($row = mysql_fetch_array($set)){
if ($row['Setting'] == '1')
$textSet1 .= '<option value="'.$row['Locale'].'">'.$row['Locale'].'</option>';
else
$textSet2 .= '<option value="'.$row['Locale'].'">'.$row['Locale'].'</option>';
}
$textSet1 .= '</select>';
$textSet2 .= '</select>';
echo $textSet1;
echo $textSet2;
?>
答案 1 :(得分:-1)
如果我理解你的问题,你想同时执行两次查询和一半代码。
我建议您更改查询以一次性获取所有记录,并使用标记为每个新设置创建新选择,这应该可以,并且可以轻松添加更多设置:
$set = mysql_fetch_assoc(mysql_query("SELECT `Locale` FROM `language` WHERE `Setting` = '1' OR 'Setting' = '2' ORDER BY Setting, FormatSet"));
$setting = 0;
$close = false;
foreach ($set as $row)
{
if ($row['Setting'] > $setting)
{
$setting = $row['Setting'];
echo "<select name=[set{$setting}]>";
// Set flag to close select
$close = true;
}
echo "<option value=\"{$row['Locale']}\">{$row['Locale']}</option>\n";
if ($close)
{
echo '</select>';
$close = false;
}
}