Solved

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

Posted on 2014-12-29
1
153 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 108

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to count occurrences of each item in an array.
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now