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

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

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.=&quot;&lt;option value=&#39;&#39;&gt;Select Project&lt;/option&gt;&quot;;
$getInfo = &quot;SELECT id, customer, job_name, haul_info 
            FROM dispatch_jobs 
            WHERE (:mydate BETWEEN delivery_date AND delivery_date_end) 
            ORDER BY customer, job_name&quot;;

$result=DB::run($getInfo, [&#39;mydate&#39; =&gt; $myDate]);
while($row=$result-&gt;fetch(PDO::FETCH_BOTH)) {
	if(!empty($row[&#39;haul_info&#39;])) {
		$haulinfo = &quot;($row[haul_info])&quot;;
	}else{
		$haulinfo = &quot;&quot;;
	}
	
	if($checkit == $row[&#39;id&#39;]){
		$woot = &#39;selected=selected&#39;;
	}else{
		$woot = &#39;&#39;;
	}
	
	$customerName = pdo_getName(&#39;name&#39;, &#39;customer&#39;, &quot;$row[customer]&quot;);
	$activeProjectDropdown.=&quot;&lt;option value=&#39;$row[customer]|$row[id]&#39; $woot&gt;$customerName $haulinfo&lt;/option&gt;\n&quot;;
}

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

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

$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 = &quot;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&quot;;
$result=DB::run($getInfo, [&#39;mydate&#39; =&gt; $myDate]);

while($row=$result-&gt;fetch(PDO::FETCH_BOTH)) {
    if(!empty($row[&#39;haul_info&#39;])) {
        $haulinfo = &quot;($row[haul_info])&quot;;
    }else{
        $haulinfo = &quot;&quot;;
    }

    if($checkit == $row[&#39;id&#39;]){
        $woot = &#39;selected=selected&#39;;
    }else{
        $woot = &#39;&#39;;
    }

    $activeProjectDropdown.=&quot;&lt;option value=&#39;$row[customer]|$row[id]&#39; $woot&gt;$row[name] $haulinfo&lt;/option&gt;\n&quot;;
}

答案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

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:

确定