What's the best way add “rank” columns in PHP-generated table? Find the total / average of columns?

StackOverflow https://stackoverflow.com/questions/4903771

  •  29-10-2019
  •  | 
  •  

Question

I'm working on generating a PHP/MySQL report that compares current and last year's sales data for a list of retail stores.

All of the data comes in fine from the SQL database. The sticking point is that the report needs to show how the stores are ranked this year, and what their ranking was last year.

Here's what the table should look like.

wanted result http://i.stack.imgur.com/trEsO.png

Here's the related part of my code.

$this->result = mysql_query($totalSalesQuery);

    echo "<table>";

        while($rows = mysql_fetch_object($this->result))
        {
            echo "<tr>";
            echo "<td>" .$rows->store . "</td">;
            // RANK COLUMN WOULD GO HERE
            echo "<td>" . $rows->CurrentSales. "<td>";
            // LAST YEAR'S RANK WOULD GO HERE
            echo "<td>" . $rows->LastYrSales . "<td>";
            echo "<td>" . ($rows->CurrentSales - $rows->LastYrSales)/$rows->LastYrSales) . "<td>";
            echo "</tr>";
        }
    echo "</table>";

Is there a good way to use arrays to represent the columns? And then sort the arrays based on the different value?

Also, has anyone found any brilliant ways to total and average the columns and append that to the table?

Was it helpful?

Solution

I would definitely do this on the SQL side of things.

Take a look at this discussion on how to do ranking in MySQL

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top