PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
For openers, delete this entire section. This is a copy/paste from the top of the file and entirely unnecessary. (And if it were necessary, it should still be at the top of the file, but that's a separate issue).
<?php
include('connect.php');
$data = [];
if (isset($_GET['d1']) && isset($_GET['d2'])) {
$d1 = strtotime($_GET['d1']);
$d2 = strtotime($_GET['d2']);
if ($d1 && $d2) {
$start_date = date('Y-m-d', $d1);
$end_date = date('Y-m-d', $d2);
$result = $db->prepare("SELECT SUM(profit) FROM purchased_software WHERE sales_month BETWEEN :a AND :b");
$result->bindParam(':a', $start_date);
$result->bindParam(':b', $end_date);
$exec = $result->execute();
if ($exec) {
$data = $result->fetchAll();
}
}
}
echo "Total Profit From Software Sales: " . '£' . $row['SUM(profit)'] . "";
?>
Now let's stop and think about what you're trying to do. You're already getting all the relevant rows, you're already iterating over them, all that's missing is the sum. No need to perform a separate lookup just for that. Instead, let PHP handle the addition as you traverse the rows you've already pulled.
ianhaney wrote:is there a way to get the selected dates displayed in the box so for example it says Total Profit for dates 1/2/2016 to 29/2/2016 £amount
You're already got $start_date and $end_date as YYYY-MM-DD dates, and $d1 and $d2 as timestamps. You can simply format either of those.