homepage Welcome to WebmasterWorld Guest from 54.237.98.229
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Help me to code [Color and format]
smartcard

10+ Year Member



 
Msg#: 3480649 posted 9:44 am on Oct 18, 2007 (gmt 0)

I have the following code, where I wanted to:

1) Color the text display of $strPrice to RED.
2) Display the $strPrice to be formated like $2,380 instead of $2380.95 (Adding thousand comma and removing the two decimals)

Can some body please help me on this.


<?php
$strPrice = $currency_logo . " " . $arr_row["item_price"];

if ($currency_id <> 1)
{
$curresult = mysql_query("select currency_id, currency_abbr, currency_buyingrate,currency_sellingrate from currencytype where currency_id=$currency_id");
if (mysql_num_rows($curresult))
{
$currencyrow = mysql_fetch_array($curresult);
$intPrice = round($arr_row["item_price"] / $currencyrow["currency_sellingrate"],2) ;
$strPrice = $currency_logo . "" .$intPrice ;
}

}

?>
<?print ($strPrice);?>


 

Habtom

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3480649 posted 10:05 am on Oct 18, 2007 (gmt 0)

<font color="red"> <?php print ($strPrice);?> </font>

You can use <p> <span> instead and also use style to make it much more appropriate. That was to show you what you can do generally.

Number Format [php.net] should do the trick.

Habtom

I try to use the full format <?php just to avoid future incompatability and small time headaces.

Robert Poole

5+ Year Member



 
Msg#: 3480649 posted 10:07 am on Oct 18, 2007 (gmt 0)

1) change <?print ($strPrice);?>
to maybe <? print "<font color='#cc0000'>".$strPrice."</font>";?>

2) slip in a line before you print strPrice like
$strPrice = number_format($strPrice, 0);

Go team.

smartcard

10+ Year Member



 
Msg#: 3480649 posted 10:22 am on Oct 18, 2007 (gmt 0)

Habtom, Thanks for the reply.

The font color is working fine now. But the number format I am lost, since I am using the "php print" command I don't know how to incorporate it with my codes. Appreciate for any further help.

Habtom

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3480649 posted 10:28 am on Oct 18, 2007 (gmt 0)

Like Robert Poole mentioned above, you can do the following:

Replace this part:
<?print ($strPrice);?>

With the following:

<font color=red>
<?php
$strPrice = number_format($strPrice, 0);
print ($strPrice);
?>
</font>

Habtom

smartcard

10+ Year Member



 
Msg#: 3480649 posted 10:31 am on Oct 18, 2007 (gmt 0)

Robert Poole, the following is not working it is giving "0" value result.


"slip in a line before you print strPrice like
$strPrice = number_format($strPrice, 0);"

I even tired the following, but no luck:


"slip in a line before you print strPrice like
$strPrice = number_format($strPrice);"

Robert Poole

5+ Year Member



 
Msg#: 3480649 posted 10:46 am on Oct 18, 2007 (gmt 0)

okay, so do it without the ,'0' on the end so like...

$strPrice = number_format($strPrice)

smartcard

10+ Year Member



 
Msg#: 3480649 posted 10:51 am on Oct 18, 2007 (gmt 0)

I did, but still result is 0

$strPrice = number_format($strPrice);"

Robert Poole

5+ Year Member



 
Msg#: 3480649 posted 10:54 am on Oct 18, 2007 (gmt 0)

Then I would double check that you're not getting 0 anyway. Comment out the number format line and make sure you're getting a proper answer and not 0.

smartcard

10+ Year Member



 
Msg#: 3480649 posted 11:04 am on Oct 18, 2007 (gmt 0)

When I Comment out the number format line I am getting the amount but not formated eg: $2380.95 I need it as $2,380

Robert Poole

5+ Year Member



 
Msg#: 3480649 posted 11:22 am on Oct 18, 2007 (gmt 0)

hmmm... might be because you've already used the round function on a previous line. maybe try taking that out... you shouldn't need it.

Habtom

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3480649 posted 11:26 am on Oct 18, 2007 (gmt 0)

Can you post the current code you have, after you put in what we discussed earlier?

deMorte

5+ Year Member



 
Msg#: 3480649 posted 11:30 am on Oct 18, 2007 (gmt 0)

$strPrice is a string, right?

You can only use the number_format on float numbers.

You should do the number format on the number only ($intPrice) and add the currency sign after that.

smartcard

10+ Year Member



 
Msg#: 3480649 posted 11:49 am on Oct 18, 2007 (gmt 0)

Following is the current code:


<?php
$strPrice = $currency_logo . " " . $arr_row["item_price"];

if ($currency_id <> 1)
{
$curresult = mysql_query("select currency_id, currency_abbr, currency_buyingrate,currency_sellingrate from currencytype where currency_id=$currency_id");
if (mysql_num_rows($curresult))
{
$currencyrow = mysql_fetch_array($curresult);
$intPrice = round($arr_row["item_price"] / $currencyrow["currency_sellingrate"],2) ;
$strPrice = $currency_logo . "" .$intPrice ;
}
}
?>

<font color=red>
<?php
// $strPrice = number_format($strPrice, 0);
// $strPrice = number_format($strPrice);
print ($strPrice);
?>


Habtom

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3480649 posted 11:56 am on Oct 18, 2007 (gmt 0)

deMorte's answer is I think the solution to the problem.

It is caused because of the $ sign you have in the number.

[edited by: Habtom at 12:45 pm (utc) on Oct. 18, 2007]

smartcard

10+ Year Member



 
Msg#: 3480649 posted 12:44 pm on Oct 18, 2007 (gmt 0)

Yes, deMorte's answer solved the problem.

Thanks a lot for you all great helpful people on this forum.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved