Returning Formatted Number in PHP

Hi There,

I want to have my php document return data in a specific format. I want the php document to pull a currency format from myphpadmin in the following format. 25d 35c I have it set in MS Access but I cannot find a way to convert it in MYPHPAdmin or writing in the actual php document.

[php]$iPartID = $_GET[“PartID”];

$oIteminfo = mysql_query("
SELECT Items.PartID, Items.VendorCost, Items.ProductName, Sum(Sales.AmountSold) AS SumOfAmountSold
FROM Items
LEFT JOIN Sales
ON Items.PartID = Sales.PartID
GROUP BY Items.PartID, Items.VendorCost, Items.ProductName
HAVING Items.PartID=$iPartID;

") or die(mysql_error());

while($row = mysql_fetch_array($oIteminfo))
{
ECHO “

WOWAuctionSales

”;
ECHO “<img src=”/images/".$iPartID.".jpg">";
ECHO “

”.$row[‘ProductName’]."


";
ECHO “Total Sold:”.$row[‘SumOfAmountSold’]."
";
ECHO “Vendor Price:”.$row[‘VendorCost’]."
";
} 

?>

[/php]

I want the Vendorcost to be the type that returns the 25d 15c

Thanks.

That is a very odd format. You may have to parse the values and use fprint for the output. I am surprised that Access does it, unless you set a mask to do so.

Thanks, I am not to familiar with Fprintf, I did some quick research on it. I have set up the following string but I am getting a syntax error and cannot seem to solve it, I also changed the format that I want the currency in.[php] {
ECHO “

WOWAuctionSales

”;
ECHO “<img src=“http://www.wowauctionsales.com/images/".$iPartID.".jpg"height=‘35’ width=‘35’>”;
ECHO “

”.$row[‘ProductName’].”


";
ECHO “Total Sold:”.$row[‘SumOfAmountSold’]."
";
ECHO fprintf([‘VendorCost’], 0’g '00’s '00’c);
ECHO “Vendor Price:”.$row[‘VendorCost’]."
";
[/php]

The problem with the syntax I am having is here.

[php]ECHO fprintf([‘VendorCost’], 0’g '00’s '00’c);
ECHO “Vendor Price:”.$row[‘VendorCost’]."
";
[/php]

For example I am currently getting an output of 164623 and I want that to be formatted as 16g 46s 23c I have searched everywhere to figure it out, I have seen other websites using this formatting but I cannot seem to figure out how to do it myself.

From doing more research I get the idea that you can assign part of the fprintf or sprintf to parse a number, but I need it to pull the number into three categories, how would I pull

[php]ECHO “Vendor Price:”.$row[‘VendorCost’]."
";
[/php]

and have it seperate into three different expression. “00g 00s 00c”

Well, you could use substr or a regular expression. I would imagine you want the last two digits and the cents, the next 3 as the single? and anything after as the g?

anything for g 2 digits for s and 2 digits for c

So, what values can be expected in the VendorCost field from the database? I am unfamiliar with how you are wanting to format the currency. If it is a normal (local) formatting, you can use money_format with a local monetary setting.

its a currency from a video game. The results differ, for example there could be an item for 20 c so the result should be 00g 00s 20c

Okay, now I am understanding… So what values are actually stored in the database, as examples?

The Values for example are 5564 , 108235, 1255464 they int (11)

g 2 digits for s and 2 digits for c 5564 , 108235, 1255464

00g 55s 64c,
10g 82s 35c,
125g 54s 64c

???

Yeah, exactly. 100 c equals 1 s 100 s equals 1 g

I know there is a better way to do this, but I am exahsuted and not thinking straight…

This will only grab exactly 6 digits and parse them accordingly:
[php]$values = [
5564,
108235,
1255464
];

$val = [];

foreach ($values as $v) {
$v = str_pad($v, 6, 0, STR_PAD_LEFT);
$c = substr($v, -2, 2);
$s = substr($v, -4, 2);
$g = substr($v, -6, 2);

array_push($val, “{$g}g {$s}s {$c}c”);
}

echo “

”;
print_r($values);
print_r($val);
echo “
”;[/php]

Thanks for your help, I was able to take what you suggested and make it work.

You are asking for an SQL Injection attack with this obsolete code.

Sponsor our Newsletter | Privacy Policy | Terms of Service