Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class csv extends Database {
- public $file = null;
- function __construct() {
- if(count($_SERVER["argv"]) > 1){
- $this->file = $_SERVER["argv"][1];
- }else{
- $this->file = "stock.csv";
- }
- $this->db = $db;
- }
- function saveCSVtoDB(){
- //opens the file
- $fp = fopen($this->file, "r");
- $db = Database::getInstance();
- $mysqli = $db->getConnection();
- $result = $mysqli->query("LOAD DATA LOCAL INFILE 'file.csv'
- IGNORE INTO TABLE products
- CHARACTER SET UTF8
- FIELDS TERMINATED BY ','
- LINES TERMINATED BY 'n'
- IGNORE 1 LINES
- (code, name, desc, stock, price, @avai)
- SET available = IF(@avai LIKE '%yes%', 1, 0)
- ");
- if($result){
- echo "success";
- }else{
- die("error");
- }
- }
- }
- class CSVParser
- {
- protected $file;
- protected $database;
- public function __construct($file, Database $db)
- {
- $this->file = $file;
- $this->db = $db;
- }
- public function saveCSVtoDB()
- {
- // @TODO import CSV to DB
- }
- }
- $file = isset($_SERVER["argv"][1]) ? $_SERVER["argv"][1] : "stock.csv";
- $parser = new CSVParser($file, Database::getInstance());
- $parser->saveCSVtoDB();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement