1

I have a problem in sorting table from information took from SQL database. I need to sort the table with PHP variable $points (which is count from two different fields in SQL database and printed as last part of the table so the points IS NOT included in SQL).

So basically I need to sort the table by $points without saving any information about points directly to the SQL database.

I wonder what would be the simplest way to do it if it is possible?

And the PHP code is down here:

if(isset($_REQUEST['button'])) {
    $connect = mysqli_connect("localhost","root","","Points_database");
    if (mysqli_connect_errno()) {
        echo "Error: ", mysqli_connect_error();
        exit();
    }
    $sql = "SELECT * FROM Points_table;";
    $query = mysqli_query($connect,$sql);
    echo "<table><tr><td>Team</td><td>Games</td><td>Wins</td><td>Tie</td><td>Losses</td><td>Goals</td><td>Points</td></tr>";
    $rowcount=0;
    while($row = mysqli_fetch_array($query,MYSQL_ASSOC)) {
        $rowcount++;
        $points= $row['wins'] * 3 + $row['tie'];
        if($rowcount%2==0) {
            echo "<tr class='rowstyle2'>";
        }
        else {
            echo "<tr>"; 
        }
        echo "<td>".$row['team']."</td><td>".$row['games']."</td><td>".$row['wins']."</td><td>".$row['tie']."</td><td>".$row['losses']."</td><td>".$row['goals']."</td><td>".$points."</td></tr>";
    }
    echo "</table>";
}
6
  • 1
    what are values for count that you created by PHP? Commented Feb 2, 2013 at 13:16
  • $rowcount is just for to make every other row in the table colored. Or if you meaned how I count the $points it is based on wins and ties from SQL database Commented Feb 2, 2013 at 13:19
  • 1
    +1 for the MS Paint table. :D Commented Feb 2, 2013 at 13:21
  • Can you just move points calculation to SQL code? Commented Feb 2, 2013 at 13:24
  • that is why I got drop off from graphic design school :( Commented Feb 2, 2013 at 13:26

1 Answer 1

1

why not?

SELECT *, (wins*3 + tie) AS pts FROM table ORDER BY pts DESC

or if you dont want calculations in SQL

SELECT * FROM table ORDER BY wins*3 + tie DESC

here it is: http://sqlfiddle.com/#!2/d2045/4

Sign up to request clarification or add additional context in comments.

5 Comments

Because it is absolute demand for this that I do not insert any data about the points into SQL. Would be very easy if I could do that :/
SELECT * FROM table ORDER BY wins*3 + tie DESC and what is this?
Seems legit. I am still trying to make $points variable with it in the original program and having some errors but will see soon if it works there.
For some reason trying to give a value for $points that way does not seem to work correctly
just replace your 7th line following: $sql = "SELECT * FROM Points_table ORDER BY wins*3 + tie DESC;"; and u'll get sorted array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.