×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Reading my lines of a csv file

Reading my lines of a csv file

Reading my lines of a csv file

(OP)
Hi
Hit a brick wall with this function, it does what I want but one line, I need it to read my lines of data, can anyone help please?

CODE

function mysql_insert_array($table, $column_list, $filename, $types) 
	{	
		$i=0;
		$f = fopen($filename, "r");
		while (($line = fgetcsv($f)) !== false) 
		{				
			$row = $line[0];    // We need to get the actual row (it is the first element in a 1-element array)
			$cells = explode(";",$row);	
			$type = explode(";",$types);
			foreach ($cells as $cell) 
			{				
				//case staement to find values types ie; int text etc
				$myresult = switch_to_get_values($type[$i],$cell);
				if($myresult)
					$fields[] = "'" . htmlspecialchars($cell) . "'";
				else
					$fields[] = $cell;
				$i++;
			}			
			$value_list = join(',', str_replace("' ","'",$fields));
			$query = "INSERT INTO " . $table . " (" . $column_list . ") VALUES (" . $value_list . ")";
		}		
		fclose($f);						
		
	return $query;
	} 

RE: Reading my lines of a csv file

(OP)
any help on this please to read the every row in a csv file?

RE: Reading my lines of a csv file

So what is not working exactly?

This line
while (($line = fgetcsv($f)) !== false)
gets one line of the csv file at a time as an array.

So this line:
$row = $line[0];
Is basically setting $row to the first value of your of the first line of your CSV file.

Which you then try to explode and since its not a semi-colon separated list of values, does nothing to it so $cells will just be equal to $row.

Not sure that is what you are trying to do.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Reading my lines of a csv file

(OP)
Ive done it, how do you mark these solved?

CODE

$fileData=fopen($filename,'r');
	while($row=fgets($fileData))
	{  
		$data_array = explode(",",$row);
		foreach($data_array as $columns=>$value)
		{
			echo "<br>" .$value;
		}
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close