Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is my solution.</p> <p>Instead of just echoing the database values as your process them, I dump them into an associative array with the total number of workouts as a key. Then you can sort them using ksort. Once that is done you can print out the array using a foreach loop.</p> <p>I didn't type out the full array function but it should give you an idea.</p> <pre><code>&lt;?php $count = 0; $statement = $db-&gt;query('SELECT * FROM workouts'); $workouts = array(); while ($row = mysql_fetch_array($statement)) { $total = 0; for($i = 0;$i &lt; 13; $i++) $total = $total + $row['week'.$i]; //Build the array containing each teams workouts $workouts[$total] = array('team_name' =&gt; $row['team_name'],'week1' =&gt; $row['week1']); } ksort($workouts); $rowcount = 0; foreach ($workouts as $total =&gt; $team): ?&gt; &lt;tr&gt; &lt;td&gt;&lt;?php echo $rowcount++; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['team_name']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week1']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week2']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week3']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week4']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week5']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week6']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week7']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week8']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week9']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week10']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week11']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $team['week12']; ?&gt;&lt;/td&gt; &lt;td&gt;&lt;?php echo $total ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;?php endforeach ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload