Reply
PHP Number Formatting
Old 11-13-2004, 09:13 AM PHP Number Formatting
Y3000's Avatar
Junior Talker

Posts: 2
Hi,

Just a simple one!

I have written a news script which works perfectly, hurray! Anyway, I use MAX() to select the lestest news article from the MySQL database. This workd fine until you come to the number 9. MAX() doesn't like different number legths. I have come up with a solution but don't know how to do it.

I want to turn 1 into a 5 digit number... eg. 00001 and 2 to 00002 etc. So when it runs into double figures it will still be the same number length, eg. 00010, 000011 etc.

Hope you understand what I'm getting out and that someone can help me out with this one.

Many Thanks
Adam
Y3000 is offline
Reply With Quote
View Public Profile Visit Y3000's homepage!
 
When You Register, These Ads Go Away!
Old 11-13-2004, 10:11 AM
Average Talker

Posts: 16
I think you could use:

$str = sprintf("%05d", $mynumber);

and that should take mynumber and turn it into a 5 digit string.
See http://www.php.net/sprintf for more details on that function. It's similar to the C++ function is you know that.

Not sure if that helps. Might be smarter not to convert to string though, and simply find the maximum number using another method.

Edit:
Actually, now I think about it... I think your problem is the fact that you're passing strings to the function instead of ints. Maybe you could try typecasting your variables to ints, before passing them to MAX.

For example:
$var=(int)$var

Last edited by dcs3jah : 11-13-2004 at 10:30 AM.
dcs3jah is offline
Reply With Quote
View Public Profile
 
Old 11-13-2004, 11:27 AM
Y3000's Avatar
Junior Talker

Posts: 2
I sorted the problem by changing the database...

I set the ID column to INT and UNSIGNED ZEROFILL in the attributes.

I think this is what you were getting at in your edit.

Thanks
__________________
Adam Castleton
Y3000 Internet Services

Web Design, Programming, Web Hosting & Domain Registration...
Y3000 is offline
Reply With Quote
View Public Profile Visit Y3000's homepage!
 
Reply     « Reply to PHP Number Formatting
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12627 seconds with 12 queries