我真的不喜欢看这段代码,它对我来说很脏,需要浓缩成更严格的代码。我觉得它可以在一个查询中完成并在while循环中而不是之前进行查询...
但是,每当我尝试将SELECT放入其中时,它都会失败。我应该如何压缩这个?以下是输入表单的数据表:
<datalist id="keys">
<?php
$tile1=$db->query("SELECT * FROM YearMakeModel GROUP BY Make1, Model1");
while($storeModel=mysqli_fetch_object($tile1))
{ echo '<option value="'.$storeModel->Model1.'">'; }
$tile2=$db->query("SELECT * FROM YearMakeModel GROUP BY Make1");
while($storeMake=mysqli_fetch_object($tile2))
{ echo '<option value="'.$storeMake->Make1.'">'; }
$tile3=$db->query("SELECT * FROM YearMakeModel GROUP BY Make1, Model1");
while($storeMakeModel=mysqli_fetch_object($tile3))
{ echo '<option value="'.$storeMakeModel->Make1.' '.$storeMakeModel->Model1.'">'; }
?>
</datalist>
答案 0 :(得分:0)
您可以通过以下方式获得所有结果:
这可以按如下方式完成:
$everything=$db->query("SELECT * FROM YearMakeModel");
$formattedResults = [ "models" => [], "makes" => "makesAndModels" => [] ];
while ($row = mysqli_fetch_object($everything)) {
$formattedResults["makes"][$row->Make1] = "Make label here?";
$formattedResults["models"][$row->Model1] = "Model label here?";
$formattedResults["makesAndModels"][$row->Make1.' '.$row->Model1] = "Combined label here?";
}
<datalist id="keys">
<?php
foreach ($formattedResults as $array) {
foreach ($array as $value => $label) {
echo "<option value='$value'>";
}
} ?>
</datalist>
我希望这是你正在寻找的。 p>