title: What's the best way add "rank" columns in PHP-generated table? Find the total / average of columns?
body: <p>I'm working on generating a PHP/MySQL report that compares current and last year's sales data for a list of retail stores. </p>
  
  <p>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.</p>
  
  <p>Here's what the table should look like.</p>
  
  <p><img src="https://i.stack.imgur.com/9cEEJ.png" alt="wanted result">
  <a href="https://i.stack.imgur.com/trEsO.png" rel="nofollow noreferrer">http://i.stack.imgur.com/trEsO.png</a></p>
  
  <p>Here's the related part of my code.</p>
  
  <pre><code>$this-&gt;result = mysql_query($totalSalesQuery);
  
      echo "&lt;table&gt;";
  
          while($rows = mysql_fetch_object($this-&gt;result))
          {
              echo "&lt;tr&gt;";
              echo "&lt;td&gt;" .$rows-&gt;store . "&lt;/td"&gt;;
              // RANK COLUMN WOULD GO HERE
              echo "&lt;td&gt;" . $rows-&gt;CurrentSales. "&lt;td&gt;";
              // LAST YEAR'S RANK WOULD GO HERE
              echo "&lt;td&gt;" . $rows-&gt;LastYrSales . "&lt;td&gt;";
              echo "&lt;td&gt;" . ($rows-&gt;CurrentSales - $rows-&gt;LastYrSales)/$rows-&gt;LastYrSales) . "&lt;td&gt;";
              echo "&lt;/tr&gt;";
          }
      echo "&lt;/table&gt;";
  </code></pre>
  
  <p>Is there a good way to use arrays to represent the columns? And then sort the arrays based on the different value? </p>
  
  <p>Also, has anyone found any brilliant ways to total and average the columns and append that to the table?</p>