By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,743 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,743 IT Pros & Developers. It's quick & easy.

How do I sort my mySQL table?

w33nie
P: 56
I want to sort the following table, that contains this data, in a .php page I'll use on my website.

I want to sort it by a few columns too, I want it to sort by points, then difference, then for.

The following is the code I have in my .php file.
Expand|Select|Wrap|Line Numbers
  1. <? $hostname = "***.secureserver.net"; // The mySQL DB server. 
  2. $username = "acssl_table"; // The username you created for this database. 
  3. $password = "Victoria3"; // The password you created for the username. 
  4. $usertable = "table"; // The name of the table you made. 
  5. $dbName = "acssl_table"; // This is the name of the database you made. 
  6.  
  7. MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
  8. @mysql_select_db( "$dbName") or die( "Unable to select database"); 
  9. ?> 
  10. <? 
  11. //error message (not found message)begins 
  12. $XX = "No Record Found, to search again please close this window"; 
  13. //query details table begins
  14. $query = mysql_query("SELECT * FROM `table` LIMIT 0, 30 "); 
  15. while ($row = @mysql_fetch_array($query)) 
  16. $variable1=$row["team"];
  17. $variable2=$row["played"]; 
  18. $variable3=$row["won"]; 
  19. $variable4=$row["drawn"];
  20. $variable5=$row["lost"];
  21. $variable6=$row["for"]; 
  22. $variable7=$row["against"]; 
  23. $variable8=$row["difference"]; 
  24. $variable9=$row["points"];
  25. //table layout for results 
  26.  
  27. print ("<tr>");
  28. print ("<td width='150'><span class='BodyTable'>$variable1</span></td>");
  29. print ("<td width='50' align='center'><span class='BodyTable'>$variable2</span></td>");
  30. print ("<td width='50' align='center'><span class='BodyTable'>$variable3</span></td>");
  31. print ("<td width='50' align='center'><span class='BodyTable'>$variable4</span></td>");
  32. print ("<td width='50' align='center'><span class='BodyTable'>$variable5</span></td>");
  33. print ("<td width='50' align='center'><span class='BodyTable'>$variable6</span></td>");
  34. print ("<td width='50' align='center'><span class='BodyTable'>$variable7</span></td>");
  35. print ("<td width='75' align='center'><span class='BodyTable'>$variable8</span></td>");
  36. print ("<td width='75' align='center'><span class='BodyTable'>$variable9</span></td>");
  37. print ("</tr>"); 
  38. }
  39. //below this is the function for no record!!
  40. if (!$variable1)
  41. print ("$XX");
  42. //end 
  43. ?>
Feb 9 '07 #1
Share this Question
Share on Google+
4 Replies


Motoma
Expert 2.5K+
P: 3,235
The images did not work for me.
I would suggest, however, that you use a simple ORDER BY clause when doing your SELECT:
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM `table` ORDER BY points DESC, difference ASC, for ASC LIMIT 0, 30 "); 
  2.  
Feb 9 '07 #2

w33nie
P: 56
thanks, but that doesn't completely work..
it'll only let me sort it by the Points and Difference. Whenever I try to sort using for, the entire table crashes.
why?

Even if I'm sorting by For on its own, it crashes the table.
Feb 10 '07 #3

w33nie
P: 56
heres another screenshot of the data, hopefully this one will work.

but i doubt the problem is in the actual database
Feb 10 '07 #4

ronverdonk
Expert 2.5K+
P: 4,258
thanks, but that doesn't completely work..
it'll only let me sort it by the Points and Difference. Whenever I try to sort using for, the entire table crashes.
why?

Even if I'm sorting by For on its own, it crashes the table.
Of course it sorts by Point and Difference only. That is what has been specified in the SELECT statement. For other columns, you just have to change the SELECT statement's ORDER BY sequence to get another order for other column(s).

So, when you want to sort on 'For' you issue the statement
Expand|Select|Wrap|Line Numbers
  1. $query = mysql_query("SELECT * FROM `table` ORDER BY for ASC LIMIT 0, 30 ");  
Ronald :cool:
Feb 10 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.