计算总计时输出不正确

时间:2014-07-03 10:09:36

标签: php mysql

我有一个网页,我可以导出一个在Excel上可读的.csv文件。发票从我的数据库中提取数据,以使用以下列计算总计和总计:

quantity, packing_price, courier_price

我注意到当价格前面有一个'£'声音时,我的代码没有输出正确的答案。有没有办法可以使数字和货币数据类型都能正常工作?

CODE:

$output2= "";
$sql2 = mysql_query("
SELECT j.date
     , j.order_ref
     , j.quantity
     , j.packing_price
     , j.dispatch_type
     , j.courier_price 
  FROM Jobs j
 WHERE j.order_type = 'Retail International' 
   AND j.confirmed = 'Yes';
");
$columns_total2 = mysql_num_fields($sql2);

$total3 = "Total";
for ($i = 0; $i < $columns_total2; $i++)
{
    $heading2 = mysql_field_name($sql2, $i);
    $output2 .= '"'.$heading2.'",';
}

$output2 .= $total3;
$output2 .="\n";

$sum2 = 0;
while ($row = mysql_fetch_array($sql2)) {
    for ($i = 0; $i < $columns_total2; $i++) {
        $output2 .='"'.$row["$i"].'",';
    }

    $qty2 = $row['quantity'];
    $pack_price2 = $row['packing_price'];
    $dispatch2 = $row['courier_price'];
    $total2 =  (($qty2*$pack_price2) + $dispatch2);
    $total3 = $total2*1.2;
    $output2 .= $total3;
    $sum2 += $total3; // Add to overall total
    $output2 .="\n";

}

输出: http://i754.photobucket.com/albums/xx182/rache_R/Screenshot2014-07-03at113133_zpsbcc09900.png

1 个答案:

答案 0 :(得分:3)

使用此代码......

$output= "<table border='1' width='60%'><tr>";

$sql = " SELECT j.date ,
                j.order_ref ,
                j.quantity ,
                j.packing_price ,
                j.dispatch_type ,
                j.courier_price 
           FROM Jobs j
          WHERE j.order_type = 'Retail International' 
            AND j.confirmed = 'Yes' ";
$query = mysql_query($sql);

$total_columns = mysql_num_fields($query);

for ($i = 0; $i < $total_columns; $i++){
    $heading = mysql_field_name($query, $i);
    $output .= '<td>' . $heading . '</td>';

    if(($i+1) == $total_columns)  $output .= '<td>Total</td></tr>';
}

while ($row = mysql_fetch_array($query)) {
    $total_price = 0;

    $total_price =( ( $row['quantity'] * $row['packing_price'] ) + 
                      $row['courier_price'] );
    $total_price = $total_price * 1.2;
    $timestamp = DateTime::createFromFormat('Y-m-d h:i:s',  
                 $row['date'])->getTimestamp();
    $output .= '<tr>'; 
    $output .= '<td>' . date("d/m/Y", $timestamp) . '</td>';
    $output .= '<tr>'; 
    $output .= '<td>' . date("d/m/Y", strtotime($row['date']) . '</td>';
    $output .= '<td>' . $row['order_ref'] . '</td>';
    $output .= '<td>' . $row['quantity']. '</td>';
    $output .= '<td>' . $row['packing_price'] . '</td>';
    $output .= '<td>' . $row['dispatch_type'] . '</td>';
    $output .= '<td>' . $row['courier_price'] . '</td>';
    $output .= '<td>' . number_format($total_price, 2) . '</td>';
    $output .= '</tr>'; 
}

$output .= "</table>";
echo '<br>' . $output;