phpQuestioner Posted June 6, 2007 Share Posted June 6, 2007 OK, so I have a page that connects to my database and I have prices that are displayed in the page from it using number_format. I want to be able to add a whitespace between my dollar sign and the actually number for any number that falls below $ 10,000. So I tried this: // Check To See If Price Is Below $ 10,000 if ($price < 10000) { $filler=" "; } PS: My "$price" field has not been set to number_format; I used a different variable to do that; like this: $var1=number_format($price); I put the above code below mysql_query, saved it, and tested it, but instead of only placing a whitespace between dollar signs and prices that were less then $ 10,000; it adds a white space to all prices. I put the code below in mysql_fetch_array. echo "$ $filler$var1"; So what should I do to correct this? Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/ Share on other sites More sharing options...
btherl Posted June 6, 2007 Share Posted June 6, 2007 Do you set $filler to empty if the price is >= 10000, after setting it to " " for a previous value? Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269006 Share on other sites More sharing options...
phpQuestioner Posted June 6, 2007 Author Share Posted June 6, 2007 like this: else { $filler=""; } I tried that.................. I didn't specifically do this: if ($price <= 10000) { $filler=" "; } else if ($price >= 10000) { $filler=""; } Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269007 Share on other sites More sharing options...
phpQuestioner Posted June 6, 2007 Author Share Posted June 6, 2007 Now I just did specify, like this if ($price < 10000) { $filler=" "; } else if ($price >= 10000) { $filler=""; } But it still had the same outcome; it still adds an " " between all the dollar signs and prices. Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269009 Share on other sites More sharing options...
phpQuestioner Posted June 6, 2007 Author Share Posted June 6, 2007 ??? Anyone know how I can correct this? Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269418 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 echo '$' . $filler . $var1; Try that Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269424 Share on other sites More sharing options...
gazalec Posted June 6, 2007 Share Posted June 6, 2007 i think the problem with your code was that you had a space between the dollar sign and the variable already ie echo "$ $filler$var1"; when it should have been echo "$$filler$var1"; Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269428 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 i think the problem with your code was that you had a space between the dollar sign and the variable already ie echo "$ $filler$var1"; when it should have been echo "$$filler$var1"; To do it that way it needs to be done like this: echo "\$$filler$var1"; the $ needs to be escaped in Double Quotes as it is not taken literally but instead as reference to variable names. In single quotes the $ is taken literally as seen in my post above, but you have to concat the values to the string. Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269437 Share on other sites More sharing options...
gazalec Posted June 6, 2007 Share Posted June 6, 2007 yeah i know sorry, if i was doing it i would do '$'.$filler.$var1; I was just trying to show what i mean about the space Sorry Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269445 Share on other sites More sharing options...
phpQuestioner Posted June 6, 2007 Author Share Posted June 6, 2007 frost110, how would i go about placing this: echo '$' . $filler . $var1; in my code; I am already using "print" to display my $var1 variable from my database. so how would i place your code in the "print"? Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269450 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 Umm the same way? print '$' . $filler . $var1; ... ??? Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269451 Share on other sites More sharing options...
phpQuestioner Posted June 6, 2007 Author Share Posted June 6, 2007 no, what I mean................ <?php print "<table><td>$var1</td></table>"; ?> like that, is what I am talking about I tried this <?php print "<table><td>'$' . $filler . $var1;</td></table>"; ?> AND THIS <?php $amount="'$' . $filler . $var1;"; print "<table><td>$amount</td></table>"; ?> But it just spit out this: '$' . . 123; So what do I do? Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269457 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 <?php print "<table><td>" . '$' . $filler . $var1 . "</td></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269460 Share on other sites More sharing options...
phpQuestioner Posted June 6, 2007 Author Share Posted June 6, 2007 OK, so I did this; but still get the same results. iI still add the in between all the dollar signs and the actual prices. <?php // mysql connect stuff here if ($price < 10000) { $filler=" "; } else if ($price >= 10000) { $filler=" "; } // mysql_fetch_array() here // defined database variables here echo "<table><td>" . '$' . $filler . $var1 . "</td></table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269465 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 Ok, please re-iterate what you want with an example of how you want it to be output. Use the [ code ] tags to show as that will assure us that it is being viewed properly. Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269467 Share on other sites More sharing options...
phpQuestioner Posted June 6, 2007 Author Share Posted June 6, 2007 I want to try to do this: $ 3,000 $ 5,000 $ 9,999 $ 10,000 $ 12,000 $ 15,000 See how all the dollar signs line up, no matter if it is a 4 digit price or 5+ digit price? Here is a HTML example of what it should do: <table> <tr> <td> $ 3,000 </td> </tr> <tr> <td> $ 5,000 </td> </tr> <tr> <td> $ 9,999 </td> </tr> <tr> <td> $ 10,000 </td> </tr> <tr> <td> $ 12,000 </td> </tr> <tr> <td> $ 15,000 </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269468 Share on other sites More sharing options...
per1os Posted June 6, 2007 Share Posted June 6, 2007 if ($price <= 10000) { $filler=" "; }elseif ($price <= 100000) { $filler=" "; }else { $filler = ""; } Try that out. or if ($price <= 10000) { $filler=" "; }elseif ($price <= 100000) { $filler=" "; }else { $filler = ""; } Try that. Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269478 Share on other sites More sharing options...
kenrbnsn Posted June 6, 2007 Share Posted June 6, 2007 Use a combination of str_replace(), sprintf(), and number_format(): <?php $amounts = array(1000,3000,5000,7000,9000,10000,12000,15000); echo '<pre>'; foreach ($amounts as $num) { echo '$'.str_replace(' ',' ',sprintf("%7s",number_format($num,0)))."\n"; } echo '</pre>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269488 Share on other sites More sharing options...
phpQuestioner Posted June 7, 2007 Author Share Posted June 7, 2007 Thank You Guys For All of Your Help. I finally just decided to nest another table and split up the dollar sign and the actual price into two separate sections and set some percentages and set some style to the table and each table section. It's a quick fix, that seems to have worked out "OK" for me. Quote Link to comment https://forums.phpfreaks.com/topic/54403-solved-preserve-white-space-for-numbers-less-then-a-specified-number/#findComment-269772 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.