Click here to Skip to main content
15,886,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to filter table in 3 filters like
date between
branchname and so on
I have tried following code to download filtered data in excel but it shows error
.
The following code works fine when I apply querry like select * from table but it doesn't work when I apply where condition.
here is php code I have tried. Please help if anyone have a solution.

What I have tried:

<?php
include_once("dbconn.php");


if(isset($_POST["getdata"])) {	
$fdate=$_POST['fromdate'];
$tdate=$_POST['todate'];
$clinicbranch=$_POST['clinicbranch'];

$newfollow=$_POST['newfollow'];
$sqlQuery = "select * from patient where billdate  between '$fdate' and '$tdate' AND (clinicbranch='$clinicbranch' OR newfollow='$newfollow')";
$resultSet = mysqli_query($conn, $sqlQuery) or die("database error:". mysqli_error($conn));
$patientData = array();
while( $patient = mysqli_fetch_assoc($resultSet) ) {
	$patientData[] = $patient;
}	

	$fileName = "spanclinic_export_".date('Ymd') . ".xls";			
	header("Content-Type: application/vnd.ms-excel");
	header("Content-Disposition: attachment; filename=\"$fileName\"");	
	$showColoumn = false;
	if(!empty($patientData)) {
	  foreach($patientData as $patientInfo) {
		if(!$showColoumn) {		 
		  echo implode("\t", array_keys($patientInfo)) . "\n";
		  $showColoumn = true;
		}
		echo implode("\t", array_values($patientInfo)) . "\n";
	  }
	}
	exit;  
}
?>
Posted
Comments
Richard MacCutchan 26-Mar-21 11:16am    
What do you mean by, "it doesn't work"? Does it produce any results or nothing? Does it cause an error in the application? Also, what is the format of the date varaibles and the date field in the database?
Member 15120888 27-Mar-21 9:25am    
"It doesn't work" means the excel file gets downloaded but it shows an error on $fdate ,$tdate,$clinicbranch and $newfollow variables.

The datatype of date is timestamp in table
And I have created simple variables that stores value received from form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900