2013-10-30 55 views
0

如何顯示所有分區明智分區總數?如何在mysql中顯示所有分區總數

例子:

我想這樣

--------------------------------------------------------------- 
    CARD NO NAME   CAR NO DIVISION DATE  AMOUNT 
    --------------------------------------------------------------- 
    80351 MANWATERTANKER 4454HM 102  2013-9-30 95.000 
    60759 TIPPER   2368BK 102  2013-8-31 77.500 
    64545 MFASIL   3334HG 102  2013-9-30 20.000 
    56565 XYAZ    93F68K 102  2013-8-31 55.000 
    --------------------------------------------------------------- 
    Total             247.500 

    --------------------------------------------------------------- 
    CARD NO NAME   CAR NO DIVISION DATE  AMOUNT 
    --------------------------------------------------------------- 
    90178 ATEEQ   8931RR 105  2013-7-31 551.000 
    43325 USMAN   6732YY 105  2013-6-30 12.500 
    55598 ALAAM   4631TT 105  2013-7-31 158.000 
    --------------------------------------------------------------- 
    Total             721.500 

    --------------------------------------------------------------- 
    CARD NO NAME   CAR NO DIVISION DATE  AMOUNT 
    --------------------------------------------------------------- 
    40678 FATHISALIM  1632AM 103  2013-6-30 454.440 
    --------------------------------------------------------------- 
    Total             454.440 

    ----------------------------------------------------------------- 
    AND OVERALL TOTAL          1423.440 
    ----------------------------------------------------------------- 

,現在的數據則顯示這樣

--------------------------------------------------------------- 
CARD NO NAME   CAR NO DIVISION DATE  AMOUNT 
--------------------------------------------------------------- 
80351 MANWATERTANKER 4454HM 102  2013-9-30 95.000 
60759 TIPPER   2368BK 102  2013-8-31 77.500 
40678 FATHISALIM  1632AM 103  2013-6-30 454.440 
90178 ATEEQ   8931RR 105  2013-7-31 551.000 
64545 MFASIL   3334HG 102  2013-9-30 20.000 
56565 XYAZ    93F68K 102  2013-8-31 55.000 
43325 USMAN   6732YY 105  2013-6-30 12.500 
55598 ALAAM   4631TT 105  2013-7-31 158.000 
--------------------------------------------------------------- 
Total             1423.440 

,這是我的代碼:

<?php 
$con = mysql_connect("localhost","root",""); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

mysql_select_db("car", $con); 
function formatMoney($number, $fractional=false) { 
    if ($fractional) { 
     $number = sprintf('%.2f', $number); 
    } 
    while (true) { 
     $replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number); 
     if ($replaced != $number) { 
      $number = $replaced; 
     } else { 
      break; 
     } 
    } 
    return $number; 
}  
$a=$_POST['from']; 
$b=$_POST['to']; 


echo "<span align='center' class='style2'>Report For The Period Of $a to $b</span>"; 
     echo "<div id='non-printable'><table class='hovertable' border='1' cellpadding='10'>"; 
     echo "<tr> <th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>"; 


// get results1 from database 
     $result1 = mysql_query("SELECT * FROM fuel where date BETWEEN '$a' AND '$b' order by division ASC"); 
       while($row = mysql_fetch_array($result1)) 
       { 



       // echo out the contents of each row into a table 
       echo "<tr>"; 
       echo '<td>' . $row['cardno'] . '</td>'; 
       echo '<td>' . $row['drivernamefuel'] . '</td>'; 
       echo '<td>' . $row['carno'] . '</td>'; 
       echo '<td>' . $row['division'] . '</td>'; 
       echo '<td>' . $row['date'] . '</td>'; 
       echo '<td>' . $row['amount'] . '</td>'; 


       echo "</tr>"; 

      //Increment the value of the Total_total variable 
      //by the salary value of one row till the while loop finishes 

      $Total_amount=$Total_amount+$row['amount']; 

    } 

    echo "<tr>"; 
    echo '<td>Total</td>'; 
    echo '<td></td>'; 
    echo '<td></td>'; 
    echo '<td></td>'; 
    echo '<td></td>'; 
    echo '<td>' . $Total_amount .'</td>'; 
    echo "</tr>"; 


     // close table> 
     echo "</table>"; 
mysql_close($con); 
?> 

回答

0

你可以嘗試這樣的事:

echo "<div id='non-printable'><table class='hovertable' border='1' cellpadding='10'>"; 
echo "<tr><th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>"; 

// get results1 from database 
$result1 = mysql_query("SELECT * FROM fuel where date BETWEEN '$a' AND '$b' order by division ASC"); 
$division = null; 
$division_total = 0; 
while($row = mysql_fetch_array($result1)) 
{ 
    if ($division !== null && $row['division'] != $division) 
    { 
     echo "<tr>"; 
     echo '<td>Total</td>'; 
     echo '<td></td>'; 
     echo '<td></td>'; 
     echo '<td></td>'; 
     echo '<td></td>'; 
     echo '<td>' . $division_total .'</td>'; 
     echo "</tr>"; 
     echo "</table>"; 

     echo "<table class='hovertable' border='1' cellpadding='10'>"; 
     echo "<tr><th>CardNo</th> <th>NAME</th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>"; 

     $division_total = 0; 
    } 
    $division = $row['division']; 

    // echo out the contents of each row into a table 
    echo "<tr>"; 
    echo '<td>' . $row['cardno'] . '</td>'; 
    echo '<td>' . $row['drivernamefuel'] . '</td>'; 
    echo '<td>' . $row['carno'] . '</td>'; 
    echo '<td>' . $row['division'] . '</td>'; 
    echo '<td>' . $row['date'] . '</td>'; 
    echo '<td>' . $row['amount'] . '</td>'; 
    echo "</tr>"; 

    //Increment the value of the Total_amount variable 
    //by the salary value of one row till the while loop finishes 
    $division_total += $row['amount']; 
    $Total_amount=$Total_amount+$row['amount']; 
} 

echo "<tr>"; 
echo '<td>Total</td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
echo '<td>' . $division_total .'</td>'; 
echo "</tr>"; 
echo "</table>"; 

echo "<table class='hovertable' border='1' cellpadding='10'>"; 
echo "<tr><th>AND OVERALL TOTAL</th> <th></th><th></th><th></th> <th></th><th>". $Total_amount ."</th></tr>"; 
echo "</table>"; 
echo "</div>"; 

在一個側面說明,我會建議你淨化你的查詢。至少使用mysql_real_escape_string()。例如

$result1 = mysql_query("SELECT * FROM fuel where date BETWEEN '".mysql_real_escape_string($a)."' AND '".mysql_real_escape_string($b)."' order by division ASC"); 

但是綁定你的參數更安全。 http://bobby-tables.com/php.html

希望這有助於!

+0

你的代碼工作正常謝謝威廉謝謝你 – msalman

+0

沒問題。很高興我能幫上忙! –

1

剛改變你的代碼的這部分...

echo "<span align='center' class='style2'>Report For The Period Of $a to $b</span>"; 
echo "<div id='non-printable'><table class='hovertable' border='1' cellpadding='10'>"; 
echo "<tr> <th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>"; 

// get results1 from database 
$currentdiv = ''; 
$total_amount = 0.00; 
$all_total_amount = 0.00; 

$result1 = mysql_query("SELECT * FROM fuel where monthdate BETWEEN '$a' AND '$b' order by division ASC"); 
while($row = mysql_fetch_array($result1)) { 
    if(trim(strtolower($row['division'])) != $currentdiv) { 
     echo "<tr>"; 
     echo '<td>Total</td>'; 
     echo '<td></td>'; 
     echo '<td></td>'; 
     echo '<td></td>'; 
     echo '<td></td>'; 
     sprintf('<td>%.2f</td>', $total_amount); 
     echo "</tr>"; 
     echo "<tr></tr>"; 
     echo "<tr><th>CardNo</th> <th>NAME/th><th>CARNO</th><th>Division</th> <th>Date</th><th>AMOUNT</th></tr>"; 

     $all_total_amount += $total_amount; 
     $total_amount = 0.00; 
     $currentdiv = trim(strtolower($row['division'])); 
    } 

    // echo out the contents of each row into a table 
    echo "<tr>"; 
    echo '<td>' . $row['cardno'] . '</td>'; 
    echo '<td>' . $row['drivernamefuel'] . '</td>'; 
    echo '<td>' . $row['carno'] . '</td>'; 
    echo '<td>' . $row['division'] . '</td>'; 
    echo '<td>' . $row['date'] . '</td>'; 
    sprintf('<td>%.2f</td>', floatval($row['amount'])); 
    echo "</tr>"; 

    $total_amount += floatval($row['amount']); 
} 

echo "<tr>"; 
echo '<td>Total</td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
sprintf('<td>%.2f</td>', $total_amount); 
echo "</tr>"; 

$all_total_amount += $total_amount; 
echo "<tr>"; 
echo '<td>All Total</td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
echo '<td></td>'; 
sprintf('<td>%.2f</td>', $all_total_amount); 
echo "</tr>"; 

// close table> 
echo "</table>"; 
+0

我替換你的代碼,但金額行不顯示任何東西,並overotal也沒有顯示任何東西 – msalman

相關問題