Jump to content

[SOLVED] excel


Recommended Posts

Well basically I REALLY dont want to have to type out 350 products into my database and my client only supplied an excel sheet, which I exported as a CSV and text file neither worked for the upload.

 

I've attached the excel sheet to this post and it has the excel file, a CSV and a text file. I really just need it so that I can export it or convert it or anything to save me from typing everything out or if anyone has a PHP file that I can run and load my excel sheet into to convert it I could make one but again sort of defies the object of the game here haha running tight deadlines now.

 

I need this excel sheet and all of its contents in a MYSQL insert list so I can just run it and have done with it lol

 

[attachment deleted by admin]

Link to comment
Share on other sites

Ok - being old, lazy and a 'sledge hammer approach' guy, here is my suggestion and comments.

 

I converted the xls file into a comma delimited CSV file - file name noir000.dat (note when I did this some of the elements had many extra empty 'fields' so I went back and deleted all those columns after the 'STOCK' column to clean things up a bit).

 

Here is a rough idea how to then move the data to your mysql database - obviously I left out the database connection and actual query portions.


<?PHP

##########################################
# read the data file into an array
# each element of this array contains all of the information for each item

$data_file = "noir000.dat";

$line_array = file($data_file);

########################################
# make your database connection here


####################################################
# loop through the first array

$number_of_items = count($line_array);

$i =0;
for($i=0;$i<$number_of_items;$i++) {

########################################
# explode each element into a new array using the comma as the delimiter

$temp_array = explode(",",$line_array[$i]);

###########################################
# make your mysql query here and
# insert each field from the temp array into the proper mysql fields

}

?>

 

Hope this helps

 

Link to comment
Share on other sites

here is how the query shold look. NOTE - your excel file lists the columns as follows:

 

Department - Sub Cat - Item - Desc - Price - Stock

(there are six cols in the excel file and only five in your insert statement)


mysql_query("INSERT INTO prod_inf (itemname, DESCRIPTION, CATAGORY, sub-catagory, PRICE)  VALUES($temp_array[2]','$temp_array[3]',$temp_array[0]',$temp_array[1]',$temp_array[4]',  ) ") or die(mysql_error()); 

 

I presumed

itemname = item

description = description

sub cat = sub cat

cat = dept

price = price

 

Hope this helps

 

Lite...

 

 

Link to comment
Share on other sites

as a side note - when saving an excel file as a CSV comma delimited, it will automatically add opening and closing quotation marks to any field that contains commas; thereby 'fixing the apparent' problem with 'extra' commas.

Link to comment
Share on other sites

Column count doesn't match value count at row 1

 

I got that, and I'm extremely tired so this could be blind idiocy but this is the code after I altered it a little bit from other errors in the query.

 

The code is now

 

<?
##########################################
# read the data file into an array
# each element of this array contains all of the information for each item

$data_file = "csv.dat";

$line_array = file($data_file);

########################################
# make your database connection here
mysql_connect("localhost", "noirorchidemporium_co_uk", "animal")or die("didnt connect1");
mysql_select_db("noirorchidemporium_co_uk_1")or die("didnt connect2");

####################################################
# loop through the first array

$number_of_items = count($line_array);

$i =0;
for($i=0;$i<$number_of_items;$i++) {

########################################
# explode each element into a new array using the comma as the delimiter

$temp_array = explode(",",$line_array[$i]);

###########################################
# make your mysql query here and
# insert each field from the temp array into the proper mysql fields

mysql_query("INSERT INTO prod_inf (ID, itemname, DESCRIPTION, CATAGORY, subcatagory, PRICE, PICTURE_1, PICTURE_2, POSTAGE, STOCK)  VALUES('$temp_array[2],$temp_array[3],$temp_array[0],$temp_array[1],$temp_array[4],,,,,,'  ) ") or die(mysql_error()); 
}
echo $temp_array;
echo $number_of_items;
echo "inserted/inserting";

?>

 

And the DB is

 

CREATE TABLE prod_inf (
  ID int(11) NOT NULL auto_increment,
  itemname text NOT NULL,
  DESCRIPTION text NOT NULL,
  CATAGORY text NOT NULL,
  subcatagory text,
  PICTURE_1 text NOT NULL,
  PICTURE_2 text NOT NULL,
  PRICE text,
  POSTAGE text,
  STOCK text NOT NULL,
  PRIMARY KEY  (ID)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;

 

Cheers guys

Link to comment
Share on other sites

Ok -

 

When you are inserting a new record into a database

1. You do NOT need to list any fields for which you do not have values.

2. If a field (ID in this case) is AUTO-INCREMEN, you do not have to list it

3. Each value needs to have the single quotes around it, not just at the begining and end of the list.

 

so change this

mysql_query("INSERT INTO prod_inf (ID, itemname, DESCRIPTION, CATAGORY, subcatagory, PRICE, PICTURE_1, PICTURE_2, POSTAGE, STOCK)  VALUES('$temp_array[2],$temp_array[3],$temp_array[0],$temp_array[1],$temp_array[4],,,,,,'  ) ") or die(mysql_error()); 

 

to this

mysql_query("INSERT INTO prod_inf (itemname, DESCRIPTION, CATAGORY, subcatagory, PRICE)  VALUES('$temp_array[2]','$temp_array[3]','$temp_array[0]','$temp_array[1]','$temp_array[4]')") or die(mysql_error()); 

 

that should do it.

 

Lite...

 

 

Link to comment
Share on other sites

Ok so the finishing code was

And it worked a charm thankyou so much guys

 

<?
##########################################
# read the data file into an array
# each element of this array contains all of the information for each item

$data_file = "csv.dat";

$line_array = file($data_file);

########################################
# make your database connection here
mysql_connect("localhost", "noirorchidemporium_co_uk", "animal")or die("didnt connect1");
mysql_select_db("noirorchidemporium_co_uk_1")or die("didnt connect2");

####################################################
# loop through the first array

$number_of_items = count($line_array);

$i =0;
for($i=0;$i<$number_of_items;$i++) {

########################################
# explode each element into a new array using the comma as the delimiter

$temp_array = explode(",",$line_array[$i]);
$temp_array2 = preg_replace("",",'&+-",$line_array[$i]);

###########################################
# make your mysql query here and
# insert each field from the temp array into the proper mysql fields

$sql = mysql_query("INSERT INTO prod_inf (itemname, DESCRIPTION, CATAGORY, subcatagory, PRICE)  VALUES('$temp_array[2]','$temp_array[3]','$temp_array[0]','$temp_array[1]','$temp_array[4]' ) ") or die(mysql_error()); 
}
echo $temp_array;
echo $number_of_items;
echo "inserted/inserting";

?>

Link to comment
Share on other sites

  • 1 month later...

I tested the code in the previous post and it works fine. However, I would like to know if there is a way to first check if the record already exist in DB before loading data from .DAT file?

 

Basically, I would like to only import new data.

 

 

Link to comment
Share on other sites

All I can suggest to save you a crap load of time is change your query to only select distinct records on the page querying from the database. Because right now I'm being way to lazy to edit that script  :D

 

for instance.

 

mysql_query("SELECT DISTINCT column FROM table")or die(mysql_error());

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.