@willvincent Or anyone I know you are good at this stuff, I have a group by and was wondering can the same be done with just joins and order by. I have fiddled but no luck yet.
This is rough draft only using just pdo, in real production I'd change the POST to the laravel request way, I am just trying to work out a mysql pdo query right now, in laravel I can use the getPDO-> if needed.
Here is a screen shot of output:
https://drive.google.com/file/d/0B1_PFw--3o74LUdQR1lyWE1WaEk/view?usp=sharing
This works great, and here is the code for group by:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#customers {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
}
#customers td, #customers th {
border: 1px solid #ddd;
text-align: left;
padding: 8px;
}
#customers tr:nth-child(even){background-color: #f2f2f2}
#customers tr:hover {background-color: #ddd;}
#customers th {
padding-top: 12px;
padding-bottom: 12px;
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<?php
$host = "localhost";
$db = "checkbook";
$user = "root";
$pass = "zzzzzzzzzzzz";
$conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
//$bsdate = $_POST['begindate'];
//$esdate = $_POST['enddate'];
$bsdate = $_POST['begindate'];
$esdate = $_POST['enddate'];
$btime = strtotime($bsdate);
$bdate = date('Y-m-d H:i:s', strtotime($bsdate));
$etime = strtotime($esdate);
$edate = date('Y-m-d H:i:s', strtotime($esdate));
$sql = "select distinct `account_types`.`AccountType` AS `AccountType`,`accounts`.`AccountNumber` AS `AccountNumber`,`accounts`.`AccountName` AS `AccountName`,sum(`transactions`.`Expense`) AS `Sum_Expense`,sum(`transactions`.`Income`) AS `Sum_Income` from ((`account_types` join `accounts` on((`account_types`.`AccountTypeID` = `accounts`.`AccountTypeID`))) join `transactions` on((`accounts`.`AccountID` = `transactions`.`AccountID`))) where (`transactions`.`TransactionDate` Between '$bdate' and '$edate') group by `account_types`.`AccountType`,`accounts`.`AccountNumber`,`accounts`.`AccountName`";
$q = $conn->query($sql) or die("failed!");
echo "<table id=\"customers\" style=\"width:94%;\">";
echo "<tr>";
echo "<th style=\"width:30px; text-align: left;\">type</th>";
echo "<th style=\"width:80px; text-align: left;\">acct</th>";
echo "<th style=\"width:80px; text-align: left;\">Expenses</th>";
echo "<th style=\"width:80px; text-align: left;\">income</th>";
echo "</tr>";
$ke = 0;
$ki = 0;
while ($r = $q->fetch(PDO::FETCH_ASSOC)) {
if ($r['AccountType'] == "expense") {
$ke = $ke + 1;
if ($ke < 2) {
//echo " ====" . $r['AccountType'] . "<br>";
echo "<tr>";
echo "<td style=\"width:30px;\">" . $r['AccountType'] . "</td>";
echo "</tr>";
echo "<tr>";
//echo "<td style=\"width:80px;\">" . $r['AccountType'] . "</td>";
echo "<td style=\"width:30px;\"></td>";
echo "<td style=\"width:80px;\">" . $r['AccountName'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Expense'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Income'] . "</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td style=\"width:30px;\"></td>";
echo "<td style=\"width:80px;\">" . $r['AccountName'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Expense'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Income'] . "</td>";
echo "</tr>";
}
} else {
$ki = $ki + 1;
if ($ki < 2) {
//echo " ====" . $r['AccountType'] . "<br>";
echo "<tr>";
echo "<td style=\"width:30px;\">" . $r['AccountType'] . "</td>";
echo "</tr>";
echo "<tr>";
//echo "<td style=\"width:80px;\">" . $r['AccountType'] . "</td>";
echo "<td style=\"width:30px;\"></td>";
echo "<td style=\"width:80px;\">" . $r['AccountName'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Expense'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Income'] . "</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td style=\"width:30px;\"></td>";
echo "<td style=\"width:80px;\">" . $r['AccountName'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Expense'] . "</td>";
echo "<td style=\"width:80px;\">" . $r['Sum_Income'] . "</td>";
echo "</tr>";
}
}
}
?>
</table>
</body>
</html>
Again I just want to get it in a test single page at first and I can go from there. Oh, the raw return without looping through
looks like this: https://drive.google.com/file/d/0B1_PFw--3o74M1hzcnNiUWkxc2s/view?usp=sharing
The results are correct, I just worry about group by when there is a lot more records. The query is only getting one month at a time.