Guest User

Untitled

a guest
May 24th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1. <?php
  2.  
  3. class CSVImport {
  4.  
  5.     public $csvData;
  6.     private $preparedData;
  7.  
  8.     function __construct($csv) {
  9.  
  10.         $delimiter = ' ';
  11.         $quote = '""';
  12.         $newline = "\t";
  13.  
  14.         $this -> csvData = $csv;
  15.         print_r($csv);  
  16.         $db_quote = $quote . $quote;
  17.  
  18.         // Clean up file
  19.         $this -> csvData = trim($this -> csvData);
  20.         $this -> csvData = str_replace("\r\n", $newline, $this -> csvData);
  21.  
  22.         $this -> csvData = str_replace($db_quote, '"', $this -> csvData);
  23.         // replace double quotes with " HTML entities
  24.         $this -> csvData = str_replace(',",', ',,', $this -> csvData);
  25.         // handle ,"", empty cells correctly
  26.  
  27.         $this -> csvData .= $delimiter;
  28.         // Put a comma on the end, so we parse last cell
  29.  
  30.         $inquotes = false;
  31.         $start_point = 0;
  32.         $row = 0;
  33.  
  34.         for ($i = 0; $i < strlen($this -> csvData); $i++) {
  35.  
  36.             $char = $this -> csvData[$i];
  37.             if ($char == $quote) {
  38.                 if ($inquotes) {
  39.                     $inquotes = false;
  40.                 } else {
  41.                     $inquotes = true;
  42.                 }
  43.             }
  44.  
  45.             if (($char == $delimiter or $char == $newline) and !$inquotes) {
  46.                 $cell = substr($this -> csvData, $start_point, $i - $start_point);
  47.                 $cell = str_replace($quote, '', $cell);
  48.                 // Remove delimiter quotes
  49.                 $cell = str_replace('"', $quote, $cell);
  50.                 // Add in data quotes
  51.                 $data[$row][] = $cell;
  52.                 $start_point = $i + 1;
  53.                 if ($char == $newline) {
  54.                     $row++;
  55.                 }
  56.             }
  57.         }
  58.         $this -> preparedData = $data;
  59.     }
  60.  
  61.     public function testecho() {
  62.         print_r($this -> preparedData);
  63.     }
  64.  
  65.     public function Send_To_Database() {
  66.         $data = $this -> preparedData;
  67.         $host = 'localhost';
  68.         $db = 'guestlist';
  69.         $DBH = new PDO("mysql:host=$host;dbname=$db", "root", "");
  70.  
  71.         for ($x = 0; $x < count($data); $x++) {
  72.             print_r($data[$x]);
  73.             $STH = $DBH -> prepare("INSERT INTO gl_guests (listID, firstName, lastName, email, addGuests, affiliation, notes) values (:listID, :firstName, :lastName, :email, :addGuests, :affiliation, :notes)");
  74.             $STH -> bindParam(':listID', $data[$x][0]);
  75.             $STH -> bindParam(':firstName', $data[$x][1]);
  76.             $STH -> bindParam(':lastName', $data[$x][2]);
  77.             $STH -> bindParam(':email', $data[$x][3]);
  78.             $STH -> bindParam(':addGuests', $data[$x][4]);
  79.             $STH -> bindParam(':affiliation', $data[$x][5]);
  80.             $STH -> bindParam(':notes', $data[$x][6]);
  81.             $STH -> execute();
  82.             print_r($data[$x] . "\r\n");
  83.             $err = $STH->errorInfo();
  84.             print_r($err);  
  85.         }
  86.  
  87.     }
  88.  
  89. }
  90. ?>
Add Comment
Please, Sign In to add comment