Last active
August 29, 2015 14:19
-
-
Save Michales/e811ddbfe4af1061b876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
POUŽITÍ | |
1) Vytvořit proměnnou pro třídu Ulozit | |
$promenna = new Ulozit('host', 'user', 'pass', 'databaze'); | |
2) Poté načíst CSV dvěma způsoby | |
a) Cesta k souboru | |
$promenna->soubor('soubor.csv'); | |
b) Předání obsahu CSV přímo | |
$promenna->data('blablabla*blablabla*...'); | |
*/ | |
if (isset($_FILES['fupload']['tmp_name'])) | |
{ | |
// POKUD se nahrál soubor | |
if (!file_exists('./csv')) | |
{ | |
// Pokud neexistuje složka "csv", vytvořím novou | |
mkdir('./csv'); | |
} | |
// Nový název | |
$nazev = Date('YmdHis').'.csv'; | |
// Uložit do složky | |
move_uploaded_file($_FILES['fupload']['tmp_name'], './csv/'.$nazev); | |
// Import do databáze - NASTAVIT UDAJE!!! | |
$ulozit = new Ulozit('localhost', 'root', '', 'database'); | |
$ulozit->soubor('./csv/'.$nazev); | |
} | |
class Ulozit | |
{ | |
function __construct($db_host, $db_user, $db_pass, $db_db) | |
{ | |
$this->databaze = mysql_connect($db_host, $db_user, $db_pass); | |
mysql_select_db($db_db, $this->databaze); | |
mysql_query('SET NAMES "utf8"', $this->databaze); | |
} | |
function soubor($cesta) | |
{ | |
if (file_exists($cesta)) | |
{ | |
$this->zpracovat(file_get_contents($cesta)); | |
} else | |
{ | |
echo 'Soubor nenalezen'; | |
} | |
} | |
function data($data) | |
{ | |
$this->zpracovat($data); | |
} | |
function zpracovat($data) | |
{ | |
$pole = explode("\n", trim($data)); | |
for ($i=0; $i<count($pole); $i++) | |
{ | |
$radek = explode('*', $pole[$i]); | |
if (trim($pole[$i]) != '' && trim($radek[9]) != '') | |
{ | |
if ($radek[0] != 'Year' && $radek[1] != 'Album') | |
{ | |
$hledat = $this->interpret($radek[9]); | |
if ($hledat != false) | |
{ | |
$id_interpret = $hledat; | |
} else | |
{ | |
$vlozit = $this->db('INSERT INTO interprets VALUES("", "'.$radek[9].'")'); | |
$id_interpret = mysql_insert_id(); | |
} | |
$hledat_album = $this->album($id_interpret, $radek[1], $radek[2], $radek[0], $radek[8]); | |
if ($hledat_album != false) | |
{ | |
$id_album = $hledat_album; | |
} else | |
{ | |
$vlozit = $this->db('INSERT INTO albums VALUES("", "'.$id_interpret.'", "'.$radek[1].'", "'.$radek[2].'", "'.$radek[0].'", "'.$radek[8].'", "'.$radek[10].'", "'.$radek[11].'")'); | |
$id_album = mysql_insert_id(); | |
} | |
$vlozit_song = $this->db('INSERT INTO songs VALUES("", "'.$id_album.'", "'.$radek[4].'", "'.$radek[3].'", "'.$radek[7].'", "'.$radek[6].'", "'.$radek[5].'", "'.$radek[12].'")'); | |
} | |
} | |
} | |
} | |
function album($interpret, $album, $albumartist, $year, $publisher) | |
{ | |
$hledani = $this->db('SELECT * FROM albums WHERE interpret = '.$interpret.' and Album = "'.$album.'" and Albumartist = "'.$albumartist.'" and Year = "'.$year.'" and Publisher = "'.$publisher.'"'); | |
if ($hledani[0]['id'] != null) | |
{ | |
return $hledani[0]['id']; | |
} else | |
{ | |
return false; | |
} | |
} | |
function interpret($hledat) | |
{ | |
$hledani = $this->db('SELECT * FROM interprets WHERE name = "'.$hledat.'"'); | |
if ($hledani[0]['id'] != null) | |
{ | |
return $hledani[0]['id']; | |
} else | |
{ | |
return false; | |
} | |
} | |
function db($dotaz, $index=null) | |
{ | |
$data = array(); | |
$db_dotaz = mysql_query($dotaz, $this->databaze); | |
if (substr($dotaz, 0, 6) == 'SELECT') | |
{ | |
while ($radek = mysql_fetch_array($db_dotaz, MYSQL_ASSOC)) | |
{ | |
if ($index != null) | |
{ | |
$data[$radek[$index]] = $radek; | |
} else | |
{ | |
$data[count($data)] = $radek; | |
} | |
} | |
return $data; | |
} else { | |
return true; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment