| 
	
 | 
 Posted by Kimmo Laine on 12/08/06 06:59 
"GarryJones" <morack@algonet.se> wrote in message  
news:1165529640.544809.174390@l12g2000cwl.googlegroups.com... 
>I have code numbers in 2 fields from a table which correspond to month 
> and date. 
> 
> (Month, Code number) 
> Field name = ml_mna 
> 1 
> 2 
> 3 
> etc up to 12 
> (Data is entered without a leading zero) 
> 
> 
> Field name = ml_dya 
> (Date, Code number) 
> 1 
> 2 
> 3 
> etc up to 31 
> (Data is entered without a leading zero) 
> 
> I can sucessfully add a leading zero if necessary to each number after 
> retrieving with select using the following.... 
> 
> $ml_mna=mysql_result($ml_upg,$i,"ml_mna"); 
> $ml_mna++; 
> if (strlen($ml_mna) < 2) { 
> $ml_mna = '0' .$ml_mna; 
> } 
> 
> for the month and 
> 
> $ml_dya=mysql_result($ml_upg,$i,"ml_dya"); 
> $ml_dya++; 
> if (strlen($ml_dya) < 2) { 
> $ml_dya = '0' .$ml_dya; 
> } 
> 
> for the day 
> 
> However, I need to display the data in date order. 
> 
> Using "order by" the dates are sorted before I add this leading zero 
> and the result is an incorrect and confusing display 
 
Well the problem might be in the database structure rather than how yo use  
the date. If the numbers are sorted like you claim - 1,30,4 - it indicates  
that the data type of the field is a textual format such as varchar rather  
than an integer. What you should be doing is use a datetime data type field  
to store the date instead of a couple of varchar fields and you'll have no  
worries at all with ORDER BY. 
 
And for simpler leading zero formatting, just use sprintf: 
echo sprintf('%02d', $m1_dya); 
 
--  
"Ohjelmoija  on  organismi  joka  muuttaa  kofeiinia  koodiksi" - lpk 
http://outolempi.net/ahdistus/ - Satunnaisesti pδivittyvδ nettisarjis 
spam@outolempi.net | rot13(xvzzb@bhgbyrzcv.arg)
 
[Back to original message] 
 |