Ask Question

Menu


How to show total amount month and year wise PHP


database.php

<?php
$url='localhost';
$username='root';
$password='';
$conn=mysqli_connect($url,$username,$password,"student");
if(!$conn){
die('Could not Connect My Sql:' .mysql_error());
}
?>

index.php

<?php
$result = mysqli_query($conn,"select year(date) as year, month(date) as month, sum(paid) as total_amount from amount_table group by year(date), month(date)");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>How to show total amount month and year wise PHP</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<table class="table table-bordered">
<thead>
<tr>
<th>Year</th>
<th>Month</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
$monthNum = $row["month"];
$dateObj = DateTime::createFromFormat('!m', $monthNum);
$monthName = $dateObj->format('F');
?>
<tr>
<td><?php echo $row["year"]; ?></td>
<td><?php echo $monthName; ?></td>
<td><?php echo $row["total_amount"]; ?></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>
</div>
</body>
</html>


Buy Template