Solved

How do I adjust the output on this XLS to CSV script

Posted on 2011-09-11
5
237 Views
Last Modified: 2012-08-13
Experts:

This script works perfectly, however, all of the results of a row are inserted into the first field on that row (part_num).   How do I eliminate the " " in each result, and how do I populate the results correctly in the respective cells?

The results are all populated in the first field of each row (part_num):  

00 83104|"21in. Cross Bar"|""|"42.00"|""|"32.15"            
0002V413|"Canon Case for XL1S"|"10.00"|"325.00"|"285.00"|"229.76"            
000-6775|"Amphenol 000-6775 BNC Male Cable End for RG8/U"|"0.00"|"27.50"|""|"20.31"            
001-008|"Calzone 001-008 Escort AT Series 42inch Plasma Case"|""|"899.95"|""|"560.30"            
0015276|"Mackie 0015276 Replacement Subwoofer Driver for SW1501"|"1.00"|"172.62"|"139.80"|"107.89"            
003-000884-01|"Replacement Bulb for Christie Digital DS65 - DS650 - DS655"|"2.00"|"495.00"|""|"419.88"            
003-120242-01|"Replacement Lamp For Christie LX380"|""|"475.00"|""|"397.95"      


Here are the database fields:

$import="INSERT into `1product_excel_download` (part_num,description,weight,List,MAP,Cost) values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]')";



<?php
require_once 'reader.php';
$excel = new Spreadsheet_Excel_Reader();
$excel->setOutputEncoding('CP1251');
$excel->read('pricelist.xls');
$x=8;
$sep = "|";
ob_start();
while($x<=$excel->sheets[0]['numRows']) {
$y=1;
$row="";
while($y<=$excel->sheets[0]['numCols']) {
$cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : '';
$row.=($row=="")?"\"".$cell."\"":"".$sep."\"".$cell."\"";
$y++;
}
echo $row."\n"; 
$x++;
}
$fp = fopen("products.csv",'w');
fwrite($fp,ob_get_contents());
fclose($fp);
ob_end_clean();
?>

Open in new window

     






mysql_query("TRUNCATE TABLE `1product_excel_download`");

//
// Include the mysql connect page
//include("connect.php");
// Set the filename that you want to import
$filename="products.csv";
//open the file
$handle = fopen($filename, "r");
//this is what causes it to skip
fgetcsv($handle,"|");


//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] = mysql_real_escape_string($value);
}
$import="INSERT into `1product_excel_download` (part_num,description,weight,List,MAP,Cost) values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]')"; 


//execute the mysql query
mysql_query($import) or die(mysql_error());
}
//close the file
fclose($handle);
//output a message saying its done.

$msgprod="Product Import done";
echo $msgprod;
echo "<BR>";

Open in new window

0
Comment
Question by:rlb1
  • 4
5 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 36520285
How are these rows of pipe-delimited text strings created?
0
 

Author Comment

by:rlb1
ID: 36520296
Please clarify your question...  do not understand exactly what you mean.

I am downloading a feed from a distributor, then converting the xls feed to csv, then uploading to the server.  I am creating the feeds with script 2.  

0
 

Author Comment

by:rlb1
ID: 36520298
uploading to the mysql database not server
0
 

Author Comment

by:rlb1
ID: 36520485
disregard.  changed delimiter to comma and it worked fine.  
0
 

Author Closing Comment

by:rlb1
ID: 36520486
I was running pipe delimited because of quotes and commas within the data.  Data is not perfect, but is usable using comma as delimiter.

Thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.

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

18 Experts available now in Live!

Get 1:1 Help Now