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

sort data inside mysql database using php

100+
P: 194
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 and want the leading zeroes as it is. please tell me. how i can sort it in ascending or descening orders (numeric) thanx.

Expand|Select|Wrap|Line Numbers
  1. function seatingOrder(){
  2.  
  3.     $result = $this->database->Query("select id, appnum, name from datFrom SORT BY appnum ASC");
  4.     echo '<table cellpadding="5" cellspacing="2"  align="center">';
  5.     while($rows = mysql_fetch_assoc($result)){
  6.  
  7.     //$app = $rows["appnum"];
  8.     //$app = (int)$app;
  9.  
  10. echo '<tr><td>'.$rows["appnum"].'</td><td>'.$rows["id"].'</td><td>'.$rows["name"].'</td></tr>';
  11.  
  12.     }
  13.  
  14. echo '</table>';
  15.  
  16.  
  17. }
Aug 10 '09 #1
Share this Question
Share on Google+
2 Replies


dlite922
Expert 100+
P: 1,584
ORDER BY fieldName+0

That will convert the sort field to number, but will not touch the field itself.



Dan
Aug 10 '09 #2

100+
P: 194
Thanx Dan it worked. thanx alot
Aug 11 '09 #3

Post your reply

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