?
Solved

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

Posted on 2011-09-11
5
Medium Priority
?
244 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
[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
  • 4
5 Comments
 
LVL 111

Accepted Solution

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

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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

770 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