繁体   English   中英

用PHP代码在HTML表中排序

[英]sort a html table with in the code in PHP

我在PHP中创建了一个简单的html表。 这是我的代码:图表->

    <div id="wrapper">
        <div class="chart">
            <h2>No. of Files Uploaded to Knowledge Base</h2>
            <table id="data-table" border="1" cellpadding="10"     cellspacing="0">


            <tr>
                    <td>Users</td>
                    <td>Project Files</td>
                    <td>Process Files</td>
                    <td>System Files</td>
                    <td>Total</td>
            </tr>   

                        <?php

                        $di = new RecursiveDirectoryIterator('upload/project/');
                        foreach (new RecursiveIteratorIterator($di) as $filename => $file) {

                        $pos = 15;
                        $file = substr("$filename", +$pos); 

                        $lenght = strlen($file);
                        $pos = strpos($file, "/");
                        $file = substr("$file",0,$pos);
                        if($file1 != '.DS_Store'){

                            $serverfiles = mysql_query("SELECT uploader FROM Project WHERE location = '$file'");

                            while($row = mysql_fetch_array($serverfiles)) {
                                $occurance1 = $row['uploader'];
                                $array1[] = $occurance1; 
                                }
                            }                           
                        }




                        $di = new RecursiveDirectoryIterator('upload/process/');
                        foreach (new RecursiveIteratorIterator($di) as $filename => $file) {

                        $pos = 15;
                        $file = substr("$filename", +$pos);                         
                        $lenght = strlen($file);
                        $pos = strpos($file, "/");
                        $file = substr("$file",0,$pos);

                        if($file != '.DS_Store'){

                            $serverfiles = mysql_query("SELECT uploader FROM Process WHERE processlocation = '$file'");

                            while($row = mysql_fetch_array($serverfiles)) {
                                $occurance2 = $row['uploader'];
                                $array2[] = $occurance2; 
                                }
                            }                           
                        }

                        $di = new RecursiveDirectoryIterator('upload/system/');
                        foreach (new RecursiveIteratorIterator($di) as $filename => $file) {

                        $pos = 14;
                        $file = substr("$filename", +$pos);                         
                        $lenght = strlen($file);
                        $pos = strpos($file, "/");
                        $file = substr("$file",0,$pos);
                        if($file != '.DS_Store'){

                            $serverfiles = mysql_query("SELECT uploader FROM System WHERE location = '$file'");

                            while($row = mysql_fetch_array($serverfiles)) {
                                $occurance3 = $row['uploader'];
                                $array3[] = $occurance3; 
                                }
                            }                           
                        }

                        $uploader = mysql_query("Select username from members");
                        while($Load = mysql_fetch_array($uploader)){
                        $value = $Load['username'];
                        $tmp = array_count_values($array1);
                        $cnt = $tmp[$value];
                        echo"<tr>";
                        echo"<td>$value</td>";
                        echo "<td>$cnt</td>";


                        $value2 = $Load['username'];
                        $tmp2 = array_count_values($array2);
                        $cnt2 = $tmp2[$value2];

                        echo "<td>$cnt2</td>";

                        $value3 = $Load['username'];
                        $tmp3 = array_count_values($array3);
                        $cnt3 = $tmp3[$value3];
                        $total = $cnt + $cnt2 + $cnt3;
                        echo "<td>$cnt3</td>";
                        echo "<td>$total</td>";
                        }
                    echo "</tr>";

                        ?>

            </table>
    </div>

          </body></html>

用户是从数据库表中填充的。 通过读取和计数目录中的文件数量来填充文件图形。 我希望能够按总数自动对表格进行排序,因此总数最高的用户将排在首位,依此类推。因此它看起来类似于联赛表格。

我不知道该怎么做。 有人可以指导我正确的方向吗?

为什么不也将其保存到数据库中?

SELECT uploader FROM Project WHERE location = '$file' ORDER BY field

否则,我将使用mysql_fetch_array所有用户的数据库查询放入一个数组中,遍历他们并计数文件,然后将此值存储到数组中。

然后对数组排序 (例如“ 按值(2)对多维数组排序 ”),然后再次对其进行迭代以输出。

无需内联回显所有内容,而是将所有数据存储在数组中,然后可以使用usort()对数组进行排序。 然后只需遍历数组以回显值。

$table_rows = array(); //add this
$counter = 0; //add this
while($Load = mysql_fetch_array($uploader)){
    //...more code here but not going to put everything
    $counter++;
    $table_rows[$counter] = array();
    $table_rows[$counter]['username'] = $value;
    $table_rows[$counter]['project'] = $cnt;
    //...more code here but not going to put everything
}

function cmp_rows($a,$b) {
    if ($a['total'] == $b['total']) {
        return 0;
    }
    return ($a['total'] > $b['total']) ? -1 : 1;
}

usort($table_rows,'cmp_rows');

foreach($table_rows as $row) {
    echo "<tr>";
    echo "<td>{$row['username']}</td>";
    //...more code here but not going to put everything
}

方法1

根据您的要求,最好的方法是从数据库中获取排序结果,如下所示。

SELECT * FROM table_name WHERE ORDER BY field_name desc

方法2

您可以使用类似于array_multisort PHP函数对结果集进行排序。 阅读以下链接以获取示例。 几个链接如下所示。 谷歌获取更多链接。

http://shiflett.org/blog/2011/jun/sorting-multi-Dimension-arrays-in-php

按值对多维数组排序

如何在PHP中对多维数组进行排序

如何在PHP中对多维数组进行排序

方法3-可选:仅供参考

您可以使用jQuery Javascript库的插件DataTables根据需要对表进行排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM