Hi, ive been working on a project which in web with php and mysql. My goal is simple user/admin can input two dates then the result of the query will exported in excel. here's my code which i manage to extract the query from database:
if(isset($_POST['submit'])){
// $frdate = $_POST['fr-date'];
$sel = "SELECT infotbl.barcodeid, tbltime.barcodeid, infotbl.fname, infotbl.lname, infotbl.jposition, tbltime.rectime, tbltime.recdate FROM infotbl INNER JOIN tbltime WHERE recdate BETWEEN :frdate AND :todate";
$stmt = $DBcon->prepare($sel);
$stmt->bindParam(':frdate',$_POST['fr-date']);
$stmt->bindParam(':todate',$_POST['to-date']);
$stmt->execute();
// if($stmt->rowCount() == 0){
?>
<div id="sub-table">
<div class="rep-content">
<table border="1">
<tr>
<th>Barcode</th>
<th>First Name</th>
<th>Last Name</th>
<th>Position</th>
<th>Time</th>
<th>Date</th>
</tr>
<?php
while($row = $stmt->fetch()){
?>
<tr>
<td><?php echo $row['barcodeid']; ?></td>
<td><?php echo $row['fname']; ?></td>
<td><?php echo $row['lname']; ?></td>
<td><?php echo $row['jposition']; ?></td>
<td><?php echo $row['rectime']; ?></td>
<td><?php echo $row['recdate']; ?></td>
</tr>
<?php
}
?>
</table>
</div>
</div>
<?php
// }
}
But when i exported it to excel, all dates is been exported, not my date range. here's my export code.
<?php
//2nd Attempt: Export to excel
include 'dbcon.php';
// if(isset($_POST['export'])){
$output = '';
$sql = "SELECT infotbl.barcodeid, tbltime.barcodeid, infotbl.fname, infotbl.lname,infotbl.jposition,
tbltime.rectime, tbltime.recdate FROM infotbl INNER JOIN tbltime ON infotbl.barcodeid = tbltime.barcodeid -- WHERE recdate = :frdate AND recdate = :todate";
$stmt = $DBcon->prepare($sql);
$stmt->bindParam(':frdate', $_POST['fr-date']);
$stmt->bindParam(':todate',$_POST['to-date']);
$stmt->execute();
$output .= '
<table class="table" bordered="1">
<tr>
<th>BARCODE</th>
<th>FIRST NAME</th>
<th>LAST NAME</th>
<th>POSITION</th>
<th>TIME</th>
<th>DATE</th>
</tr>
';
while($row = $stmt->fetch())
{
$output .= '
<tr>
<td>'.$row["barcodeid"].'</td>
<td>'.$row["fname"].'</td>
<td>'.$row["lname"].'</td>
<td>'.$row["jposition"].'</td>
<td>'.$row["rectime"].'</td>
<td>'.$row["recdate"].'</td>
</tr>
';
}
$output .= '</table>';
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename = Attendance_Report-" . date('Y-m-d') .".xls");
echo $output;
?>
Need help to figure this out.
Thanks.
Otep