adam291086 Posted November 29, 2007 Share Posted November 29, 2007 Hello I have a script that calls on a CSV and displays every row. At the moment it seperates each field with a comma. I am struggling with how to seperate each field and put it into a table column. Can someone push me in the right direction. Heres what i have so far <?PHP $file_handle = fopen("adam.csv", "rb"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $parts = explode('<br />', $line_of_text); print $parts[0].$parts[1]. $parts[2]. "<BR>"; } fclose($file_handle); ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 29, 2007 Author Share Posted November 29, 2007 i now have it printing out each row in a table. But i need to split the rows into seperate columns <?php $filename = "adam.csv"; $id = fopen($filename, "r"); while ($data = fgetcsv($id, filesize($filename),";")) $table[] = $data; fclose($id); echo "<table width=\"100%\" border=\"1\">\n"; foreach($table as $row) { echo "<tr>"; foreach($row as $data) echo "<td>$data</td>"; echo "</tr>\n"; } echo "</table>\n"; ?> You can see whats happening here http://www.adamplowman.co.uk/cycling/test/adam.php Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 29, 2007 Share Posted November 29, 2007 Just a quick one, worth a try <?php $filename = "adam.csv"; $id = fopen($filename, "r"); while ($data = fgetcsv($id, filesize($filename),";")) $table[] = $data; fclose($id); echo "<table width=\"100%\" border=\"1\">\n"; foreach($table as $row) { echo "<tr>"; foreach($row as $data) { $data = split (",", $data); foreach ($data as $newdata) { echo "<td>$newdata</td>"; } } echo "</tr>\n"; } echo "</table>\n"; ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 29, 2007 Author Share Posted November 29, 2007 Works wounderfully. Thanks Quote Link to comment Share on other sites More sharing options...
aschk Posted November 29, 2007 Share Posted November 29, 2007 fgetcsv returns an array for each row (if used in a while loop) thus in order to preserve some sense : $filename = "adam.csv"; $file = fopen($filename,"r"); $delimiter = ";"; $enclosed = ""; $filesize = filesize($filename); while(!feof($file)){ // An array of each line. Fields are DELIMITED by ; $row = fgetcsv($file,$filesize,$delimiter,$enclosed); $table[] = $row; } fclose($file); ...etc I think what you have failed to realise is how fgetcsv works. The first two parameters you understand. But I think you have the third parameter wrong. The FIELD delimiter (NOT LINE). Most people use "," to delimit fields so I would check that this is correct before you do anything more. Also consider that you may have an enclosure. Thus my question (which will give you your answer): What does your CSV file look like? Quote Link to comment Share on other sites More sharing options...
adam291086 Posted November 29, 2007 Author Share Posted November 29, 2007 i am confused. I followed a tutorial for this. I know that a csv file works by seperating a column using a "," and a row by a new line. ??? ???. What i have works. Can you explain some more? Quote Link to comment Share on other sites More sharing options...
aschk Posted November 29, 2007 Share Posted November 29, 2007 Because I couldn't verify your CSV file (i.e. you never gave a sample of what it looked like) I assumed you made the common mistake of thinking that the delimiter parameter of the fgetcsv function was for each line (instead of each field), as it had occured to me you were using a rather obscure one (. However what you have appears to work fine now , and you obviously have used ; to delimit each field. Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted November 29, 2007 Share Posted November 29, 2007 You can download from the link he gave and BTW aschk what Adam did isn't that a good way just asking cause , i am also doing the same stuff... maybe you can explain bit , it will be helpful Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.