crosstab using php & mysql -


i have database table in format

date_from  date_to   charge  amount

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

01/01/15   07/01/15   agti     500
01/01/15   07/01/15   agtii    700.50
08/01/15   14/01/15   agti     330.19
08/01/15   14/01/15   agtii    540.19

now want display depending on date range given user

charge  01/01/15-07/01/15        08/01/15-14/01/15          total

==================================================================
agti     500                            330.19                          830.19
agtii    700.50                        540.19                         1240.69
====================================================================
total    1200.50                        870.38                          2070.88

if user select 01/01/15-07/01/15 date calender 01/01/15-07/01/15 column , total value come , if select 01/01/15-14/01/15 column 2 weeks , total value display. badly stuck 1 please help.. 

this not complete solution. add value site answering question title only. meaning query not dynamic , doesn't use datepickers. specified dates hardcoded can see, adjusted. reasonably starting point want be.

checkout sqlfiddle see details of database schema.

<?php  $host = "localhost"; // hostname or ip address of mysql server $user = "user"; // username mysql $pass = "pass"; //password mysql server $db = "database"; //name of database  $conn = mysqli_connect($host, $user, $pass, $db); // connect database values defined above  if (mysqli_connect_errno())   {   echo "failed connect mysql: " . mysqli_connect_error(); // show error if fail connect   }    $sql = "select charge,             sum((case date_from                     when '2015-01-01'                          amount                         else null                     end)) `week1`,                 sum((case date_from                     when '2015-01-08'                          amount                         else null                     end)) `week2`,                 sum(amount) total             charges             group charge             order charge asc";    $query = mysqli_query($sql, $conn); //define our query // leave php , define 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 php display our results.  //we want add total line @ bottom we're going need add these //start declaring counter 0 each thing want count $w1total = 0; //week 1 total $w2total = 0; // week 2 total $gtotal = 0; // week 3 total  //loop through each row in query while($row = mysqli_fetch_array($query, mysqli_assoc)){      $w1total += $row['week1']; // add values our counters     $w2total += $row['week2'];     $gtotal += $row['total'];      // build html row 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 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.   ?> 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -