Jump to content


Photo

case problem


  • Please log in to reply
5 replies to this topic

#1 NorthWind

NorthWind
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 16 May 2006 - 07:12 AM

here is my 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;
                }

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

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 May 2006 - 08:55 AM

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...
<?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
?>


#3 NorthWind

NorthWind
  • Members
  • PipPip
  • Member
  • 10 posts

Posted 17 May 2006 - 06:35 AM

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) View Post[/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...
<?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
?>
[/quote]


#4 samshel

samshel
  • Members
  • PipPipPip
  • Advanced Member
  • 837 posts

Posted 17 May 2006 - 07:01 AM



is it a bad idea to use function called

str_pad()


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

hth
Cheers,
SamShel
-----------------------------------------------
--: FUTURE IS LINUX :--
-----------------------------------------------

#5 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 17 May 2006 - 08:42 AM

[!--quoteo(post=374584:date=May 17 2006, 08:01 AM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ May 17 2006, 08:01 AM) View Post[/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!

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 May 2006 - 02:59 PM

You can also use the sprintf() function:
<?php
for ($i=1;$i<120;$i += 10)
     echo sprintf("%010d",$i)."<br>\n";
?>

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users