473,385 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Export data in excel format using datepicker in php & mysql

hi,
I managed to export whole table using following code. Now I want to export data to excel using from and to date or using any date picker.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. //include 'connectdb.php';
  3.  
  4.  
  5. mysql_connect("localhost", "root", "123") or die(mysql_error());
  6. mysql_select_db("nadaehsan") or die(mysql_error());
  7.  
  8.  
  9.  
  10.  
  11. $count = 0;
  12. $sqlquery = "select * from main" ;
  13. $result = mysql_query($sqlquery) or die(mysql_error());  
  14. $count = mysql_num_fields($result);
  15. $header = '';
  16. $data ='';
  17.  
  18. for ($i = 0; $i < $count; $i++) {
  19.     $header .= mysql_field_name($result, $i)."\t";
  20. }
  21.  
  22. while($row = mysql_fetch_row($result))  {
  23.   $line = '';
  24.   foreach($row as $value)       {
  25.     if(!isset($value) || $value == "")  {
  26.       $value = "\t";
  27.     }   else  {
  28. # important to escape any quotes to preserve them in the data.
  29.       $value = str_replace('"', '""', $value);
  30. # needed to encapsulate data in quotes because some data might be multi line.
  31. # the good news is that numbers remain numbers in Excel even though quoted.
  32.       $value = '"' . $value . '"' . "\t";
  33.     }
  34.     $line .= $value;
  35.   }
  36.   $data .= trim($line)."\n";
  37. }
  38. # this line is needed because returns embedded in the data have "\r"
  39. # and this looks like a "box character" in Excel
  40.   $data = str_replace("\r", "", $data);
  41.  
  42.  
  43. # Nice to let someone know that the search came up empty.
  44. # Otherwise only the column name headers will be output to Excel.
  45. if ($data == "") {
  46.   $data = "\no matching records found\n";
  47. }
  48.  
  49. $count = mysql_num_fields($result);
  50.  
  51.  
  52.  
  53. # This line will stream the file to the user rather than spray it across the screen
  54.  header("Content-type: application/octet-stream");
  55. //header("Content-type: text/plain");
  56.  
  57. # replace excelfile.xls with whatever you want the filename to default to
  58. header("Content-Disposition: attachment; filename=excelfile.xls");
  59.  
  60. header("Pragma: no-cache");
  61. header("Expires: 0");
  62.  
  63. //echo $header."\n".$data;
  64. echo $header."\n".$data."\n";
  65. ?>
  66.  
  67.  


I am new in PHP and mysql so please help out me..Thanks
May 8 '15 #1
0 1454

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: AF | last post by:
I am a real novice to php and MySQL, with about a week's worth of reading and self tutoring. I have an urgent need to publish a database of information and need some guidance on how to do this. ...
3
by: zaphod | last post by:
I want to use MS Accesss as a front end for an existing MySQL database. Since Acceess doesn't have the same data types, eg. ENUM, am I tied to the smaller subset of types supported by Access? I'm...
1
by: Peter | last post by:
I manually created a Datatable and now I want to export it to my clients in Excel format. Is there an easy way of doing this?
4
by: Agnes | last post by:
I searched from previous post and got that there is a solution which export the data to export without using Excel auotmation. However, my database is SQL server, How can I amend...
0
by: balean | last post by:
Hi guys, I need help. I am trying to extract data from XML file. I am using all the fucntions needed to parse the xml data every thing working fine, but what I am looking for is to take the string...
1
by: krisvalli | last post by:
HI, This is krishna varma a software employee, working on platform .Net Technologies. Here I'm looking for CODE which can lead to export data from database to QuickBook using ASP.Net & C#....
1
George Lft
by: George Lft | last post by:
ok, first of all, i built my register page using dreamweaver tool which the codes haven been out of control. Now i'm thinking that turning over everything - by using this another set of codes. And...
2
by: benicio | last post by:
I now have completed my site using basic php/mysql Thanks to all who helped me with those errors I got. I use a ftp to publish my static website but don't how with PHP & MySQL and the...
0
by: ghjk | last post by:
I want to insert web application data to mysql database. I'm using struts, jsp and hibernet.This is my action class. public ActionForward execute(ActionMapping mapping, ActionForm form, ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.