123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
-
-
-
- include_once "config.php";
- error_reporting(E_ALL);
- ini_set("display_errors",1);
-
- foreach (glob("../ndata/*.txt") as $file){
-
- $laboratory = array();
- $pi = array();
- $cho = array();
- $register = array();
-
- $success = 0;
- $null = "n/a";
- $row = 1;
-
- if(($fhandle = fopen($file,"r")) !== FALSE){
- while(($data = fgetcsv($fhandle,1000,"\t")) !== FALSE){
-
- if($row > 6 and $row < 12){
-
- if($data[0]){
- $laboratory[$data[0]] = $data[1];
- }
- if($row < 10){
- $pi[$data[7]] = $data[8];
- }
- else{
- $cho[$data[7]] = $data[8];
- }
- }
-
- elseif($row == 12){
- $cho[$data[7]] = $data[8];
- }
-
- elseif($row == 14){
- for($index=0; $index < count($data); $index++){
-
- if($data[$index]){
- $register[$data[$index]] = "";
- }
- }
- foreach($pi as $key=>&$value){
- if(!$value){
- $value = $null;
- }
- }
- foreach($cho as $key=>&$value){
- if(!$value){
- $value = $null;
- }
- }
-
-
- $person = $db->stmt_init();
- if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
- $person->bind_param("sssss",$pi['pi'],$pi['piemail'],$null,$pi['piphone'],$null);
- if($person->execute()){
- $pi = $db->insert_id;
- }
-
- else{
- $email = $pi['piemail'];
-
- if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
-
- $person->bind_param("s",$email);
- if($person->execute()){
-
- $person->bind_result($pi);
- $person->fetch();
- }
- }
- }
- }
-
- $person = $db->stmt_init();
- if($person = $db->prepare("INSERT INTO Person(pname,email,password,phone,privileges) VALUES (?,?,?,?,?)")){
- $person->bind_param("sssss",$cho['cho'],$cho['choemail'],$null,$cho['chophone'],$null);
- if($person->execute()){
- $cho = $db->insert_id;
- }
-
- else{
- $email = $cho['choemail'];
-
- if ($person = $db->prepare("SELECT pid FROM Person WHERE email=?")){
-
- $person->bind_param("s",$email);
- if($person->execute()){
-
- $person->bind_result($cho);
- $person->fetch();
- }
- }
- }
- }
- $person->close();
-
-
- $lab = $db->stmt_init();
- if($lab = $db->prepare("INSERT INTO Laboratory(lname,lab,department,building,extension,pi,cho) VALUES (?,?,?,?,?,?,?)")){
- $lab->bind_param("sssssii",$laboratory['lname'],$laboratory['lab'],$laboratory['department'],$laboratory['building'],$laboratory['extension'],$pi,$cho);
- $lab->execute();
- $lab->close();
- $lab = $db->insert_id;
- }
- }
-
- elseif($row > 15){
- $d = 0;
- foreach($register as $key=>&$value){
-
- if($data[$d]){
- $value = $data[$d];
- }
- else{
- $value = $null;
- }
- $d++;
-
- if($d >= count($register)){
- break;
- }
- }
-
-
- $inventory = $db->stmt_init();
- if($register['chemical'] != "null entry"){
- if($inventory = $db->prepare("INSERT INTO Inventory(chemical,manufacturer,sds,cas,state,hazard,type,amount,quantity,total,location,ghs,lid) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")){
- $inventory->bind_param("ssssssssssssi",$register['chemical'],$register['manufacturer'],$register['sds'],$register['cas'],$register['state'],$register['hazard'],$register['type'],$register['amount'],$register['quantity'],$register['total'],$register['location'],$register['ghs'],$lab);
-
- if(!($inventory->execute())){
- echo "Entry error:";
- echo "<br>";
- print_r($inventory);
- echo "<br>";
- print_r($register);
- echo "<br>";
- echo "PI id: " . $pi . "<br>";
- echo "CHO id: " . $cho . "<br>";
- echo "LAB id: " . $lab . "<br>";
- }
-
- else{
- $success++;
- $inventory->close();
- }
- }
- }
- }
- $row++;
- }
-
- fclose($fhandle);
- echo "<br>FILE: " . $file . "<br>";
- echo "PI id: " . $pi . "<br>";
- echo "CHO id: " . $cho . "<br>";
- echo "LAB id: " . $lab . "<br>";
- echo $success . " successful entries." . "<br><br>";
- }
- }
-
- $db->close();
- ?>
|