944,050 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2713
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 6th, 2009
0

Button php export table data

Expand Post »
Hi! I need a button in php code, that when i click on that button it export's my SELECT to a text file or excell file..

i searched.. there's a lot of program's that make that.. but i don't want that.. i want to make on my webpage a simple code button that when someone click there it save's the content of a table..



thanks a lot
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jppr03 is offline Offline
8 posts
since Nov 2009
Nov 7th, 2009
0
Re: Button php export table data
please.. anyone know? :s
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jppr03 is offline Offline
8 posts
since Nov 2009
Nov 7th, 2009
0
Re: Button php export table data
Hey.

What data are you looking to export?
Fetching data from a form and writing it to a file is fairly simple. We just have no idea what sort of data you are using, and without knowing that we can't really say anything for certain.

We need to see your code. The form that contains your data.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 7th, 2009
0
Re: Button php export table data
thanks a lot for reply

PHP Syntax (Toggle Plain Text)
  1. $db = new Banco();
  2.  
  3. $id = $_REQUEST['id'];
  4. $datai = $_REQUEST['iData'];
  5. $dataf = $_REQUEST['fData'];
  6.  
  7. $rs = $db->Query("SELECT *
  8. FROM localizacao
  9. WHERE (local_clien_id='$id') AND
  10. ((data>=to_date('$datai', 'dd/mm/yyyy')) AND
  11. (data<=to_date('$dataf', 'dd/mm/yyyy')+1))
  12. ORDER BY local_id asc");
  13.  
  14. if(pg_num_rows($rs)>0)
  15. {
  16. while($myrow = pg_fetch_assoc($rs)) {
  17. printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['latitude'], $myrow['longitude'], $myrow['altitude'], $myrow['data']);
  18. }
  19. }

OUTPUT:

http://i.imagehost.org/0085/example.jpg

i wanted to save that table content that show's in the figure.. latitude, longitude and data, to a text file or excell file...

you know now what i mean?
Last edited by jppr03; Nov 7th, 2009 at 9:12 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jppr03 is offline Offline
8 posts
since Nov 2009
Nov 7th, 2009
0
Re: Button php export table data
Ok, I see.

You can have PHP create whatever output you want out of that.

For example, to create a XML file and save it on the server, you can do:
php Syntax (Toggle Plain Text)
  1. // <snipped the DB code>
  2.  
  3. $output = '<?xml version="1.0" encoding="UTF-8"?><root>';
  4. if(pg_num_rows($rs)>0)
  5. {
  6. while($myrow = pg_fetch_assoc($rs))
  7. {
  8. $output .= sprintf('<row><latitude>%s</latitude><longitude>%s</longitude><altitude>%s</altitude><date>%s</date></row>',
  9. $myrow['latitude'], $myrow['longitude'], $myrow['altitude'], $myrow['data']);
  10. $output .= "\n";
  11. }
  12. }
  13. $output .= '</root>';
  14.  
  15. file_put_contents('/path/to/my/file.xml', $output);
Creating a Excell file is a bit more complicated (Microsoft don't play well with others), but Excell should be able to read basic XML files like these.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 7th, 2009
0
Re: Button php export table data
Click to Expand / Collapse  Quote originally posted by Atli ...
Ok, I see.

You can have PHP create whatever output you want out of that.

For example, to create a XML file and save it on the server, you can do:
php Syntax (Toggle Plain Text)
  1. // <snipped the DB code>
  2.  
  3. $output = '<?xml version="1.0" encoding="UTF-8"?><root>';
  4. if(pg_num_rows($rs)>0)
  5. {
  6. while($myrow = pg_fetch_assoc($rs))
  7. {
  8. $output .= sprintf('<row><latitude>%s</latitude><longitude>%s</longitude><altitude>%s</altitude><date>%s</date></row>',
  9. $myrow['latitude'], $myrow['longitude'], $myrow['altitude'], $myrow['data']);
  10. $output .= "\n";
  11. }
  12. }
  13. $output .= '</root>';
  14.  
  15. file_put_contents('/path/to/my/file.xml', $output);
Creating a Excell file is a bit more complicated (Microsoft don't play well with others), but Excell should be able to read basic XML files like these.
Creating a simple exel file is fairly simple, you will get the idea if you save an exel sheet as html and open it.
Basicly you can create an .xls file and fill it with html.

//K0ns3rv
Reputation Points: 11
Solved Threads: 8
Junior Poster in Training
K0ns3rv is offline Offline
86 posts
since Oct 2009
Nov 7th, 2009
0
Re: Button php export table data
thanks for helping Alti ..

how i make that, but with a button, like a button then when i click on it, it save's my content to that XML file?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jppr03 is offline Offline
8 posts
since Nov 2009
Nov 7th, 2009
0
Re: Button php export table data
Also, forgot to mention.

If you would rather send the file to the user, to allow him to download it, you can do this instead of the file_put_contents .
php Syntax (Toggle Plain Text)
  1. header('content-type: text/xml');
  2. header('content-disposition: attachment; filename=data_export.xml');
  3. echo $output;
This prompts a download box in all the major browsers.
Last edited by Atli; Nov 7th, 2009 at 11:26 am.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007
Nov 7th, 2009
0
Re: Button php export table data
I have the code for saving a file then sending it to the user, I'd be glad to help you, but i won't be back for 30 min, so stick around if you havn't gotten an answer.

//K0ns3rv
Reputation Points: 11
Solved Threads: 8
Junior Poster in Training
K0ns3rv is offline Offline
86 posts
since Oct 2009
Nov 7th, 2009
0
Re: Button php export table data
Consider this code.
It has a form where a user enters the start and end date, and the PHP code compiles a XML file with all the data from a table that were created in that time-frame.

I created it for a MySQL database, but you can easily re-write the database part to fit your database. I just don't have anything else to test on my end.

php Syntax (Toggle Plain Text)
  1. <?php
  2. if(isset($_POST['StartDate'], $_POST['EndDate']))
  3. {
  4. $dbLink = new mysqli('localhost', 'usr', 'pwd', 'dbName');
  5.  
  6. // Fetch the data
  7. $start = $dbLink->real_escape_string($_POST['StartDate']);
  8. $end = $dbLink->real_escape_string($_POST['EndDate']);
  9. $sql = "SELECT * FROM `news`
  10. WHERE `Created` BETWEEN '{$start}' AND '{$end}'";
  11. $result = $dbLink->query($sql);
  12.  
  13. // Create the XML output
  14. $output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n";
  15. if($result->num_rows > 0)
  16. {
  17. while($myrow = $result->fetch_assoc())
  18. {
  19. $output .= "\t<row>\n";
  20. foreach($myrow as $_name => $_value)
  21. {
  22. $output .= "\t\t<$_name>$_value</$_name>\n";
  23. }
  24. $output .= "\t</row>\n";
  25. }
  26. }
  27. $output .= "</root>";
  28.  
  29. // Send it as a XML file for download
  30. header('content-type: text/xml');
  31. header('content-disposition: attachment; filename=data_export.xml');
  32. echo $output;
  33. exit;
  34. }
  35. else
  36. {
  37. ?>
  38. <!DOCTYPE html>
  39. <html>
  40. <head>
  41. <title>Data Export Thingie</title>
  42. </head>
  43. <body>
  44. <h1>Export some dataz!</h1>
  45. <form action="?" method="post">
  46. <label for="StartDate">Start Date:</label>
  47. <input type="text" name="StartDate" id="StartDate" value="YYYY-MM-DD HH:MM" /><br />
  48. <label for="EndDate">End Date:</label>
  49. <input type="text" name="EndDate" id="EndDate" value="YYYY-MM-DD HH:MM" /><br />
  50. <input type="submit" />
  51. </form>
  52. </body>
  53. </html>
  54. <?php
  55. }
  56. ?>
Does that help at all?
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Dynamic Array?
Next Thread in PHP Forum Timeline: Dynamic title errors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC