CSV to HTML - Choosing Fields Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

CSV to HTML - Choosing Fields Help

Post by millsy007 »

~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: :arrow: 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>"; ?>
 
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: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: CSV to HTML - Choosing Fields Help

Post by novice4eva »

Code: Select all

 
 echo "<tr>";
$neededCols= array(5,6);//ANY COL U WANT TO DISPLAY
 foreach($line as $key=>$value)
 {
if(in_array($key,$neededCols))
 echo "<td>$value</td>";
 }
 echo "</tr>\n";
 }
 
millsy007
Forum Commoner
Posts: 78
Joined: Wed Jul 02, 2008 7:00 pm

Re: CSV to HTML - Choosing Fields Help

Post by millsy007 »

Works Exactly as I wanted :D Cheers
Post Reply