如何在PHP中创建一个按字母顺序排列的下拉菜单

huangapple go评论92阅读模式
英文:

How to create an alphabetized drop down menu in PHP

问题

我正在尝试重写这段代码,以便下拉菜单按字母顺序排列:

  1. $activeProjectDropdown .= "<option value=''>Select Project</option>";
  2. $getInfo = "SELECT id, customer, job_name, haul_info
  3. FROM dispatch_jobs
  4. WHERE (:mydate BETWEEN delivery_date AND delivery_date_end)
  5. ORDER BY customer, job_name";
  6. $result = DB::run($getInfo, ['mydate' => $myDate]);
  7. $dropdownOptions = []; // 用于存储下拉选项的数组
  8. while ($row = $result->fetch(PDO::FETCH_BOTH)) {
  9. if (!empty($row['haul_info'])) {
  10. $haulinfo = "($row[haul_info])";
  11. } else {
  12. $haulinfo = "";
  13. }
  14. if ($checkit == $row['id']) {
  15. $woot = 'selected=selected';
  16. } else {
  17. $woot = '';
  18. }
  19. $customerName = pdo_getName('name', 'customer', "$row[customer]");
  20. $option = "<option value='$row[customer]|$row[id]' $woot>$customerName $haulinfo</option>";
  21. $dropdownOptions[] = $option;
  22. }
  23. // 对下拉选项进行排序
  24. sort($dropdownOptions);
  25. // 将排序后的选项连接成一个字符串
  26. $activeProjectDropdown .= implode("\n", $dropdownOptions);

在这个修改后的代码中,我将下拉选项存储在一个数组 $dropdownOptions 中,然后使用 sort 函数对这个数组进行排序,最后使用 implode 函数将排序后的选项连接成一个字符串,以获得按字母顺序排列的下拉菜单。

英文:

I'm trying to re-write this code so that the drop down menu is alphabetized:

  1. $activeProjectDropdown.=&quot;&lt;option value=&#39;&#39;&gt;Select Project&lt;/option&gt;&quot;;
  2. $getInfo = &quot;SELECT id, customer, job_name, haul_info
  3. FROM dispatch_jobs
  4. WHERE (:mydate BETWEEN delivery_date AND delivery_date_end)
  5. ORDER BY customer, job_name&quot;;
  6. $result=DB::run($getInfo, [&#39;mydate&#39; =&gt; $myDate]);
  7. while($row=$result-&gt;fetch(PDO::FETCH_BOTH)) {
  8. if(!empty($row[&#39;haul_info&#39;])) {
  9. $haulinfo = &quot;($row[haul_info])&quot;;
  10. }else{
  11. $haulinfo = &quot;&quot;;
  12. }
  13. if($checkit == $row[&#39;id&#39;]){
  14. $woot = &#39;selected=selected&#39;;
  15. }else{
  16. $woot = &#39;&#39;;
  17. }
  18. $customerName = pdo_getName(&#39;name&#39;, &#39;customer&#39;, &quot;$row[customer]&quot;);
  19. $activeProjectDropdown.=&quot;&lt;option value=&#39;$row[customer]|$row[id]&#39; $woot&gt;$customerName $haulinfo&lt;/option&gt;\n&quot;;
  20. }

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[&#39;customer&#39;] 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查询并在一次查询中获取所有数据,然后可以按客户姓名进行排序,我认为这是您要求的。

这将提高性能并简化代码。

  1. $getInfo = "SELECT dj.id, dj.customer, dj.job_name, dj.haul_info, c.name
  2. FROM dispatch_jobs dj
  3. LEFT JOIN customer c ON c.id = dj.customer
  4. WHERE (:mydate BETWEEN dj.delivery_date AND dj.delivery_date_end)
  5. ORDER BY c.name, dj.job_name";
  6. $result = DB::run($getInfo, ['mydate' => $myDate]);
  7. while ($row = $result->fetch(PDO::FETCH_BOTH)) {
  8. if (!empty($row['haul_info'])) {
  9. $haulinfo = "($row[haul_info])";
  10. } else {
  11. $haulinfo = "";
  12. }
  13. if ($checkit == $row['id']) {
  14. $woot = 'selected=selected';
  15. } else {
  16. $woot = '';
  17. }
  18. $activeProjectDropdown .= "<option value='$row[customer]|$row[id]' $woot>$row[name] $haulinfo</option>\n";
  19. }
英文:

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.

  1. $getInfo = &quot;SELECT dj.id, dj.customer, dj.job_name, dj.haul_info
  2. c.name
  3. FROM dispatch_jobs dj
  4. LEFT JOIN customer c ON c.id = dj.customer
  5. WHERE (:mydate BETWEEN dj.delivery_date AND dj.delivery_date_end)
  6. ORDER BY c.name, dj.job_name&quot;;
  7. $result=DB::run($getInfo, [&#39;mydate&#39; =&gt; $myDate]);
  8. while($row=$result-&gt;fetch(PDO::FETCH_BOTH)) {
  9. if(!empty($row[&#39;haul_info&#39;])) {
  10. $haulinfo = &quot;($row[haul_info])&quot;;
  11. }else{
  12. $haulinfo = &quot;&quot;;
  13. }
  14. if($checkit == $row[&#39;id&#39;]){
  15. $woot = &#39;selected=selected&#39;;
  16. }else{
  17. $woot = &#39;&#39;;
  18. }
  19. $activeProjectDropdown.=&quot;&lt;option value=&#39;$row[customer]|$row[id]&#39; $woot&gt;$row[name] $haulinfo&lt;/option&gt;\n&quot;;
  20. }

答案2

得分: 0

尝试这样做:

  1. SELECT ... ORDER BY customer ASC, job_name

这将首先按顾客(升序)对所有内容进行排序,然后在两个或更多行的顾客字段相等时,按job_name(升序,默认情况下)进行排序。

更多信息请查看这里

英文:

Try this:

  1. 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

huangapple
  • 本文由 发表于 2020年1月4日 01:23:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定