我正在使用 PHP 和 MySQL DB (sugarcrm) 为 HTML 表单构建一个动态下拉框,以填充所述框。
到目前为止,对于这个我已经硬编码了很多 - 但必须有更好的方法。
必须有一种比我正在做的更有效的方法来编写这段代码。欢迎任何输入:
function services(){
mysql_connect('myhost', 'myname', 'mypass');
mysql_select_db('spaladon_sugar');
$sqlF = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'facial'";
$resultF = mysql_query($sqlF);
$sqlT = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'treatments'";
$resultT = mysql_query($sqlT);
$sqlS = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'salon'";
$resultS = mysql_query($sqlS);
$sqlWax = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'waxing'";
$resultWax = mysql_query($sqlWax);
$sqlWell = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'wellness'";
$resultWell = mysql_query($sqlWell);
$sqlH = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'haircutting'";
$resultH = mysql_query($sqlH);
$sqlM = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'makeup'";
$resultM = mysql_query($sqlM);
$sqlC = "SELECT id, type, name FROM serv1_services WHERE type LIKE 'color'";
$resultC = mysql_query($sqlC);
echo "<select name='services'>";
echo "<option value=''> - Facials - </option>";
while ($row = mysql_fetch_array($resultF)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "<br /><option value=''> - Medical Spa Treatments - </option>";
while ($row = mysql_fetch_array($resultT)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "<option value=''> - Salon - </option>";
while ($row = mysql_fetch_array($resultS)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "<option value=''> - Waxing - </option>";
while ($row = mysql_fetch_array($resultWax)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "<option value=''> - Wellness - </option>";
while ($row = mysql_fetch_array($resultWell)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "<option value=''> - Haircutting - </option>";
while ($row = mysql_fetch_array($resultH)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "<option value=''> - Makeup - </option>";
while ($row = mysql_fetch_array($resultM)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "<option value=''> - Color and Highlight - </option>";
while ($row = mysql_fetch_array($resultC)) {
echo "<option value='" . $row['name'] . "'>". $row['name'] . "</option>";
}
echo "</select>";
}