Solved

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

Posted on 2014-12-29
1
155 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
1 Comment
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 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