NorthWind Posted May 16, 2006 Share Posted May 16, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/9762-case-problem/ Share on other sites More sharing options...
zq29 Posted May 16, 2006 Share Posted May 16, 2006 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]<?phpfunction 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 Link to comment https://forums.phpfreaks.com/topic/9762-case-problem/#findComment-36182 Share on other sites More sharing options...
NorthWind Posted May 17, 2006 Author Share Posted May 17, 2006 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]<?phpfunction 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] Quote Link to comment https://forums.phpfreaks.com/topic/9762-case-problem/#findComment-36520 Share on other sites More sharing options...
samshel Posted May 17, 2006 Share Posted May 17, 2006 is it a bad idea to use function called str_pad()just was curious if it is what u need....hth Quote Link to comment https://forums.phpfreaks.com/topic/9762-case-problem/#findComment-36533 Share on other sites More sharing options...
zq29 Posted May 17, 2006 Share Posted May 17, 2006 [!--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! Quote Link to comment https://forums.phpfreaks.com/topic/9762-case-problem/#findComment-36550 Share on other sites More sharing options...
kenrbnsn Posted May 17, 2006 Share Posted May 17, 2006 You can also use the sprintf() function:[code]<?phpfor ($i=1;$i<120;$i += 10) echo sprintf("%010d",$i)."<br>\n";?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/9762-case-problem/#findComment-36619 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.