0

データベースから結果を追加するループが発生しています。値は次の形式の数値文字列です:$$$、$$$。$$結果を合計して同じ形式で表示するのに問題があります:

function totals (){ 
    $result = mysql_query("SELECT meta_value FROM postmeta WHERE postmeta.meta_key = 'Value'");
    $value = 0;
    while( $row = mysql_fetch_array($result) ) {
        $value = $value + str_replace(',', '', $row['meta_value']);
    }
    echo "Worth $".$value."!";
};

上記を使用すると、$653987.32の価値があります。次に、コンマを再挿入する必要があるので、これを試してみます。

$value = number_format($value, 2, '.', ',');

しかし、それから私は$ 10,000.00の価値があります!

要求に応じて出力:

    original: 5,000.00   numeric: 5 
    original: 0.00   numeric: 0 
    original: 100.00   numeric: 100 
    original: 19.95   numeric: 20 
    original: 28.00   numeric: 28 
    original: 3,777.00   numeric: 3 
    original: 2,500.00   numeric: 2 
    original: 575.00   numeric: 575 
    original: 4,000.00   numeric: 4 
    original: 100.00   numeric: 100 
    original: 1,500.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 343.50   numeric: 344 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 1,070.00   numeric: 1 
    original: 305.00   numeric: 305 
    original: 1,000.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 1144.99   numeric: 1,145 
    original: 900.00   numeric: 900 
    original: 3,750.00   numeric: 3 
    original: 1,750   numeric: 1 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 200.00   numeric: 200 
    original: 619.00   numeric: 619 
    original: 200.00   numeric: 200 
    original: 50.00   numeric: 50 
    original: 0.00   numeric: 0 
    original: 2,699.00   numeric: 2 
    original: 0.00   numeric: 0 
    original: 10,000.00   numeric: 10 
    original: 500.00   numeric: 500 
    original: 186.00   numeric: 186 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 39,000.00   numeric: 39 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 8,330.00   numeric: 8 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 100.00   numeric: 100 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 4,875.00   numeric: 4 
    original: 1,000.00   numeric: 1 
    original: 3,135.00   numeric: 3 
    original: 15,000.00   numeric: 15 
    original: 400.00   numeric: 400 
    original: 75.00   numeric: 75 
    original: 100.00   numeric: 100 
    original: 6,893.00   numeric: 6 
    original: 330.00   numeric: 330 
    original: 484.96   numeric: 485 
    original: 8,600.00   numeric: 8 
    original: 2,500.00   numeric: 2 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 6,150.00   numeric: 6 
    original: 500.00   numeric: 500 
    original: 275.00   numeric: 275 
    original: 900.00   numeric: 900 
    original: 750.00   numeric: 750 
    original: 450.00   numeric: 450 
    original: 4,600.00   numeric: 4 
    original: 299.50   numeric: 300 
    original: 250.00   numeric: 250 
    original: 825.00   numeric: 825 
    original: 750.00   numeric: 750 
    original: 50.00   numeric: 50 
    original: 2,000.00   numeric: 2 
    original: 16,000   numeric: 16 
    original: 2,600.00   numeric: 2 
    original: 240.00   numeric: 240 
    original: 4,500.00   numeric: 4 
    original: 0.00   numeric: 0 
    original: 6,000.00   numeric: 6 
    original: 0.00   numeric: 0 
    original: 3,000.00   numeric: 3 
    original: 0.00   numeric: 0 
    original: 15,000.00   numeric: 15 
    original: 45,987.24   numeric: 45 
    original: 1,100.00   numeric: 1 
    original: 20,000.00   numeric: 20 
    original: 5,100.00   numeric: 5 
    original: 12,000.00   numeric: 12 
    original: 0.00   numeric: 0 
    original: 550.00   numeric: 550 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 150.00   numeric: 150 
    original: 4,500.00   numeric: 4 
    original: 1,500.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 1,758.00   numeric: 1 
    original: 450.00   numeric: 450 
    original: 400.00   numeric: 400 
    original: 28.00   numeric: 28 
    original: 20.00   numeric: 20 
    original: 50.00   numeric: 50 
    original: 20.00   numeric: 20 
    original: 22.00   numeric: 22 
    original: 99.00   numeric: 99 
    original: 500.00   numeric: 500 
    original: 500.00   numeric: 500 
    original: 33,080.00   numeric: 33 
    original: 3,000.00   numeric: 3 
    original: 5,000.00   numeric: 5 
    original: 25,000.00   numeric: 25 
    original: 25,750.00   numeric: 25 
    original: 5,000.00   numeric: 5 
    original: 1,800.00   numeric: 1 
    original: 419.75   numeric: 420 
    original: 600.00   numeric: 600 
    original: 130.00   numeric: 130 
    original: 1,000.00   numeric: 1 
    original: 650.00   numeric: 650 
    original: 650.00   numeric: 650 
    original: 285.00   numeric: 285 
    original: 949.00   numeric: 949 
    original: 500.00   numeric: 500 
    original: 798.80   numeric: 799 
    original: 279.50   numeric: 280 
    original: 369.90   numeric: 370 
    original: 0.00   numeric: 0 
    original: 5,000.00   numeric: 5 
    original: 1,995.00   numeric: 1 
    original: 1,800.00   numeric: 1 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 3,316.00   numeric: 3 
    original: 50.00   numeric: 50 
    original: 50.00   numeric: 50 
    original: 0.00   numeric: 0 
    original: 50.00   numeric: 50 
    original: 17,182.28   numeric: 17 
    original: 30,000.00   numeric: 30 
    original: 0.00   numeric: 0 
    original: 0.00   numeric: 0 
    original: 880.00   numeric: 880 
    original: 12.00   numeric: 12 
    original: 100.00   numeric: 100 
    original: 100.00   numeric: 100 
    original: 100.00   numeric: 100 
    original: 5,600.00   numeric: 5 
    original: 1,600.00   numeric: 1 
    original: 100.00   numeric: 100 
    original: 14.95   numeric: 15 
    original: 6,000.00   numeric: 6 
    original: 7,400.00   numeric: 7 
    original: 750.00   numeric: 750 
    original: 1,575.00   numeric: 1 
    original: 50.00   numeric: 50 
    original: 3,500.00   numeric: 3 
    original: 5,000.00   numeric: 5 
    original: 1,000.00   numeric: 1 
    original: 6,500.00   numeric: 6 
    original: 14,375.00   numeric: 14 
    original: 9,500.00   numeric: 9 
    original: 91,343.00   numeric: 91 
    original: 22,610.00   numeric: 22 
    original: 15,000.00   numeric: 15 
    original: 10,000.00   numeric: 10 

明らかに何かが正しくありません。誰か助けてもらえますか?

numeric total: 661,379 Original total: 661379.32
4

3 に答える 3

2

何を出力しますか?数値が大きすぎて31ビットに収まらないようですが、正確な出力が表示されるまでわかりません。

すべての$row['meta_value']値がコンマ付きの直線の数値であると確信していますか?これを試してみてください:

while($row = mysql_fetch_array($result)) {
    echo "Value: {$row['meta_value']}\n";
    $value += str_replace(',', '', $row['meta_value']);
}

echo "final value: $value\n";
echo "formatted: ".number_format($value, 2)."\n";

一部の値が期待どおりではないかと思います。

于 2012-04-26T00:49:10.560 に答える
0

または、値を浮動小数点数にキャストすることもできます。

(float)str_replace(',','',$row['meta_value'])

編集:

完全なコード:

function totals () { 
    $result = mysql_query("SELECT meta_value FROM postmeta WHERE postmeta.meta_key = 'Value'");
    $value = 0;
    while ($row = mysql_fetch_array($result)) {
        $value += (float)str_replace(',', '', $row['meta_value']);
    }
    echo "Worth \$$value!";
};
于 2012-04-26T01:19:50.530 に答える
0

クエリ自体から適切にフォーマットされたものを取得して、必要なフィールドを操作することもできます。

SELECT replace(meta_value, ',', '') as number_value, meta_value as original_value
FROM postmeta
WHERE postmeta.meta_key = 'Value'
于 2012-04-26T01:24:05.570 に答える