sort data inside mysql database using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mfaisalwarraich
    New Member
    • Oct 2007
    • 194

    sort data inside mysql database using php

    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.

    Code:
    function seatingOrder(){
        
        $result = $this->database->Query("select id, appnum, name from datFrom SORT BY appnum ASC");
        echo '<table cellpadding="5" cellspacing="2"  align="center">';
        while($rows = mysql_fetch_assoc($result)){
            
        //$app = $rows["appnum"];
        //$app = (int)$app;
            
    echo '<tr><td>'.$rows["appnum"].'</td><td>'.$rows["id"].'</td><td>'.$rows["name"].'</td></tr>';
            
        }
        
    echo '</table>';
        
        
    }
    Last edited by Markus; Aug 10 '09, 10:10 AM. Reason: Added [code] tags.
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    ORDER BY fieldName+0

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



    Dan

    Comment

    • mfaisalwarraich
      New Member
      • Oct 2007
      • 194

      #3
      Thanx Dan it worked. thanx alot

      Comment

      Working...