Jump to content

[SOLVED] CSV help


adam291086

Recommended Posts

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);

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Just a quick one, worth a try  ;D

<?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";
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.