WeLoveCSS Logo
Home Profile Members Search Rules Help New Posts



WeLoveCSS > PROGRAMMING LANGUAGES > Scripting and Server Side > Export html table to csv

Reply
  Thread Tools Display Modes
Old 29th August 12, 03:48 PM   #1
vinex08
WLC Lover
 
Join Date: Oct 2010
Posts: 94
Default Export html table to csv

Hi,
Just want to ask on how can i export html table into a CSV file.

I have tried this code but the html code is printed too.

Code:
<?php

    header("Content-type: application/vnd.ms-excel; name='excel'");
    header("Content-Disposition: filename=export.csv");
?>
what i want is similar to this,
http://www.kunalbabre.com/projects/table2CSV.php

but prompts to save the file.
vinex08 is offline   Reply With Quote
Old 29th August 12, 08:53 PM   #2
meesa
WLC Mod
 
meesa's Avatar
 
Join Date: Jul 2009
Location: Milky Way Galaxy
Posts: 3,134
Default Re: Export html table to csv

Just make sure the PHP is only passed the tabular data, and not the rest of the HTML
__________________
Praise be to the Lord God for the ability to learn, the capability to analyze, and the time to help users on this forum.
meesa is offline   Reply With Quote
Old 30th August 12, 10:31 AM   #3
vinex08
WLC Lover
 
Join Date: Oct 2010
Posts: 94
Default Re: Export html table to csv

I don't get it, here is my code:

Code:
<?php  
$connect = mysql_connect("localhost","root",""); 
mysql_select_db("phptest",$connect); 
$result = mysql_query("SELECT * FROM sales");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Check Page</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript" src="jquery.tablesorter.js"></script>
<script>
 $(document).ready(function() 
    { 
        $("#myTable").tablesorter(); 
    } 
); 
    
</script>
<style>
#container {
	width:1276px;
	margin: 0 auto;
}

#myTable td {
	padding:0 10px;
	border: 1px solid #09F;
}

#myTable th {
	font-size:16px;
	padding: 5px 0;
}
</style>
</head>

<body>
<div id="container">	
<table id="myTable" cellpadding="0" class="tablesorter"> 
<thead bgcolor="#CCCCCC"> 
<tr>
<th>Contact</th>
<th>City</th>
<th>Company</th>
<th>State</th>
<th>Zip</th>
<th>TimeStamp</th>
<th>Status</th>
<th>Close Date</th>
<th>Units</th>
<th>Price</th>
<th>Group</th>

</tr>
</thead> 
<tbody> 
<?php
while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['contact'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['company'] . "</td>";
  echo "<td>" . $row['state'] . "</td>";
  echo "<td>" . $row['zip'] . "</td>";
  echo "<td>" . $row['timestamp'] . "</td>";
  echo "<td>" . $row['status'] . "</td>";
  echo "<td>" . $row['closedate'] . "</td>";
  echo "<td>" . $row['units'] . "</td>";
  echo "<td>" . $row['price'] . "</td>";
  echo "<td>" . $row['grouping'] . "</td>";
  echo "</tr>";
  }
  ?>
</tbody> 
</table>
</div>
<?php

    header("Content-type: application/vnd.ms-excel; name='excel'");
    header("Content-Disposition: filename=export.csv");
	?>
</body>
</html>
vinex08 is offline   Reply With Quote
Old 31st August 12, 06:00 AM   #4
meesa
WLC Mod
 
meesa's Avatar
 
Join Date: Jul 2009
Location: Milky Way Galaxy
Posts: 3,134
Default Re: Export html table to csv

Well, I don't know how the jQuery works, but I do know that the header has to be the first part of the PHP. You should be getting an error on that page.
__________________
Praise be to the Lord God for the ability to learn, the capability to analyze, and the time to help users on this forum.
meesa is offline   Reply With Quote
Reply


Thread Tools
Display Modes
Linear Mode Linear Mode

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 01:38 AM.



Home | Advertise | Contact Us | Top
Home | Advertise | Contact Us | Top

Copyright© 2006 WeLoveCSS.com. All Rights Reserved.
Powered by vBulletin Version 3.8.4 Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.