Skip to content

Commit 2db44b4

Browse files
committed
Create table.php
1 parent b60a876 commit 2db44b4

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

table.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<!DOCTYPE html><head>
2+
<meta charset="utf-8">
3+
<title>Orders Table</title>
4+
</head>
5+
<?php
6+
$file_handle = fopen("EtsySoldOrders2015-12.csv", "r");
7+
?>
8+
<table align="center" border="1" cellpadding="0" cellspacing="0" width="90%">
9+
<tr class="dataTableRow">
10+
<th class="main" width="5%">Price</th>
11+
<th class="main" width="10%">Ordered By</th>
12+
<th class="main" width="10%">Email</th>
13+
<th class="main" width="5%">Ordered On</th>
14+
<th class="main" width="5%">Completed On</th>
15+
</tr>
16+
<?php
17+
//Now that we've created such a nice heading for our html table, lets create a heading for our csv table
18+
$csv_hdr = "Order ID, Ordered By, Email, Order Date, Completed Date";
19+
//Quickly create a variable for our output that'll go into the CSV file (we'll make it blank to start).
20+
$csv_output="";
21+
22+
// Ok, we're done with the table heading, lets connect to the database
23+
24+
25+
while (!feof($file_handle) ) {
26+
$row_line= fgetcsv($file_handle, 1024);
27+
?>
28+
<tr>
29+
<td align="left" valign="center">
30+
<br><?php
31+
if(intval ($row_line[0]) ){
32+
echo ($row_line[0] * 45);
33+
$csv_output .= ($row_line[0] * 45) . "&#8369; , ";
34+
35+
}
36+
else {
37+
echo $row_line[0];
38+
$csv_output .= $row_line[0]." , ";
39+
}
40+
//here we are displaying the contents of the field or column in our rows array for a particular row.
41+
//while we're at it we might as well store the data in comma separated values (csv) format in the csv_output variable for later use.
42+
43+
?>
44+
</td>
45+
<td align="left" valign="center">
46+
<br><?php echo $row_line[4]; //repeat for all remaining fields or columns we have headings for...
47+
$csv_output .= $row_line[4] . ", ";?>
48+
</td>
49+
<td align="left" valign="center">
50+
<br><?php echo $row_line[5]; //repeat for all remaining fields or columns we have headings for...
51+
$csv_output .= $row_line[5] . ", ";?>
52+
</td>
53+
<td align="left" valign="center">
54+
<br><?php echo $row_line[3]; //repeat for all remaining fields or columns we have headings for...
55+
$csv_output .= $row_line[3] . ", ";?>
56+
</td>
57+
<td align="left" valign="center">
58+
<br><?php echo $row_line[4]; //repeat for all remaining fields or columns we have headings for...
59+
$csv_output .= $row_line[4] . ", \n"; //ensure the last column entry starts a new line - eto yung magbreabreak ng per td element?>
60+
</td>
61+
</tr>
62+
<?php
63+
}
64+
//closing while loop
65+
//closing if stmnt
66+
?>
67+
<!--closing the table-->
68+
</table>
69+
<?php
70+
/*
71+
Here is the important part. we've got the 2 variables (csv_hdr & csv_output) to create our csv file, but we can't do it in this file.
72+
Why? Because the header for this file has already been sent and will show up in our csv file if we generate it on this page. We don't
73+
want any html header in our csv file, so we've got to post our 2 variables to another php page (export.php) on which we generate our csv
74+
file.
75+
76+
Here's the code for a form & button that'll post our 2 variables as hidden _POST to export.php.
77+
*/
78+
?>
79+
<br />
80+
<center>
81+
<form name="export" action="export.php" method="post">
82+
<input type="submit" value="Export table to CSV">
83+
<input type="hidden" value="<?php echo $csv_hdr; ?>" name="csv_hdr">
84+
<input type="hidden" value="<?php echo $csv_output; ?>" name="csv_output">
85+
</form>
86+
</center>
87+
</html>

0 commit comments

Comments
 (0)