Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Transitioning to PDO/MySQL.  Need help  with CSV import - Pipe delimited

Posted on 2014-12-29
1
Medium Priority
?
166 Views
Last Modified: 2014-12-29
How do I set the delimiter to pipe and use quotes?   I have a field with several commas, so I am changing to pipe delimited.  Each field has quotes as well.

I have tried several different ways to fix.  fgetcsv($handle,""","|",""");




$filename="EP_download.csv";




//open the file
$handle = fopen($filename, "r");
//this is what causes it to skip
fgetcsv($handle,""","|",""");    //How do I set the delimiter to pipe with quotes?



//begin looping through the lines
while (($data = fgetcsv($handle, ""","|",""")) !== FALSE)
{


// loop all column values and escape special characters
 foreach ($data as $key => $value){
  $data[$key] = $value;
}


$statement = $conn->prepare('INSERT IGNORE INTO EPdownload (ID, Title, Category, SubCategory, SubSubCategory, Brand, BrandURL, ProductImage, MSRP, MAPPrice, Description, Specification, DealerCost, PageURL, PageTitle, StartingPageURL, DateTime) VALUES(:ID, :Title, :Category, :SubCategory, :SubSubCategory, :Brand, :BrandURL, :ProductImage, :MSRP, :MAPPrice, :Description, :Specification, :DealerCost, :PageURL, :PageTitle, :StartingPageURL, :DateTime)');

		$params = array(':ID'=>$data[0],
						':Title'=>$data[1],
						':Category'=>$data[2],
						':SubCategory'=>$data[3],
						':SubSubCategory'=>$data[4],
						':Brand'=>$data[5],
						':BrandURL'=>$data[6],
						':ProductImage'=>$data[7],
						':MSRP'=>$data[8],
						':MAPPrice'=>$data[9],
						':Description'=>$data[10],
						':Specification'=>$data[11],
						':DealerCost'=>$data[12],
						':PageURL'=>$data[13],
						':PageTitle'=>$data[14],
						':StartingPageURL'=>$data[15],
						':DateTime'=>$data[16]);
		                $statement->execute($params); 
	
}

Open in new window

0
Comment
Question by:lawrence_dev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 40523035
Please see http://php.net/manual/en/function.fgetcsv.php

Arguments are resource $handle [, int $length = 0 [, string $delimiter = "," [,...

So the first-cut of code would look something like this:
$handle = fopen($filename, "r");
if (!$handle) trigger_error("FAILED TO OPEN $filename", E_USER_ERROR);
$arr = fgetcsv($handle, 0, '|');
var_dump($arr);

Open in new window

If that works correctly, then we can add the appropriate loop control to detect EOF on the $handle resource.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question