Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. class csv extends Database {
  2.  
  3. public $file = null;
  4.  
  5. function __construct() {
  6.  
  7. if(count($_SERVER["argv"]) > 1){
  8. $this->file = $_SERVER["argv"][1];
  9. }else{
  10. $this->file = "stock.csv";
  11. }
  12.  
  13. $this->db = $db;
  14.  
  15. }
  16.  
  17. function saveCSVtoDB(){
  18. //opens the file
  19. $fp = fopen($this->file, "r");
  20.  
  21.  
  22.  
  23. $db = Database::getInstance();
  24. $mysqli = $db->getConnection();
  25.  
  26. $result = $mysqli->query("LOAD DATA LOCAL INFILE 'file.csv'
  27. IGNORE INTO TABLE products
  28. CHARACTER SET UTF8
  29. FIELDS TERMINATED BY ','
  30. LINES TERMINATED BY 'n'
  31. IGNORE 1 LINES
  32. (code, name, desc, stock, price, @avai)
  33. SET available = IF(@avai LIKE '%yes%', 1, 0)
  34. ");
  35.  
  36. if($result){
  37. echo "success";
  38. }else{
  39. die("error");
  40. }
  41.  
  42. }
  43. }
  44.  
  45. class CSVParser
  46. {
  47. protected $file;
  48. protected $database;
  49.  
  50. public function __construct($file, Database $db)
  51. {
  52. $this->file = $file;
  53. $this->db = $db;
  54. }
  55.  
  56. public function saveCSVtoDB()
  57. {
  58. // @TODO import CSV to DB
  59. }
  60. }
  61.  
  62. $file = isset($_SERVER["argv"][1]) ? $_SERVER["argv"][1] : "stock.csv";
  63.  
  64. $parser = new CSVParser($file, Database::getInstance());
  65. $parser->saveCSVtoDB();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement