[PHP] Adding zeros in front
Hi all, Does anyone remembers how to add zeros in front of a result number given by a query to MySQL and returned as an array, so it always shows a 4 digit number? I have: [snip] echo $row[main_group]./.$row[sub_group]; [snip] but throws out: 1/0 2/0 3/0 instead of: 0001/0 0002/0 0003/0 which it should. Thanks in advance, Cesar Aracena mailto:[EMAIL PROTECTED] CE / MCSE+I Neuquen, Argentina +54.299.6356688 +54.299.4466621
Re: [PHP] Adding zeros in front
I think certain types of INT fields (not sure which ones) zero fill the number to the field length... you should read up on the col types, and maybe change it over. Maybe do a search on zerofill / zero fill. Anyhoo, to solve your immediate problem, you could check the length of $row[main_group], and add some zeros to the start of it, until it's length = your desired length. ? // UNTESTED $row[main_group] = 45; $desiredLength = 4; while(strlen($row[main_group] $desiredLength)) { $row[main_group] = 0.$row[main_group]; } ? I'm sure there are other ways, and it'd be best if you wrapped it in a function :) Justin French on 11/06/02 1:11 PM, César L. Aracena ([EMAIL PROTECTED]) wrote: Hi all, Does anyone remembers how to add zeros in front of a result number given by a query to MySQL and returned as an array, so it always shows a 4 digit number? I have: [snip] echo $row[main_group]./.$row[sub_group]; [snip] but throws out: 1/0 2/0 3/0 instead of: 0001/0 0002/0 0003/0 which it should. Thanks in advance, Cesar Aracena mailto:[EMAIL PROTECTED] CE / MCSE+I Neuquen, Argentina +54.299.6356688 +54.299.4466621 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Adding zeros in front
$num2 = sprintf(%4d, $num); the d might have to be something else, can't remember -Original Message- From: Justin French [mailto:[EMAIL PROTECTED]] Sent: Tuesday, June 11, 2002 1:20 PM To: César L. Aracena; PHP General List Subject: Re: [PHP] Adding zeros in front I think certain types of INT fields (not sure which ones) zero fill the number to the field length... you should read up on the col types, and maybe change it over. Maybe do a search on zerofill / zero fill. Anyhoo, to solve your immediate problem, you could check the length of $row[main_group], and add some zeros to the start of it, until it's length = your desired length. ? // UNTESTED $row[main_group] = 45; $desiredLength = 4; while(strlen($row[main_group] $desiredLength)) { $row[main_group] = 0.$row[main_group]; } ? I'm sure there are other ways, and it'd be best if you wrapped it in a function :) Justin French on 11/06/02 1:11 PM, César L. Aracena ([EMAIL PROTECTED]) wrote: Hi all, Does anyone remembers how to add zeros in front of a result number given by a query to MySQL and returned as an array, so it always shows a 4 digit number? I have: [snip] echo $row[main_group]./.$row[sub_group]; [snip] but throws out: 1/0 2/0 3/0 instead of: 0001/0 0002/0 0003/0 which it should. Thanks in advance, Cesar Aracena mailto:[EMAIL PROTECTED] CE / MCSE+I Neuquen, Argentina +54.299.6356688 +54.299.4466621 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Adding zeros in front
Try this: echo str_pad($row[main_group],4,0,STR_PAD_LEFT)./.$row[sub_group]; -Clay From: César L. Aracena [EMAIL PROTECTED] Date: Tue, 11 Jun 2002 00:11:30 -0300 To: PHP General List [EMAIL PROTECTED] Subject: [PHP] Adding zeros in front Hi all, Does anyone remembers how to add zeros in front of a result number given by a query to MySQL and returned as an array, so it always shows a 4 digit number? I have: [snip] echo $row[main_group]./.$row[sub_group]; [snip] but throws out: 1/0 2/0 3/0 instead of: 0001/0 0002/0 0003/0 which it should. Thanks in advance, Cesar Aracena mailto:[EMAIL PROTECTED] CE / MCSE+I Neuquen, Argentina +54.299.6356688 +54.299.4466621 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Adding zeros in front
You can declare the column ZEROFILL when you create the table, or use ALTER to add it now. Quote: Another extension is supported by MySQL for optionally specifying the display width of an integer value in parentheses following the base keyword for the type (for example, INT(4)). This optional width specification is used to left-pad the display of values whose width is less than the width specified for the column, but does not constrain the range of values that can be stored in the column, nor the number of digits that will be displayed for values whose width exceeds that specified for the column. When used in conjunction with the optional extension attribute ZEROFILL, the default padding of spaces is replaced with zeroes. For example, for a column declared as INT(5) ZEROFILL, a value of 4 is retrieved as 4. Note that if you store larger values than the display width in an integer column, you may experience problems when MySQL generates temporary tables for some complicated joins, as in these cases MySQL trusts that the data did fit into the original column width. ---John Holmes... -Original Message- From: Justin French [mailto:[EMAIL PROTECTED]] Sent: Monday, June 10, 2002 11:20 PM To: César L. Aracena; PHP General List Subject: Re: [PHP] Adding zeros in front I think certain types of INT fields (not sure which ones) zero fill the number to the field length... you should read up on the col types, and maybe change it over. Maybe do a search on zerofill / zero fill. Anyhoo, to solve your immediate problem, you could check the length of $row[main_group], and add some zeros to the start of it, until it's length = your desired length. ? // UNTESTED $row[main_group] = 45; $desiredLength = 4; while(strlen($row[main_group] $desiredLength)) { $row[main_group] = 0.$row[main_group]; } ? I'm sure there are other ways, and it'd be best if you wrapped it in a function :) Justin French on 11/06/02 1:11 PM, César L. Aracena ([EMAIL PROTECTED]) wrote: Hi all, Does anyone remembers how to add zeros in front of a result number given by a query to MySQL and returned as an array, so it always shows a 4 digit number? I have: [snip] echo $row[main_group]./.$row[sub_group]; [snip] but throws out: 1/0 2/0 3/0 instead of: 0001/0 0002/0 0003/0 which it should. Thanks in advance, Cesar Aracena mailto:[EMAIL PROTECTED] CE / MCSE+I Neuquen, Argentina +54.299.6356688 +54.299.4466621 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Adding zeros in front
str_pad worked!!! Thank you all... César Aracena IS / MCSE+I Neuquén, NQN (0299) 156-356688 (0299) 446-6621 -Mensaje original- De: Clay Loveless [mailto:[EMAIL PROTECTED]] Enviado el: Martes, 11 de Junio de 2002 12:35 a.m. Para: PHP-General Asunto: Re: [PHP] Adding zeros in front Try this: echo str_pad($row[main_group],4,0,STR_PAD_LEFT)./.$row[sub_group]; -Clay From: César L. Aracena [EMAIL PROTECTED] Date: Tue, 11 Jun 2002 00:11:30 -0300 To: PHP General List [EMAIL PROTECTED] Subject: [PHP] Adding zeros in front Hi all, Does anyone remembers how to add zeros in front of a result number given by a query to MySQL and returned as an array, so it always shows a 4 digit number? I have: [snip] echo $row[main_group]./.$row[sub_group]; [snip] but throws out: 1/0 2/0 3/0 instead of: 0001/0 0002/0 0003/0 which it should. Thanks in advance, Cesar Aracena mailto:[EMAIL PROTECTED] CE / MCSE+I Neuquen, Argentina +54.299.6356688 +54.299.4466621 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php