You are here: Re: Small problem with PHP « PHP Programming Language « IT news, forums, messages
Re: Small problem with PHP

Posted by alpha.beta0 on 05/26/07 18:59

On May 26, 5:44 pm, Justin.Halbs...@gmail.com wrote:
> I don't believe there is a deinfed function for working like this, but
> I have created a custom function for this purpose:
>
> function addOne($n) {
> $places = array_reverse(str_split($n,1)); //split apart, and put in
> order with least place first
> foreach ($places as $p => $number) {
> $places[$p] = $number+1;
> if ($places[$p] == 10) {
> $places[$p] = 0; //this means we gotta add one to the next place as
> well, so let it go on
> } else {
> break; //this means we're in good shape and dont have to continue,
> so break out of foreach
> }
> //from now on, we need to add 1 to the next place...but lets make
> sure its there first, if not, lets just create it and add 1 now
> if ($p == count($places)-1)
> $places[$p+1] = 1;
> }
> return implode("",array_reverse($places)); //puts back in the correct
> order and returns
>
> }
>
> Note that the number is actually a string. When you set it like:
> $count = 001;
> PHP actually drops those 0s. All it knows is that it's the value of 1,
> and doesn't care about 0 places to the left of the number.
>
> So this is how you would use it in your case:
>
> $count = "001";
> foreach($Replace_With_List as $Replace_With) {
> $count = addOne($count);
>
> }
>
> Other notes:
> Notice that it will even add another place if it needs it. So if 999
> is given, the return will be 1000.
> If using PHP5, you could actually shorten this code a little by the
> use of referenceing.
> If using anything less then PHP5, you will also need a custom
> str_split function, since none is available in <5.0 versions. So here
> is mine:
>
> function str_split($str,$l=1) {
> do {
> $ret[] = substr($str,0,$l);
> $str = substr($str,$l);
> }
> while($str != "");
> return $ret;
>
> }
>
> Hope that helps! Feel free to email me with any questions.
>
> alpha.be...@googlemail.com wrote:
> > I set it to 001 at start but the next number PHP produces is
> > 2,3,4,5,etc. instead of 002,003,004,005,etc.
>
> > What will I need to change to get the latter?
>
> > ===
>
> > $Count = 001;
>
> > foreach($Replace_With_List as $Replace_With)
> > {
> > ++$Count;
> > }
>
> > ===

Thanks for all the replies, I understand now :)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация