在PHP制作的HTML表中按字母顺序排序SQL数据

I have a directory of company folders, and each folder is named according to a company abbreviation. I am using php to make a table which creates a checkbox, a href to the directory, and then the company name (which it pulls from SQL).

Is there a way to alphabetically order the table based on the full company name pulled from SQL rather than the name of the folder. For example, in my image, 'United Stationers' would be below the other two alphabetically, but it isn't because the folder name comes first alphabetically.

The Table

Here's my PHP code:

$dir = "/var/files/companies/";
$myDirectory = opendir($dir);

$blacklist = array("Review");

while(false !== ($entryName = readdir($myDirectory))) {
    if (!in_array($entryName, $blacklist)) {
        $dirArray[] = $entryName;}}

closedir($myDirectory);
sort($dirArray);
$indexCount = count($dirArray);

//new array here for matching short/long names
$longNames=array();

$con = new mysqli($host, $user, $password, $dbname, $port, $socket)
or die ('Could not connect to the database server' . mysqli_connect_error());

$query = "Select comp_id, short_name FROM database where vid=2";

if ($stmt = $con->prepare($query)) {
    $stmt->execute();
    $stmt->bind_result($comp_id, $short_name);
    while ($stmt->fetch()) {
        $longNames[strtolower($comp_id)]=$short_name;}
    $stmt->close();}

echo ("<TABLE border=1 cellpadding=5 cellspacing=0 class= whitelinks>
");
echo ("<TR><TH>Manufacturer's Name</TH></TR>
");

for ($index=0; $index < $indexCount; $index++) {
    if (substr("$dirArray[$index]", 0, 1) != ".") {
        echo("<td>");
        echo("<input type=\"checkbox\" name=\"comp[]\" value= '$dirArray[$index]' </a>");
        echo(" ");
        echo("<a href='/master/$dirArray[$index]'\>$dirArray[$index]</a>");
        echo("- ");
        if (array_key_exists($dirArray[$index], $longNames)){
            $short = ($longNames[$dirArray[$index]]);
            echo($short); }
        echo("</td>");
        echo("</TR>
");}}

echo("</TABLE>
");
查看全部
dongzhu7329
dongzhu7329
2015/06/16 15:24
  • php
  • mysql
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复