2015-04-02 35 views
0

我的查詢從數據庫中獲取下面的數據。php代碼顯示總共列數

這是正確的,但我也需要像下面總顯示所有行的總和..

我怎麼能做到這一點...

我需要顯示最後一行totalbooking列的總和。 。

bookno bookingdate totalbooking 
401  15-03-2015  1 
401  16-03-2015  2 
404  15-03-2015  1 

      Total   4 

下面是我的代碼...

<?php 
if($_POST && isset($_POST['Submit'])) 
{ 
    if($_POST['hawkername']=='All') 
    { 
    $cityname = $_SESSION['Auth']['city']; 
    $data = $database->getRows("SELECT RE.book_no AS book, RE.bookingdate AS bookingdate, HM.hawker_name AS hawkername,SAM.tehsil AS centername, 
    COUNT(DISTINCT RE.receipt_no) AS totalbooking,SUM(DISTINCT RE.receipt_no) AS final FROM receipt_entry RE INNER JOIN hawker_master HM ON HM.id = RE.hawkername INNER JOIN sub_agent_master SAM ON SAM.id = RE.subagentname WHERE RE.city_name = :cityname GROUP BY RE.hawkername, RE.book_no",array(':cityname'=>$cityname)); 
    } 

?> 


<?php if (is_array($data)) { foreach($data as $row){ ?> 
     <tr>  
     <td><?php echo htmlspecialchars($row['book']); ?></td> 
     <td><?php echo htmlspecialchars($row['bookingdate']); ?></td>   
     <td><?php echo htmlspecialchars($row['totalbooking']); ?></td> 
     </tr> 
     <?php } } ?> 
     <tr>   
     <td>Total</td> 
     <td></td> 
     </tr> 
+0

共創建變量 - '$總= 0'並添加每個totalbooking在環 - '$總額+ = $行[ 'totalbooking'];'。然後回顯總數。 – Sean 2015-04-02 15:45:18

+0

好的謝謝它的工作謝謝.. – 2015-04-02 15:53:19

回答

0
<?php if (is_array($data)) { $total = 0; foreach($data as $row){ $total += $row['totalbooking']; ?> 
     <tr>   

     <td><?php echo htmlspecialchars($row['book']); ?></td> 
     <td><?php echo htmlspecialchars($row['bookingdate']); ?></td>   
     <td><?php echo htmlspecialchars($row['totalbooking']); ?></td> 
     </tr> 

     <?php $i++; } } ?> 
     <tr> 
     <td colspan="4"></td> 
     <td style="font-size:13px; font-weight:bold;">Total</td> 
     <td style="font-size:13px; font-weight:bold;"><?php echo $total; ?></td> 
     </tr> 
1

更改查詢到這一點:

SELECT 
    (SELECT SUM(DISTINCT RE.receipt_no) FROM FROM receipt_entry RE) as totalbooking, 
    RE.book_no AS book, RE.bookingdate AS bookingdate, 
    ...