incrementing variables with leading zeros

This is a discussion on "incrementing variables with leading zeros" within the PHP Forum section. This forum, and the thread "incrementing variables with leading zeros are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Jan 16th, 2007, 20:01
New Member
Join Date: Jan 2007
Location: Toronto
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
incrementing variables with leading zeros

Not sure if this got posted last time I tried.

Here is the code in question;

*[code]*
<?
if ($picnumber == 001)
{
echo '<center><a href="index.html"><img src="buttons/home.gif" </a><a href="pic.php?picnumber='.++$picnumber.'"><img src="buttons/next.gif" </a></center>';
}
?>
*[code]*

The problem is that I need the incremented result for $picnum to equal "002" but it increments to "2".

Is there another way to increment or a way to initialize the variable so it retains the leading zeros and always keep a three digit number?

Thanks
Saxman
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Jan 17th, 2007, 08:51
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
Re: incrementing variables with leading zeros

use a string rather than a numerical datatype.
I don't do PHP but I hope you'll understand this pseudo:

Code: Select all
Function Digits(Number,HowMany)
 L = length of number
 s = number as a string
 While L<HowMany
  s = '0'.s
 Loop
 Return s
You can normally convert a number into a string by concatinating it with another string.
number = number & "" (in ASP)
$number = $number.'' (I think, in PHP)

Then, all you gotta do is:
[code]<?
if ($picnumber == 001)
{
echo '<center><a href="index.html"><img src="buttons/home.gif" </a><a href="pic.php?picnumber='.++Digits($picnumber,2).'"><img src="buttons/next.gif" </a></center>';
}
?>[code]
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)

Last edited by spinal007; Jan 17th, 2007 at 08:55.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Jan 17th, 2007, 11:33
Ryan Fait's Avatar
Elite Veteran
Join Date: May 2006
Location: Las Vegas
Posts: 3,787
Thanks: 0
Thanked 0 Times in 0 Posts
Re: incrementing variables with leading zeros

I believe if you put the "001" in quotes, it will read it as a string instead of as a number.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Jan 17th, 2007, 12:49
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
Re: incrementing variables with leading zeros

Quote:
Originally Posted by Ryan Fait View Post
I believe if you put the "001" in quotes, it will read it as a string instead of as a number.
The thing is Ryan, he has a variable that is being incremented...
I think!
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Jan 19th, 2007, 20:59
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: incrementing variables with leading zeros

$val = sprintf("%03d",$val+1);

will take what's in $val, add 1 to it, format it to 3 digits leading 0 filled and put it back in $val.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Jan 22nd, 2007, 09:48
spinal007's Avatar
Moderator
Join Date: Mar 2004
Location: Good Ol'London
Age: 23
Posts: 1,669
Blog Entries: 1
Thanks: 1
Thanked 4 Times in 4 Posts
Re: incrementing variables with leading zeros

Quote:
Originally Posted by grahame View Post
$val = sprintf("%03d",$val+1);

will take what's in $val, add 1 to it, format it to 3 digits leading 0 filled and put it back in $val.
There you go, all you needed was a PHP expert...
LOL, I'll go back to the ASP section
Last Blog Entry: Random String in Javascript (Apr 21st, 2008)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
operator

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Incrementing numbers RubyRue ASP.NET Forum 3 Mar 12th, 2008 03:25
kerning, leading, and justified text? PicoDeath Web Page Design 7 Jan 16th, 2008 08:29
Create templates for the leading edge Terapad.com Terapad Job Opportunities 0 Aug 9th, 2007 12:57
Web Developer needed for Leading Dance Label Big fish Digital Job Opportunities 0 Apr 27th, 2007 11:54
Unable to get correct leading Audioz Web Page Design 4 Nov 14th, 2004 19:32


All times are GMT. The time now is 15:03.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42