英文:
How to create an alphabetized drop down menu in PHP
问题
我正在尝试重写这段代码,以便下拉菜单按字母顺序排列:
$activeProjectDropdown .= "<option value=''>Select Project</option>";
$getInfo = "SELECT id, customer, job_name, haul_info
FROM dispatch_jobs
WHERE (:mydate BETWEEN delivery_date AND delivery_date_end)
ORDER BY customer, job_name";
$result = DB::run($getInfo, ['mydate' => $myDate]);
$dropdownOptions = []; // 用于存储下拉选项的数组
while ($row = $result->fetch(PDO::FETCH_BOTH)) {
if (!empty($row['haul_info'])) {
$haulinfo = "($row[haul_info])";
} else {
$haulinfo = "";
}
if ($checkit == $row['id']) {
$woot = 'selected=selected';
} else {
$woot = '';
}
$customerName = pdo_getName('name', 'customer', "$row[customer]");
$option = "<option value='$row[customer]|$row[id]' $woot>$customerName $haulinfo</option>";
$dropdownOptions[] = $option;
}
// 对下拉选项进行排序
sort($dropdownOptions);
// 将排序后的选项连接成一个字符串
$activeProjectDropdown .= implode("\n", $dropdownOptions);
在这个修改后的代码中,我将下拉选项存储在一个数组 $dropdownOptions
中,然后使用 sort
函数对这个数组进行排序,最后使用 implode
函数将排序后的选项连接成一个字符串,以获得按字母顺序排列的下拉菜单。
英文:
I'm trying to re-write this code so that the drop down menu is alphabetized:
$activeProjectDropdown.="<option value=''>Select Project</option>";
$getInfo = "SELECT id, customer, job_name, haul_info
FROM dispatch_jobs
WHERE (:mydate BETWEEN delivery_date AND delivery_date_end)
ORDER BY customer, job_name";
$result=DB::run($getInfo, ['mydate' => $myDate]);
while($row=$result->fetch(PDO::FETCH_BOTH)) {
if(!empty($row['haul_info'])) {
$haulinfo = "($row[haul_info])";
}else{
$haulinfo = "";
}
if($checkit == $row['id']){
$woot = 'selected=selected';
}else{
$woot = '';
}
$customerName = pdo_getName('name', 'customer', "$row[customer]");
$activeProjectDropdown.="<option value='$row[customer]|$row[id]' $woot>$customerName $haulinfo</option>\n";
}
In this code the query returns some rows from the database where customer is a numeric code which isn't in any kind of alphabetical order. Further down in the code a function called pdo_getName
is called which takes a column of name
table of customer
and the id from $row['customer']
and queries the database, returning the stringified name of the customer. Because the name isn't being retrieved until later on down the loop I'm having trouble figuring out a way that I can alphabetize the $activeProjectDropdown
. I've tried putting the $customerName
and drop down code into an associative array, then sort that by $customerName
and concat everything into a string, but that didn't work because there are duplicate keys. Down that same path, I could potentially have a nested array but I figure there must an easier solution I'm missing. Thanks for the help!
答案1
得分: 1
编写一个JOIN查询并在一次查询中获取所有数据,然后可以按客户姓名进行排序,我认为这是您要求的。
这将提高性能并简化代码。
$getInfo = "SELECT dj.id, dj.customer, dj.job_name, dj.haul_info, c.name
FROM dispatch_jobs dj
LEFT JOIN customer c ON c.id = dj.customer
WHERE (:mydate BETWEEN dj.delivery_date AND dj.delivery_date_end)
ORDER BY c.name, dj.job_name";
$result = DB::run($getInfo, ['mydate' => $myDate]);
while ($row = $result->fetch(PDO::FETCH_BOTH)) {
if (!empty($row['haul_info'])) {
$haulinfo = "($row[haul_info])";
} else {
$haulinfo = "";
}
if ($checkit == $row['id']) {
$woot = 'selected=selected';
} else {
$woot = '';
}
$activeProjectDropdown .= "<option value='$row[customer]|$row[id]' $woot>$row[name] $haulinfo</option>\n";
}
英文:
write a JOIN query and get all the data in one query then you can sort on the customers name as I think you are asking to do.
This will improve performance as well as simplify the code.
$getInfo = "SELECT dj.id, dj.customer, dj.job_name, dj.haul_info
c.name
FROM dispatch_jobs dj
LEFT JOIN customer c ON c.id = dj.customer
WHERE (:mydate BETWEEN dj.delivery_date AND dj.delivery_date_end)
ORDER BY c.name, dj.job_name";
$result=DB::run($getInfo, ['mydate' => $myDate]);
while($row=$result->fetch(PDO::FETCH_BOTH)) {
if(!empty($row['haul_info'])) {
$haulinfo = "($row[haul_info])";
}else{
$haulinfo = "";
}
if($checkit == $row['id']){
$woot = 'selected=selected';
}else{
$woot = '';
}
$activeProjectDropdown.="<option value='$row[customer]|$row[id]' $woot>$row[name] $haulinfo</option>\n";
}
答案2
得分: 0
尝试这样做:
SELECT ... ORDER BY customer ASC, job_name
这将首先按顾客(升序)对所有内容进行排序,然后在两个或更多行的顾客字段相等时,按job_name(升序,默认情况下)进行排序。
更多信息请查看这里。
英文:
Try this:
SELECT ... ORDER BY customer ASC, job_name
This sorts everything by costumer (ascending) first, and then by job_name (ascending, which is the default) whenever the costumer fields for two or more rows are equal.
more info here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论