millsy007 Posted December 2, 2008 Share Posted December 2, 2008 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: <?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>"; ?> Link to comment https://forums.phpfreaks.com/topic/135152-csv-to-html-choosing-fields/ Share on other sites More sharing options...
rhodesa Posted December 2, 2008 Share Posted December 2, 2008 try this: <?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. $limit_columns = array(5,6); /* 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 $n=>$value) { if(!count($limit_columns) || in_array($n+1,$limit_columns)){ 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>"; ?> the variable $limit_columns can be adjusted accordingly. Link to comment https://forums.phpfreaks.com/topic/135152-csv-to-html-choosing-fields/#findComment-703946 Share on other sites More sharing options...
millsy007 Posted December 2, 2008 Author Share Posted December 2, 2008 Cheers Link to comment https://forums.phpfreaks.com/topic/135152-csv-to-html-choosing-fields/#findComment-704295 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.