0

我在 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>

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

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

4

3 回答 3

1

你为什么不把它也保存到数据库中呢?

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

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

然后对数组进行排序(如“ Sort multidimensional Array by Value (2) ”)并再次对其进行迭代以输出。

于 2013-01-08T16:01:05.290 回答
0

代替内联回显所有内容,将所有数据存储在一个数组中,然后您可以使用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
}
于 2013-01-08T16:19:37.850 回答
0

方法一

根据您的要求,完成此操作的最佳方法是从数据库中获取排序结果,如下所示。

SELECT * FROM table_name WHERE ORDER BY field_name desc

方法二

您可以使用 PHP 函数对结果集进行排序,例如array_multisort. 阅读以下链接以获取示例。下面显示了几个链接。谷歌获取更多链接。

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

按值排序多维数组

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

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

方法 3 - 可选:仅供参考

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

于 2013-01-08T16:21:11.983 回答