0

I have created a simple html table within PHP. Here is my code: Chart -->

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

The users are populated from a database table. The file figures are populated by reading and counting the amount of files in the directory. I want to be able to automatically sort the table by the total figure so the user with the highest total figure will be on top and first and so on..so it will look similar to a league table.

I do not know how to do this. Can someone please guide me in the right direction?

Techie
  • 44,706
  • 42
  • 157
  • 243

3 Answers3

1

Why don't you save it into the database as well?

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

Otherwise, I would just get the database query for all users into an array using mysql_fetch_array, iterate over them and count the files, and store this value into the array.

Then order the array (like "Sort multidimensional Array by Value (2)") and iterate it again to output.

Community
  • 1
  • 1
b1nary
  • 289
  • 3
  • 11
0

Instead of echoing everything inline, store all data in an array, then you can use usort() to sort the array. Then simply loop through the array to echo values.

$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
}
Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • How would I store all the data that is already i variables in an array? – user1958313 Jan 08 '13 at 20:53
  • Updated answer above to show you how to store array and sort array and display array. – Pitchinnate Jan 09 '13 at 13:44
  • Thanks!Although this code doesnt work as it should - the names of the users are being pulled form the database. Can you explain the code to me and then I could maybe make changes myself? – user1958313 Jan 09 '13 at 14:48
  • Yeah inside your while loop where you pull the data out of the database you have echo's that display username, project count, etc... instead of echoing those values store them in to the $table_rows array like i gave an example in my code. – Pitchinnate Jan 09 '13 at 14:53
  • Ok I think I understand. So I should keep "$value = $Load['username']; $tmp = array_count_values($array1); $cnt = $tmp[$value];"? – user1958313 Jan 09 '13 at 15:07
  • Yeah but instead of echoing them store them into the array. – Pitchinnate Jan 09 '13 at 15:16
  • Ok thanks. The usernames and project prints correctly but the process does not.I presume this has something to do with the $value, $value2 variables both being 'username'.What do you think? – user1958313 Jan 09 '13 at 15:25
  • Did you add them into the array like I did for username and project in the example? I didn't do everything for you I was just giving you an example. You need to store $cnt2 $cnt3 and $total in the array. – Pitchinnate Jan 09 '13 at 15:29
  • I know, sorry, I realised I had made a mistake in typing my code.This was a great help!Thank you so much! – user1958313 Jan 09 '13 at 15:34
  • Do you know how I would make the first row a different colour for the highest user? – user1958313 Jan 10 '13 at 09:55
0

Method 1

According to your requirement best way to get this done is to get the sorted results from the database as below.

SELECT * FROM table_name WHERE ORDER BY field_name desc

Method 2

You can sort the result set using PHP functions like array_multisort. Read the below links for examples. Few links are shown below. Google for more links.

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

Sort Multi-dimensional Array by Value

How do I Sort a Multidimensional Array in PHP

How do I Sort a Multidimensional Array in PHP

Method 3 - Optional : Just For Your Information

You can use DataTables which is a plug-in for the jQuery Javascript library to sort the table as required.

Community
  • 1
  • 1
Techie
  • 44,706
  • 42
  • 157
  • 243