I have a functioning search bar and a functioning excel writer but I'm unaware on what query I would use to export results from a search that has been made. Is this possible?
The code that I've got for the search bar is:
<?php
include ('database_conn.php');
$output = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
$query = mysqli_query($conn, "SELECT * FROM attendance WHERE stud_id LIKE '%$search%'") or die ("Could not search");
$count = mysqli_num_rows($query);
if($count == 0){
$output = "There was no search results!";
}else{
while ($row = mysqli_fetch_array($query)) {
$stud_id = $row ['stud_id'];
$module = $row ['module'];
$attendance_status = $row ['attendance_status'];
$output .='<div> '.$stud_id.' '.$module.' '.$attendance_status.'</div>';
}
}
}
?>
and then the code that I have for the excel writer is: (database connection is already on)
$output = '';
if(isset($_POST["export"]))
{
$result=mysqli_query($conn, "SELECT * FROM attendance ");
if(mysqli_num_rows($result) > 0)
{
$output .= '
<table class="table" bordered="1">
<tr>
<th>Name</th>
<th>Module</th>
<th>Status</th>
<th>Date</th>
</tr>
';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td>'.$row["stud_id"].'</td>
<td>'.$row["module"].'</td>
<td>'.$row["attendance_status"].'</td>
<td>'.$row["date"].'</td>
</tr>
';
}
$output .= '</table>';
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=download.xls');
echo $output;
}
}
?>
I'm aware at the moment that my export button will just export everything from the attendance table, I'm not sure which query I would use to make it export the search results.