943,148 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1564
  • PHP RSS
Feb 6th, 2010
0

Exporting dynamic HTML table to CSV document not working properly

Expand Post »
Hi
I need to save the values printed in a html table from the database in a CSV file if requested by user. I've only managed to save the header of the table so far.
The MySql query is made in a page called reportclient.php which then calls phpReportGen.php in order to nicely diplay the data.
This is phpReportGen.php where I try to save the data in "Report.csv" located on the server:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. class phpReportGenerator
  4. {
  5. var $mysql_resource;
  6. var $header;
  7. var $foolter;
  8. var $fields = array();
  9. var $cellpad;
  10. var $cellspace;
  11. var $border;
  12. var $width;
  13. var $modified_width;
  14. var $header_color;
  15. var $header_textcolor;
  16. var $header_alignment;
  17. var $body_color;
  18. var $body_textcolor;
  19. var $body_alignment;
  20. var $surrounded;
  21.  
  22. function generateReport()
  23. {
  24. $this->border = (empty($this->border))?"0":$this->border;
  25. $this->cellpad = (empty($this->cellpad))?"1":$this->cellpad;
  26. $this->cellspace = (empty($this->cellspace))?"0":$this->cellspace;
  27. $this->width = (empty($this->width))?"100%":$this->width;
  28. $this->header_color = (empty($this->header_color))?"#FFFFFF":$this->header_color;
  29. $this->header_textcolor = (empty($this->header_textcolor))?"#000000":$this->header_textcolor;
  30. $this->header_alignment = (empty($this->header_alignment))?"left":$this->header_alignment;
  31. $this->body_color = (empty($this->body_color))?"#FFFFFF":$this->body_color;
  32. $this->body_textcolor = (empty($this->body_textcolor))?"#000000":$this->body_textcolor;
  33. $this->body_alignment = (empty($this->body_alignment))?"left":$this->body_alignment;
  34. $this->surrounded = (empty($this->surrounded))?false:true;
  35. $this->modified_width = ($this->surrounded==true)?"100%":$this->width;
  36.  
  37. //echo "modified_width : ".$this->modified_width."<br>";
  38.  
  39. if (!is_resource($this->mysql_resource))
  40. die ("User doesn't supply any valid mysql resource after executing query result!");
  41.  
  42. /*
  43. * Lets calculate how many fields are there in supplied resource
  44. * and store their name in $this->fields[] array
  45. */
  46.  
  47. $field_count = mysql_num_fields($this->mysql_resource);
  48. $i = 0;
  49.  
  50. while ($i < $field_count)
  51. {
  52. $field = mysql_fetch_field($this->mysql_resource);
  53. $this->fields[$i] = $field->name;
  54. $this->fields[$i][0] = strtoupper($this->fields[$i][0]);
  55. $i++;
  56. }
  57.  
  58.  
  59. /*
  60. * Now start table generation
  61. * We must draw this table according to number of fields
  62. */
  63.  
  64. echo "<b><i>".$this->header."</i></b>";
  65. echo "<P></P>";
  66.  
  67. //Check If our table has to be surrounded by an additional table
  68. //which increase style of this table
  69. if ($this->surrounded == true)
  70. echo "<table width='$this->width' border='1' cellspacing='0' cellpadding='0'><tr><td>";
  71.  
  72. echo "<table width='$this->modified_width' border='$this->border' cellspacing='$this->cellspace' cellpadding='$this->cellpad'>";
  73. echo "<tr bgcolor = '$this->header_color'>";
  74. // $out="";
  75. //Header Draw
  76. for ($i = 0; $i< $field_count; $i++)
  77. {
  78. //Now Draw Headers
  79. echo "<th align = '$this->header_alignment'><font color = '$this->header_textcolor'>&nbsp;".$this->fields[$i]."</font></th>";
  80. $out .="\" ";
  81. $out .= $this->fields[$i].'"'.',';
  82.  
  83. }
  84. $out .= "\n";
  85. echo "</tr>";
  86.  
  87. //Now fill the table with data
  88. while ($rows = mysql_fetch_row($this->mysql_resource))
  89. {
  90. echo "<tr align = '$this->body_alignment' bgcolor = '$this->body_color'>";
  91. for ($i = 0; $i < $field_count; $i++)
  92. {
  93. //Now Draw Data
  94. echo "<td><font color = '$this->body_textcolor'>&nbsp;".$rows[$i]."</font></td>";
  95. if($i==1)
  96. {
  97. if (preg_match('/href="([^"]*)"/i', $rows[$i] , $regs))
  98. { $result = $regs[1];}
  99. else { $result = "No URL Found";}
  100. $out .= "\"".$result.'"'.',';
  101. }
  102. else
  103. $out .= "\"".$rows[$i].'"'.',';
  104. //if ($i<>$field_count-1) $out .= ',';
  105.  
  106. }
  107.  
  108. $out .= "\n";
  109. echo "</tr>";
  110.  
  111. }
  112. echo "</table>";
  113.  
  114. if ($this->surrounded == true)
  115. echo "</td></tr></table>";
  116.  
  117. echo "csv:". $out;// this prints out correctly
  118. $f = fopen ('Report.csv','w');
  119. fputs($f, $out);
  120. fclose($f);
  121. echo "<br /><br />";
  122. echo"<form action=\"\" method=\"post\" name=\"textform\">";
  123. echo "<br /><br />";
  124. echo"<input name=\"download\" type=\"submit\" id=\"download\" value=\"Download\"><br /><br />";
  125.  
  126. echo"<a href='report.php' style=\"text-decoration:none\">"."<< Back"."</a\>";
  127. echo"</form>";
  128.  
  129. if ($_POST['download'])
  130. {
  131. header("location:download.php");
  132. }
  133.  
  134.  
  135. }
  136. }
  137.  
  138. ?>


And download.php:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. $output=fopen('Report.csv','r');
  3. header("Pragma: no-cache");
  4. header('Expires: 0');
  5. header('Cache-control: private');
  6. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  7. header('Content-Description: File Transfer');
  8. header('Content-Type: application/vnd.ms-excel');
  9. header('Content-disposition: attachment; filename="Report.csv"');
  10. // send output
  11. readfile('Report.csv');
  12.  
  13. //print_r($output);
  14. fclose($output);
  15. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 3
Light Poster
michelleradu is offline Offline
41 posts
since Dec 2009
Feb 7th, 2010
0
Re: Exporting dynamic HTML table to CSV document not working properly
I've also tried saving the table data without column headers and I've sent the $out variable at download.php (so I don't write $out to Report.csv on the server anymore) like this:
PHP Syntax (Toggle Plain Text)
  1. while ($rows = mysql_fetch_row($this->mysql_resource))
  2. {
  3. echo "<tr align = '$this->body_alignment' bgcolor = '$this->body_color'>";
  4. for ($i = 0; $i < $field_count; $i++)
  5. {
  6. echo "<td><font color = '$this->body_textcolor'>&nbsp;".$rows[$i]."</font></td>";
  7. $out .="\"";
  8. if($i==1)
  9. {
  10. if (preg_match('/href="([^"]*)"/i', $rows[$i] , $regs))
  11. { $result = $regs[1];}
  12. else
  13. { $result = "No URL Found";}
  14. $out .= $result."\"".',';
  15. }
  16. else
  17. $out .= $rows[$i]."\"".',';
  18. $out .= ',';
  19.  
  20. }
  21.  
  22. $out .= "\n";
  23. echo "</tr>";
  24. }
  25. echo "<br /><br />";
  26. echo"<form action=\"\" method=\"post\" name=\"textform\">";
  27. echo "<br /><br />";
  28. echo"<input name=\"download\" type=\"submit\" id=\"download\" value=\"Download\"><br /><br />";
  29. echo"</form>";
  30.  
  31. if ($_POST['download'])
  32. {
  33. header("location:download.php?string=$out");
  34. }
Thats what I have in download.php now:
PHP Syntax (Toggle Plain Text)
  1. $out=$_GET['string'];
  2. header('Content-Description: File Transfer');
  3. header('Content-Type: application/vnd.ms-excel');
  4. header('Content-disposition: attachment; filename="Report.csv"');
  5. echo $out;

So, now I get an "Unable to read file" error when I try downloading the info. I'm suspecting something is wrong with the content of $out. Any better ideas?
Last edited by michelleradu; Feb 7th, 2010 at 2:46 pm.
Reputation Points: 10
Solved Threads: 3
Light Poster
michelleradu is offline Offline
41 posts
since Dec 2009
Feb 7th, 2010
0
Re: Exporting dynamic HTML table to CSV document not working properly
My CSV file has got this type of contents:

"321","http://www.google.co.uk","link","2010-02-07 22:30:07","0","No","No","0","3","0","70.100.00.04",

I am now convinced this is where the problem is because Excel says it can not read the downloaded CSV file...
Reputation Points: 10
Solved Threads: 3
Light Poster
michelleradu is offline Offline
41 posts
since Dec 2009
Feb 8th, 2010
0
Re: Exporting dynamic HTML table to CSV document not working properly
My CSV file has got this type of contents:

"321","http://www.google.co.uk","link","2010-02-07 22:30:07","0","No","No","0","3","0","70.100.00.04",

I am now convinced this is where the problem is because Excel says it can not read the downloaded CSV file...

Problem solved... If anyone bumps into the same issue, look carefully when sending variables from one page to another. It took me a while till I realized I wasnt sending anything to the download.php page.
Reputation Points: 10
Solved Threads: 3
Light Poster
michelleradu is offline Offline
41 posts
since Dec 2009

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: I need some help with choosing correct CMS
Next Thread in PHP Forum Timeline: PHP auto suggest





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


Follow us on Twitter


© 2011 DaniWeb® LLC