2014-07-03 158 views
-2

我有一個網頁,我可以導出一個在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

+0

什麼是你從數據庫中獲取的價格?什麼是輸出? 我想你應該在行之間加上一些'echo'聲明。這應該有所幫助。 – Krumia

+0

代碼的輸出是什麼,什麼是正確的答案? –

+0

@Krumia數量,courier_price和packing_price信息的輸出很好,但總數和總數沒有給出寫回答。 – user3519721

回答

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; 
+0

由於某種原因,在.date(「d/m/Y」,...)部分獲得解析器錯誤 – user3519721

+0

您可以用DateTime :: createFromFormat替換date(「d/m/Y」,...) ('YMD H:I:S',$ row ['date']) - > getTimestamp(); – Bireshwar

+0

只顯示錶頭:S – user3519721