<?php
// Konekkciju 
// $conn = mysqli_connect("localhost","tagstudi_ukica","Ukica2019","tagstudi_ulfeta");

//funkcija stampa elemente niza
function stampaj_niz($niz)
{
    echo "BR:".$i;
    while($i<=count($niz))
    {
        echo $i.":".$niz[$i++]."||";
    }
}
//Menja slova sa dijakritickim simbolima
function ocisti_od_dijakritickih_simbola($text)
{
  $textcx = str_replace('č', 'cx', $text); //č
  $textcx2 = str_replace('Č', 'CX', $textcx); //Č
  $textcy = str_replace('ć', 'cy', $textcx2); //ć
  $textcy2 = str_replace('Ć', 'CY', $textcy); //Ć
  $textzx = str_replace('ž', 'zx', $textcy2); //ž
  $textzx2 = str_replace('Ž', 'ZX', $textzx); //Ž  
  $textsx = str_replace('š', 'sx', $textzx2); //š
  $textsx2 = str_replace('Š', 'SX', $textsx); //Š 
  $textdx = str_replace('đ', 'dx', $textsx2); //đ
  $textdx2 = str_replace('Đ', 'DX', $textdx); //Đ   
  
  return $textdx2;
}
//Izdvaja skup dijagnoza
function select_all_ampule()
{
   $conn = mysqli_connect("localhost","tagstudi_ukica","Ukica2019","tagstudi_ulfeta");
   
   if (!mysqli_connect_errno())
   {
   //IZLISTAVA SKUP Ampule2  
  $sql = "SELECT `IDAmpule`,`FabrickiNaziv`,`Generik` FROM `Ampule`";
  $result = $conn->query($sql);

if ($result->num_rows > 0) {
 //CISCENJE PRAZNINA , SPECIJALNIH ZNAKOVA...
    while($row = $result->fetch_assoc()) {
        $Sifra=$row["IDAmpule"];
		
        $original = strtolower($row["Generik"]);//PREBACENA SVA SLOVA U MALA
		$original1 = trim(preg_replace('/\s\s+/', '\s', $original));//CICENJE VISESTRUKIH RAZMAKA
        $original2 = str_replace(' ', ';', $original1); //OBELEZAVANJE RAZMAKA ;
        $original3 = trim(preg_replace("/[^A-Za-z\-0-9,.;()]/", " ", $original2));//uKLANJANJE SVEGA SEM BROJEVA SLOVA I - . ,;
        $original4 = str_replace(';', ' ', $original3);//VRACANJE RAZMAKA
        $original5 = trim(preg_replace('/\s\s+/', '\s', $original4));//CICENJE VISESTRUKIH RAZMAKA
        $original61 = str_replace(',\s', '/ ', $original5);//OBELEZAVANJE SEPARATORA RECENICE
        $original6 = str_replace(',', '/ ', $original61);//OBELEZAVANJE SEPARATORA RECENICE
        $original71 = str_replace('.\s', '/ ', $original6);//OBELEZAVANJE KRAJA RECENICE
		$original7 = str_replace('.', '/ ', $original71);//OBELEZAVANJE KRAJA RECENICE
		$original81 = str_replace('(\s', '/ ', $original7);//OBELEZAVANJE KRAJA RECENICE
		$original8 = str_replace('(', '/ ', $original81);//OBELEZAVANJE KRAJA RECENICE
		$original91 = str_replace(')\s', '/ ', $original8);//OBELEZAVANJE KRAJA RECENICE
        $original9 = str_replace(')', '/ ', $original91);//OBELEZAVANJE KRAJA RECENICE
		 $original10 = str_replace('-\s', '/ ', $original9);//OBELEZAVANJE KRAJA RECENICE
		$original11 = str_replace('-', '/ ', $original10);//OBELEZAVANJE KRAJA RECENICE
		
    echo "<b>Originalna dijagnoza: </b>". $original."<br/>";
    echo "<b>Originalna dijagnoza: </b>". $original11."<br/>";
   
     
	    
    echo "<b>Originalna dijagnoza: </b>". $original."<br/>";
    echo "<b>Originalna dijagnoza: </b>". $original11."<br/>";
   
   	$array= explode('/',$original11);
     $i=0;
     while($i<count($array))
     {
		 $m1=$i+1;
		 
		 //UPISUJE CELINU
		 if($array[$i]!="" and ($i==0 or !preg_match('/^[0-9]/',$array[$i])))
		
               {
            
             //UPIS CELINE IZ lekova prvu i sve ostale koje ne pocinju sa brojem
              $sql = "INSERT INTO Ampula_celina ( IDAmpule, tip,idCeline, celinaAmpula_norm) VALUES(".$Sifra.", 1,".$m1.", '".$array[$i]."')";
                if ($conn->query($sql) === TRUE) {
                //echo "OK<br/>";
                } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
                }
            
               }
         
         // $array1=nil;
         $array1= explode(' ',$array[$i]);
         $ii=0;
		 
         while($ii<count($array1))
           {
			   //UPISUJE REC
               if($array1[$ii]!="" and ($ii==0 or !preg_match('/^[0-9]/',$array1[$ii])))
               {
            // echo $id." ".$m1." ".$array1[$ii]." "."<br/>";
             //UPIS RECI IZ CELINA
              $sql = "INSERT INTO Ampula_rec (IDAmpule, tip, idCeline, idRec, recA_norm) VALUES (".$Sifra.", 1,".$m1.",".$ii.", '".$array1[$ii]."')";
                if ($conn->query($sql) === TRUE){
               // echo "OK<br/>";
                } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
                }
            
               }
               $ii++;
           }
		   
            $i++;
     }
     echo "<br/>";
	 
	}
}
     else
      {
          echo "Zero rows!";
      }}//KRAJ CITANJA REDOVA
  else
  {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
      
  }
}
select_all_ampule();

?>