Jump to content

[SOLVED] Preserve White Space For Numbers Less Then A Specified Number


phpQuestioner

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>";

?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.