2015-07-02 71 views
0

我有一個數據庫表在該格式交叉使用PHP和MySQL

Date_from   DATE_TO  電荷 量

================= ===================

  15年1月1日15年7月1日  AGTI   15年1月1日  07/01/15   AGTII     700.50
15年8月1日  14/01/15   AGTI     330.19
15年8月1日  14/01/15   AGTII     540.19

現在我要像顯示它這依賴於由用戶

電荷給定的時間範圍  01/01/15-07/1月15日        8月1日/ 15-14/1月15日         總

======================================= ===========================
AGTI                             330.19                           830.19
AGTII     700.50                         540.19                         1240.69
========================================== ==========================
總    1200.50                         870。38                           2070.88

如果用戶選擇01/01/15-07/03/15非日期壓延然後僅01/01/15-07/01/15列和總價值將會來,如果選擇01/01/15-14/01/15列爲2周和t 總計值將顯示。我嚴重卡住這個請幫助..  

+3

你試過什麼?你可以發佈你的嘗試 – user1844933

+0

我做了這一個..我從stackoverflow得到。但我怎麼能得到行和列明智的總結https://stackoverflow.com/questions/22200229/how-do-i-create-a-crosstab-table-with-php-mysql/31176631#31176631 –

+0

請發表爲您的數據庫創建TABLE語句。日期值是以字符串還是MySQL日期時間格式存儲的? –

回答

0

這不是一個完整的解決方案。但是,它只通過回答問題標題來增加網站的價值。意思是這個查詢不是動態的,並且不使用datepickers。您可以看到指定的日期是硬編碼的,但很容易調整。然而,這會讓你有一個相當好的起點,去你想要的地方。

結算此SQLfiddle查看數據庫模式的詳細信息。

<?php 

$host = "localhost"; // the hostname or ip address of the mysql server 
$user = "user"; // this is the username for mysql 
$pass = "pass"; //password for mysql server 
$db = "database"; //name of your database 

$conn = mysqli_connect($host, $user, $pass, $db); // connect to the database with the values we defined above 

if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); // show an error if we fail to connect 
    } 

    $sql = "SELECT charge, 
      SUM((CASE Date_from 
        WHEN '2015-01-01' 
         THEN amount 
         ELSE NULL 
        END)) AS `week1`, 
       SUM((CASE Date_from 
        WHEN '2015-01-08' 
         THEN amount 
         ELSE NULL 
        END)) AS `week2`, 
       SUM(amount) AS total 
      FROM charges 
      GROUP BY charge 
      ORDER BY charge ASC"; 

    $query = mysqli_query($sql, $conn); //define our query 
// leave php and define some html table headers 
?> 
<table> 
    <tr> 
     <th>Charge</th> 
     <th>01/01/15-07/01/15</th> 
     <th>08/01/15-14/01/15 </th> 
     <th>Total</th> 
    </tr> 
<?php //now back to php to display our results. 

//we want to add a TOTAL line at the bottom so we're going to need to add these up 
//start by declaring a counter as zero for each thing we want to count 
$w1total = 0; //week 1 total 
$w2total = 0; // week 2 total 
$gtotal = 0; // week 3 total 

//loop through each row in the query 
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 

    $w1total += $row['week1']; // add the values to our counters 
    $w2total += $row['week2']; 
    $gtotal += $row['total']; 

    // build a html row for each of our results 
    echo "<tr>"; 
     echo "<td>".$row['charge']."</td>"; 
     echo "<td>".$row['week1']."</td>"; 
     echo "<td>".$row['week2']."</td>"; 
     echo "<td>".$row['total']."</td>"; 
    echo "</tr>"; 
    } 

    //and finally add our totals row 
    echo "<tr>"; 
    echo "<td>Total</td>"; 
    echo "<td>".$w1total."</td>"; 
    echo "<td>".$w2total."</td>"; 
    echo "<td>".$gtotal."</td>"; 
    echo "</tr>"; 
echo "</table>"; 

mysqli_close($conn); //close our connection when we're finished. 

?>