Good Day,
I have a page where the user are suppose to do a date-range to select the data from the database. For example, she has a payroll period of March 1 to 15, so the user should pick the calendar date using the form (shown in the figure below)
Figure 1 [payroll_date.php]
<form action='report.php'> <input type='text' name='from' class='calendar'> <input type='text' name='to' class='calendar'> <input type='submit' name='submit'>
Once the user picked the March 1 to March 15 date and clicks the submit button, this would trigger the report.php where it holds the codes for converting the data taken from the database to the excel file. Below are the codes.
Figure 2 [report.php]
header("Content-Disposition: attachment; filename=Summary".date('m-d-Y').".csv");
//connect the database
include('db.php');
//Enter the headings of the excel columns
$con="Names of Columns \n";
//Mysql query to get records from datanbase
//You can customize the query to filter from particular date and month etc...Which will depend your database structure.
if(isset($_POST['submit']))
{
$from = $_POST['from'];
$to = $_POST['to'];
$user_query = mysql_query("SELECT * FROM table_name WHERE date between '$from' and '$to' ORDER by id ASC") or die(mysql_error());
//While loop to fetch the records
while($row = mysql_fetch_array($user_query))
{
$con.=$row['first_name'].$row['lasts_name'].",";
}
$con = strip_tags($con);
//header to make force download the file
print $con;
}
But when I tried this code I frequently get this error. ` header("Content-Disposition:attachment,filename=Summary".date('m-d-Y').".csv"); header('Content-Type:text/html;charset=utf-8'); Warning cannot modify header info-headers already sent by...
I suspect the header for Content-Disposition is the issue here. Coz, the report.php page is located at a certain directory which is named Payroll (a folder/directory) whilst the index.php is outside this folder. Am pretty sure all of you knows the purpose of the index page and how it connects different php files from different directory.
inside the index.php is the code for header Content-Typ: text/html.
when I placed the header for Content-Disposition (the once containing the convertion of mysql data to excel (.csv) format. It works fine, it indeed convert the data into excel but along with the source code! and also whenever I tried to use other navigation or links to the page, it keeps on doing a forced download.
But when I moved it back to report.php, I get the same error message and it didn't convert the whole date into excel (.csv) format.
Anyone who once had the same issue but get solved? Please give me some advice. I understand the logic behind it, which is am certain that has something to do with the header thingy. But I don't know if it was the index page is the problem or the source code for header.
Aucun commentaire:
Enregistrer un commentaire