CSV to HTML - Choosing Fields Help
Posted: Tue Dec 02, 2008 8:12 am
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I have some php code that takes a csv file and outputs it to an html table.
The problem is that I only want to show certain fields / columns from the csv file in my html table.
In this case they are the 5th and 6th columns (named ResponseText and DateAdded)
Where in the code could I make this selection?
PHP Code:
Thanks in advance for any advice 
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
I have some php code that takes a csv file and outputs it to an html table.
The problem is that I only want to show certain fields / columns from the csv file in my html table.
In this case they are the 5th and 6th columns (named ResponseText and DateAdded)
Where in the code could I make this selection?
PHP Code:
Code: Select all
<?php
/*/-------------------------------------------------------------------\
| Original Name : csvview.php |
| Origional Author : Neil Maskell |
| Revised by: Charles W. Reace, Jr. |
| |
| Function : Reads a specified CSV file (Comma seperated) and |
| converts it into a readable HTML table. |
| |
| You could set up a html form with an input field |
| called filename. Then use csvview.php as the action. |
| |
| the reason cache is being disabled is because if you |
| update the csv file the cache doesnt realise and shows|
| an older version of the information. |
| |
| The CSV files should (and normally are) in the format:|
| field1,field2,field3,field4 |
| |
| thanks to Charles W. Reace, Jr. |
| now this works with csv files in the format: |
| "field1","field2","field3","field4" |
| |
\-------------------------------------------------------------------/
*/
//
$filename = "ResponsesText.csv"; // File to open. quote out this variable if you are using a form to link to this script.
/*
No cache!!
*/
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// always modified
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
/*
End of No cache
*/
# bool viewlog(str filename)
# parses input CSV file into table rows
# returns FALSE if cannot open file, otherwise TRUE
function viewlog($filename)
{
$fp = fopen($filename,"r");
if($fp === false)
{
return(false);
}
while(($line = fgetcsv($fp, 1000)) !== false)
{
echo "<tr>";
foreach($line as $value)
{
echo "<td>$value</td>";
}
echo "</tr>\n";
}
return(true);
}
echo "<html><head><base href=\"./\"><title>CSV File Viewer</title></head><body bgcolor=silver>";
// Start the table definition of your choice
echo "<table>\n";
viewlog($filename);
echo "</table></body></html>"; ?>
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: