Button php export table data

Thread Solved

Join Date: Nov 2009
Posts: 8
Reputation: jppr03 is an unknown quantity at this point 
Solved Threads: 0
jppr03 jppr03 is offline Offline
Newbie Poster

Button php export table data

 
0
  #1
Nov 6th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: jppr03 is an unknown quantity at this point 
Solved Threads: 0
jppr03 jppr03 is offline Offline
Newbie Poster
 
0
  #2
Nov 7th, 2009
please.. anyone know? :s
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 454
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #3
Nov 7th, 2009
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.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: jppr03 is an unknown quantity at this point 
Solved Threads: 0
jppr03 jppr03 is offline Offline
Newbie Poster
 
0
  #4
Nov 7th, 2009
thanks a lot for reply

  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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 454
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #5
Nov 7th, 2009
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:
  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.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 53
Reputation: K0ns3rv is an unknown quantity at this point 
Solved Threads: 6
K0ns3rv K0ns3rv is offline Offline
Junior Poster in Training
 
0
  #6
Nov 7th, 2009
Originally Posted by Atli View Post
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:
  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
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 8
Reputation: jppr03 is an unknown quantity at this point 
Solved Threads: 0
jppr03 jppr03 is offline Offline
Newbie Poster
 
0
  #7
Nov 7th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 454
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #8
Nov 7th, 2009
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 .
  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.
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 53
Reputation: K0ns3rv is an unknown quantity at this point 
Solved Threads: 6
K0ns3rv K0ns3rv is offline Offline
Junior Poster in Training
 
0
  #9
Nov 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 454
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
0
  #10
Nov 7th, 2009
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.

  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?
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC