954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How we can use download button on a php page

Hi everyone,

I am facing one problem. In one php page which is showing a table or record of an employee and i want to show that table in excel sheet with using download button which table is coming in php file.

So how i can do this using php code or html code . Please help me
Actually i want to show a empoyee record in excel page ( directly clicking on a button).

So how i can do this. If u have any example to do this. then please tell me . It is very urgent.

Thanks,
Gagan

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

There is a topic about it at http://www.daniweb.com/forums/showthread.php?t=91019&highlight=excel
It basically says to use the excel html formating like when you export html file from excel. Also it quotes the following code:

<?php


echo "<table cellspacing='4' cellpadding='4' border=1 align=center>";
echo"<tr>";
echo"<td>Day</td>";
echo"<td>Time</td>";
echo"<td>Unit</td>";
echo"<td>Lecturer</td>";
echo"</tr>";

echo"</table>";















header("Content-type: application/octet-stream");

# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");


?>


</p>
</body>
</html>

Hope that helps.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

There is a topic about it at http://www.daniweb.com/forums/thread91019.html&highlight=excel It basically says to use the excel html formating like when you export html file from excel. Also it quotes the following code:

Hope that helps.

hi sir,

I have used Sql query to fetching a record from a databse and this is showing this on a web page like this--

Date Day TimeIn TimeOut TotalTime Status
2009-02-23 Monday 10:11:18 19:38:22 9.27 Hours O.K.
2009-02-24 Tuesday 10:09:52 20:06:27 9.56 Hours O.K.
2009-02-25 Wednesday 10:04:13 18:52:28 8.49 Hours O.K.
2009-02-26 Thursday 09:56:55 19:52:56 9.56 Hours O.K.
2009-02-27 Friday 10:08:51 18:00:00 -.09 Hours O.K.
2009-02-28 Saturday 10:06:13 19:50:34 9.44 Hours O.K.
2009-03-01 Sunday Sunday Sunday Sunday Sunday
2009-03-02 Monday 10:09:09 15:31:12 5.31 Hours O.K.
below this i have used this link .
Downaload This page

but now i want this should be copy in excel file and what code i wil have to written in php so that these all result would come in Excel file . that is the thing i want to do . this i was asking. Please help me
Thanks
gagan

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Well I don't know you database structure (column names) but first step is to make the data output like the below. After you make a php file with the output like the below, then I shall show you how to add the headers to download as a excel file. The headers are also shown in the example above (previous post).

<table cellspacing='4' cellpadding='4' border=1 align=center>
<tr>
<td>Date</td>
<td>Day</td>
<td>TimeIn</td>
<td>TimeOut</td>
<td>TotalTime</td>
<td>Status</td>
</tr>
<tr>
<td>2009-02-23</td>
<td>Monday</td>
<td>10:11:18</td>
<td>19:38:22</td>
<td>9.27 Hours</td>
<td>O.K.</td>
</tr>
<tr>
<td>2009-02-24</td>
<td>Tuesday</td>
<td>10:09:52</td>
<td>20:06:27</td>
<td>9.56 Hours</td>
<td>O.K.</td>
</tr>
<tr>
<td>2009-02-25</td>
<td>Wednesday</td>
<td>10:04:13</td>
<td>18:52:28</td>
<td>8.49 Hours</td>
<td>O.K.</td>
</tr>
<tr>
<td>2009-02-26</td>
<td>Thursday</td>
<td>09:56:55</td>
<td>19:52:56</td>
<td>9.56 Hours</td>
<td>O.K.</td>
</tr>
<tr>
<td>2009-02-27</td>
<td>Friday</td>
<td>10:08:51</td>
<td>18:00:00</td>
<td>09 Hours</td>
<td>O.K.</td>
</tr>
<tr>
<td>2009-02-28</td>
<td>Saturday</td>
<td>10:06:13</td>
<td>19:50:34</td>
<td>9.44 Hours</td>
<td>O.K.</td>
</tr>
</table>

Just a note, I noticed the link I provided didn't work so the update is: http://www.daniweb.com/forums/thread91019.html

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 
//while clicking download button do this process
 $content - should have the content like
$content = "<table><tr><td>val1</td><td>val2</td></tr></table>";

header('Pragma: anytextexeptno-cache', true);
header("Content-type: application/vnd.ms-excel");
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename='myfile.xls');
print $content;
vinothkumarc
Light Poster
28 posts since Apr 2008
Reputation Points: 19
Solved Threads: 3
 

//while clicking download button do this process $content - should have the content like $content = "val1val2";

header('Pragma: anytextexeptno-cache', true); header("Content-type: application/vnd.ms-excel"); header('Content-Transfer-Encoding: Binary'); header('Content-disposition: attachment; filename='myfile.xls'); print $content;


You have that backtofront. Unusally it is more like the following as quoted from an earlier post:

<?
$content - should have the content like
$content = "<table><tr><td>val1</td><td>val2</td></tr></table>";
echo $content;
header('Pragma: anytextexeptno-cache', true);
header("Content-type: application/vnd.ms-excel");
header('Content-Transfer-Encoding: Binary');
header('Content-disposition: attachment; filename=myfile.xls');
?>
</p>
</body>
</html>
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

//while clicking download button do this process $content - should have the content like $content = "val1val2";

header('Pragma: anytextexeptno-cache', true); header("Content-type: application/vnd.ms-excel"); header('Content-Transfer-Encoding: Binary'); header('Content-disposition: attachment; filename='myfile.xls'); print $content;


Hi vinoth,

I want to discuss with you . because i am facing one problem which i have already discussed in this forum session_start warning . please help me . i have done every effort for solving that warning.
pls help me if u can solve this.
thanks ,
gagan

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Hi vinoth,

I want to discuss with you . because i am facing one problem which i have already discussed in this forum session_start warning . please help me . i have done every effort for solving that warning. pls help me if u can solve this. thanks , gagan


Did you check my post on http://www.daniweb.com/forums/thread177977.html
I have found numerous topics on both daniwebs and PHPfreaks in the past and two main reasons are the editor adds a hidden string at the beginning and the second being that there is something before <? tag. I believe some have even tried using <?php instead of <? or vice versa and is then successful.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 
Did you check my post on http://www.daniweb.com/forums/thread177977.html I have found numerous topics on both daniwebs and PHPfreaks in the past and two main reasons are the editor adds a hidden string at the beginning and the second being that there is something before <? tag. I believe some have even tried using <?php instead of <? or vice versa and is then successful.

thanks for reply,
I have also tried this as u told me. I have changed the <? tag to <?php also .Still the problem is same . pls help me . how i can solve this.

thanks,

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

thanks for reply, I have also tried this as u told me. I have changed the <? tag to <?php also .Still the problem is same . pls help me . how i can solve this.

thanks,


Try making a file using your normal editor and type the following code in it to see if it reports an error.

<?
session_start();
?>
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

hi gagan,
Please send me ur thread URL... (session_start)..

vinothkumarc
Light Poster
28 posts since Apr 2008
Reputation Points: 19
Solved Threads: 3
 
hi gagan, Please send me ur thread URL... (session_start)..


ya sure sir,pls see this warning which i am facing . i am sending the url of my website. pls see this page and then tell me the solution for this. http://www.warddamon.com/login/hoa.php?login=T

this is the url where this warning is coming. when i do session_start(); line in comments then this problem is not coming. but i need this line so that i can know about session.pls help me.
thanks,
gagan

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

hi gagan,
pls send ur (session_start) thread url...

vinothkumarc
Light Poster
28 posts since Apr 2008
Reputation Points: 19
Solved Threads: 3
 
hi gagan, pls send ur (session_start) thread url...


Did you mean this one: http://www.daniweb.com/forums/thread177977.html

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 
hi gagan, pls send ur (session_start) thread url...


about which URL u r saying about .........on which page i am facing this problem i have already send u that url. http://www.warddamon.com/login/hoa.php?login=T

this is that URL where warning is coming. Can we chat on gmail . if u will say that will be easy for us communication.
thanks,
gagan

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Also I still haven't heard of the results for when you try to open a page with only the following code:

<?
session_start();
?>


That will say if it is your editor.

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Also I still haven't heard of the results for when you try to open a page with only the following code:

<?
session_start();
?>
That will say if it is your editor.


yes, if i run only this code in my php file .
means i am saying if i will write code in a.php file only
<?php
session_start();
?>
then warning are coming. then what will be reason for that ??

thanks,
gagan

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

hi gagan,
I tried to create the same error.... in my local system...
by,
i removed write permission to my temp folder(c:\windows\temp).
and i ran my php program which has the session_start.. and etc., things..
i got the same error.......
So the conclusion is...

Please make sure ur tmp folder and its SUB folders has the write permission... for all users

vinothkumarc
Light Poster
28 posts since Apr 2008
Reputation Points: 19
Solved Threads: 3
 

hi gagan, I tried to create the same error.... in my local system... by, i removed write permission to my temp folder(c:\windows\temp). and i ran my php program which has the session_start.. and etc., things.. i got the same error....... So the conclusion is...

Please make sure ur tmp folder and its SUB folders has the write permission... for all users

thanks for reply,
i am so confused about this tmp folder .You are saying that tmp folder which are in my local system means on my wamp server or u r saying on that sever where this site is uploaded .

Pls tell me this on which sever i will have to check this tmp folder on my wamp sever or my client's sever.
thanks,
gagan

gagan22
Junior Poster
131 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

my window server...
default temp folder (c:/windows/temp)

wt abt u? r u using window/linux?
by seeing ur error.. i could able to understand . its linux.

So please make sure var/tmp folder and its sub folder has the write permission....

if still prob exist... we will try it in some other option

vinothkumarc
Light Poster
28 posts since Apr 2008
Reputation Points: 19
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You