[php]transferring data from text file to Mysql Table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cooweck
Forum Newbie
Posts: 4
Joined: Thu Jul 09, 2009 2:42 am

[php]transferring data from text file to Mysql Table

Post by cooweck »

I've a example text file below. I need to pass this data to mysql.
and can anyone help with this.
I figure out the way of connecting database,
and the thing I am suffering with is transferring below data to mysql
ignore dots

Phone......MCO Accessory.........VPS Bluetooth.........VPC Bluetooth (FACTORS)
White..... .T950....................off.....................off (CHOICES)
White......C300....................on.....................off (CHOICES)
White......T950....................on.....................off (CHOICES)
White......T950....................off.....................on (CHOICES)
White......C300....................off.....................off (CHOICES)
White..... .C300....................on.....................on (CHOICES)
White..... .C300....................off.....................on (CHOICES)
White..... .T950....................on......................on (CHOICES)
Last edited by cooweck on Thu Jul 09, 2009 3:13 am, edited 1 time in total.
SvanteH
Forum Commoner
Posts: 50
Joined: Wed Jul 08, 2009 12:25 am

Re: transferring data from text file to Mysql Table

Post by SvanteH »

Code: Select all

<?php
 
  $data = "Phone MCO Accessory VPS Bluetooth VPC Bluetooth (FACTORS)
White..... T950....................off.....................off (CHOICES)
White..... C300....................on.....................off (CHOICES)
White..... T950....................on.....................off (CHOICES)
White..... T950....................off.....................on (CHOICES)
White..... C300....................off.....................off (CHOICES)
White..... C300....................on.....................on (CHOICES)
White..... C300....................off.....................on (CHOICES)
White..... T950....................on......................on (CHOICES)
";
  
  $arrData = explode("\r\n", $data);
  
  foreach($arrData as $row)
  {
    $arrRowData = explode(".", $row);
    
    echo $arrRowData[0]; //Color
    echo $arrRowData[5]; //Name
    echo $arrRowData[25]; //on/off
    echo $arrRowData[46]; //on/off (choices)
    echo "<br>";
  }
?>
cooweck
Forum Newbie
Posts: 4
Joined: Thu Jul 09, 2009 2:42 am

Re: transferring data from text file to Mysql Table

Post by cooweck »

thanks for reply.
actually factors are arbitrary so do choices,
so I don't know how many factors and choices there are.
there are plenty of data I need to handle with
and every data has different number of factor, and choice and also names
this data is just an example.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: [php]transferring data from text file to Mysql Table

Post by onion2k »

The first thing to do is design a database structure that will work with all the options. By the sounds of it, if there are lots of different options, you'll be best off with 2 tables. One will hold the basic product data with the fields that are common to every product (name, sku, color, etc), and a second that holds key/value data for each product. Eg

Code: Select all

Products Table
id | product  | sku | color | price
1  | Product1| 123 | White | 200.00
2  | Product2| 124 | White | 250.00
3  | Product3| 125 | White | 300.00
 
Product Data Table
id | key           | value
1  | MCO Accessory | T950
1  | VPS Bluetooth | Off
1  | VPC Bluetooth | Off
2  | MCO Accessory | T950
2  | VPS Bluetooth | On
3  | MCO Accessory | T950
3  | VPS Bluetooth | On
3  | VPC Bluetooth | Off
 
Once that's in place you'll need to write a script that takes each line from the data file, explodes it into individual fields, and then inserts the product into the products table, and rows for each data field into the data table.
Post Reply