Jump to content

Archived

This topic is now archived and is closed to further replies.

NorthWind

case problem

Recommended Posts

here is my code:
[code]
$zero=0;
switch ($serial)
                {
                case 'na':
                  $querycms = "SELECT * FROM cmserial";
                  $resultcms= mssql_query($querycms, $link);
                  $num_itemscm = mssql_num_rows($resultcms);
            
                
                    //$serial=('CM-'.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$num_itemscm);}
                   echo strlen($num_itemscm);
                  echo $num_itemscm;
                 switch (strlen($num_itemscm))
                    {
                        case '1':
                            if ($num_itemscm==9)
                                { $serial=('CM-'.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$num_itemscm++);}
                            else
                                { $serial=('CM-'.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$num_itemscm++);}
                        break;    

                        case '2':
                            if ($num_itemscm==99)
                                { $serial=('CM-'.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$num_itemscm++);}
                            else
                                { $serial=('CM-'.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$num_itemscm++);}
                        break;    
                    }
                
                 break;
                
                  case '':
                  $serial = '(not entered)';
                  break;
                }
[/code]

i'm trying to make the numbers look like 0000000001 rather than just 1, for serial purposes. so the serials look like this CM-0000000001. total 10 digits.
now as you know when it incrases from 9 to 10, i need to decrease a zero from the code so i used case for all lengths.

my problem is for example when we have 9 numbers in the db, it counts them and sees tat it will increase to 10 for the next lvl but instead of decreasing zeros by 1 and increasing 9 by 1 to 10 and concatting all; it simply does not run the code where
"if ($num_itemscm==9)
{ $serial=('CM-'.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$zero.$num_itemscm++);}"

and directypasses to the else section and does that.

any ideas appriciated? thanks

Share this post


Link to post
Share on other sites
There is a function called money_format() that should be able to do what you want, although in the PHP manual it says that it is available in PHP versions >= 4.3.0. I'm running 4.3.11 on my development machine and it does not recognise the function, some others are having the same issue by reading the comments section on the manual page.

If you can get it to work, great, if not, heres a shorter way of doing it than you currently have... [code]<?php
function ten($number) {
    $length = strlen($number);
    if($length < 10) {
        for($i=0; $i<(10-$length); $i++) {
            $number = "0".$number;
        }
    }
    return $number;
}

echo ten(54782); //Prints 0000054782
?>[/code]

Share this post


Link to post
Share on other sites
thanks i did it already but this is shorter as well, ill use this next time i have to use it. thanks :)

[!--quoteo(post=374226:date=May 16 2006, 04:55 PM:name=SemiApocalyptic)--][div class=\'quotetop\']QUOTE(SemiApocalyptic @ May 16 2006, 04:55 PM) [snapback]374226[/snapback][/div][div class=\'quotemain\'][!--quotec--]
There is a function called money_format() that should be able to do what you want, although in the PHP manual it says that it is available in PHP versions >= 4.3.0. I'm running 4.3.11 on my development machine and it does not recognise the function, some others are having the same issue by reading the comments section on the manual page.

If you can get it to work, great, if not, heres a shorter way of doing it than you currently have... [code]<?php
function ten($number) {
    $length = strlen($number);
    if($length < 10) {
        for($i=0; $i<(10-$length); $i++) {
            $number = "0".$number;
        }
    }
    return $number;
}

echo ten(54782); //Prints 0000054782
?>[/code]
[/quote]

Share this post


Link to post
Share on other sites


is it a bad idea to use function called

str_pad()


just was curious if it is what u need....

hth

Share this post


Link to post
Share on other sites
[!--quoteo(post=374584:date=May 17 2006, 08:01 AM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ May 17 2006, 08:01 AM) [snapback]374584[/snapback][/div][div class=\'quotemain\'][!--quotec--]
is it a bad idea to use function called

str_pad()
just was curious if it is what u need....

hth
[/quote]
Yes, that would be an easier solution - I knew I saw that function in the manual a while back, couldn't remember what it was called - After searching for it, and not finding it, I thought I imagined it! Hah!

Share this post


Link to post
Share on other sites
You can also use the sprintf() function:
[code]<?php
for ($i=1;$i<120;$i += 10)
     echo sprintf("%010d",$i)."<br>\n";
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.