473,748 Members | 2,688 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I sort my mySQL table?

w33nie
56 New Member
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
4 1617
Motoma
3,237 Recognized Expert Specialist
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
56 New Member
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
56 New Member
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
4,258 Recognized Expert Specialist
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

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

Similar topics

5
10220
by: somaBoy MX | last post by:
This may be a stupid question, but what is the easiest way to sort a mysql resultset? For example I have a resulset containing a colomn 'product_name_english' and 'product_name_french' and want to sort the result on one of these colums based on a language parameter. I know how to do this in a query ("ORDER BY") but how can I do this after the query has been executed?
4
2656
by: Phil Powell | last post by:
Very simplistic but I am utterly STUMPED at this one. I have a db table "person" that I can sort by title, first_name, last_name, or city with no problems at all because "title", "first_name", "last_name", and "city" are columns in the table. The requirement is to ALSO sort by department NAME. Problem is, I can't do that in the "person" table because the column is "department_id" which is a foreign key constraint to the department...
0
1479
by: anders thoresson | last post by:
Is it possible to resort the rows in a table like in a query where using ORDER BY, but have to new sort order stored in the table structure? -- anders thoresson -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe: http://lists.mysql.com/mysql?unsub=mysql@freebsd.csie.nctu.edu.tw
8
2084
by: yellow1912 | last post by:
Ok, lets say I have 1000 items in my database (mysql) and want to display X items per page. I learned to do it by querying with the LIMIT constrain (something like this http://www.snipe.net/content/view/12/35/). The problem is I cant sort using this algorithm (or maybe I just dont know how to do it). ORDER BY didnt do the trick for me :(. The only way I can think of is to pass all the item keys/names/anything I want to sort into an array,...
9
5316
by: phillip.s.powell | last post by:
Ok, you have three tables. You're supposed to be able to not only sort (ORDER BY) according to a_name, no problem, but you must also have the ability to sort (ORDER BY) the relationship between table_a and table_b, that is, say you have this: Here is where the problem lies. I am required to be able to sort by 'Phil', no problem:
2
5664
by: news reader | last post by:
Hi, Does anoone of you know if there is already a simple application doing something like this. I would enhance / tune the missing features, but would like to avoid to start from scratch or to start from something, that has far too many features and is difficult to setup.
6
8843
by: weetat.yeo | last post by:
Hi all , I have sql statement below , SELECT serial_no,host_name,chasis_model,chasis_flash_size,chasis_dram_size, country,city,building,other,chasis_sw_version,status,chasis_eos,chasis_eol,chasis_user_field_1,chasis_user_field_2,chasis_user_field_3 FROM tbl_chassis ORDER BY country = '', country
3
2076
by: Vic Spainhower | last post by:
Hello, I have an HTML table that is being constructed from a MySQL table and displays a form that includes a check box on 1 of the fields on the form for each record. I have included in this PHP program a javascript routine called sorttable.js which is something I found on the internet for sorting tables and works quite well. The HTML table includes a check box on each record so the user can select certain records for passing to the...
2
4004
by: mfaisalwarraich | last post by:
Hi Everybody, I am using the following function to get the data from mysql database. i have entered appnum field as text filed in the database which has leading zeroes to this field like if number is 0001, 0010, 0101 (four figures). Now i got problem while sorting them as it is a string value and its giving error when im trying to convert it into integer using (int)$appnum its trimming the leading zeros. I want this field as the string field...
0
8995
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.