Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 15th, 2012  |  syntax: PHP  |  size: 2.28 KB  |  views: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. $con = mysql_connect("localhost", "root", "");
  4.  
  5. if(!$con)
  6. {
  7.     die('Could not connect' . mysql_error());
  8. }
  9.  
  10. mysql_select_db("db_lemonstand", $con);
  11.  
  12. class csvIterator extends LimitIterator
  13. {
  14.     public function __construct($path)
  15.     {
  16.         $csv = new SplFileObject($path);
  17.         $csv->setFlags(SplFileObject::READ_CSV);
  18.         parent::__construct($csv, 1);
  19.     }
  20. }
  21. foreach (new csvIterator('data/catalogue.csv') as $entry) {
  22.  
  23.     $name = $entry[23];
  24.  
  25.     /* Replace Strings To Make URL Name */
  26.     $search_array = array(" ", "/", "+");
  27.     $replace_array = array("-");
  28.     $url_name = strtolower($name);
  29.     $url_name = str_replace($search_array, $replace_array, $url_name);
  30.  
  31.     $long_desc = $entry[9];
  32.     $short_desc = $entry[23];
  33.     $manufacturer = $entry[11];
  34.     $price = $entry[15];
  35.     $sku = $entry[2];
  36.     $weight = $entry[29];
  37.     $width = $entry[30];
  38.     $height = $entry[5];
  39.     $enabled = '1';
  40.     $created_at = date('Y-m-d H:i:s');
  41.     $product_type_id = '1';
  42.     $tax_class_id = '1';
  43.  
  44.     echo '<pre>';
  45.     print_r($entry);
  46.     echo '</pre>';
  47.  
  48.     // Check see if products' SLU already exists or not
  49.     $product_exists = "SELECT sku FROM shop_products WHERE sku = '$sku'";
  50.     $result = mysql_query($product_exists, $con);
  51.  
  52.     $exists = mysql_num_rows($result);
  53.  
  54.     if($exists == 0 )
  55.     {
  56.         $insert = "INSERT INTO shop_products (name, description, short_description, url_name, price, sku, weight, width, height, enabled, created_at, tax_class_id, product_type_id) VALUES ('$name', '$long_desc', '$short_desc', '$url_name', '$price', '$sku', '$weight', '$width', '$height', '$enabled', '$created_at', '$tax_class_id', '$product_type_id')";
  57.         $insert_data = mysql_query($insert, $con);
  58.     }
  59.     else
  60.     {
  61.         $update = "UPDATE shop_products SET name = '$name', description = '$long_desc', short_description = '$short_desc', url_name = '$url_name', price = '$price', sku = '$sku', weight = '$weight', height = '$height', enabled = '$enabled', created_at = '$created_at', tax_class_id = '$tax_class_id', product_type_id = '$product_type_id'";
  62.         $update_data = mysql_query($update, $con);
  63.  
  64.         if (!mysql_query($update,$con))
  65.         {
  66.             die('Error: ' . mysql_error());
  67.         }
  68.     }
  69.  
  70. }
  71. ?>
clone this paste RAW Paste Data