<?php
$root = ('c:\xampp\htdocs');
require_once "$root\lexington\includes\connection_home_mysqli.php";

$start = time(); 
$db->set_charset("utf8");
$xml = file_get_contents('c:\xampp\htdocs\lexington\php\wawi_files\99.xml');
$array = json_decode(json_encode((array)simplexml_load_string($xml)),1);

$array = $array['products']['item'];
$array = array_values ($array);
	
	for($i=0;$i <count ($array);$i++){ //<-- count 1
	$number = $array[$i]['number'];
		
		$producer_id = $array[$i]['produceritem'];
		$search = array ();
		$replace = array ();
		$producer_id = str_replace( $search, $replace, strtolower( $producer_id ) );
		
		if (array_key_exists ("stock",$array[$i]['sizes']['size'])){
			//STOCKCHEK
			$array_formated[$number][0] = $array[$i]['sizes']['size']['stock'];
			
			//GET SIZE
			$array_sizebarcode[$number][0] = $array[$i]['sizes']['size']['sizebarcode'];		
			$array_sizebarcode_string = implode (";",$array_sizebarcode[$number]);
			//GET PRICE
			$array_sp[$number][0] = trim ($array[$i]['sizes']['size']['sp']);
			$array_sp_string = implode (";",$array_sp[$number]);
			$array_sp_string = str_replace(',','.',$array_sp_string);
			//GET STOCK
			$array_stock[$number][0] = $array[$i]['sizes']['size']['stock'];
			$array_stock_string = implode (";",$array_stock[$number]);
			//GET EAN
			if (array_key_exists ("ean",$array[$i]['sizes']['size']))
			{
			$array_ean[$number][0] = $array[$i]['sizes']['size']['ean'];
			$array_ean_string = implode (";",$array_ean[$number]);
			}elseif (array_key_exists ("upc",$array[$i]['sizes']['size']))
			{
			$array_ean[$number][0] = $array[$i]['sizes']['size']['upc'];
			$array_ean_string = implode (";",$array_ean[$number]);
			}else
			{$array_ean_string = '';}
			
			$sql = ("UPDATE lexington SET
						LEX_stock = ?
						WHERE LEX_ID = ?");
									
						$update = $db->prepare( $sql );
						$update->bind_param( 'is',
						$array_stock_string,$number);
						$update->execute();
			
		}

	for ($a=0;$a<count($array[$i]['sizes']['size']);$a++){ // <-- count 2
	
		if (array_key_exists ("0",$array[$i]['sizes']['size'])){
			
			//STOCKCHEK
			$array_formated[$number][] = $array[$i]['sizes']['size'][$a]['stock'];
			
			//GET SIZE
			$array_sizebarcode[$number][] = $array[$i]['sizes']['size'][$a]['sizebarcode'];		
			$array_sizebarcode_string = implode (";",$array_sizebarcode[$number]);
			//GET PRICE
			$array_sp[$number][] = trim ($array[$i]['sizes']['size'][$a]['sp']);
			$array_sp_string = implode (";",$array_sp[$number]);
			$array_sp_string = str_replace(',','.',$array_sp_string);
			//GET STOCK
			$array_stock[$number][] = $array[$i]['sizes']['size'][$a]['stock'];
			$array_stock_string = implode (";",$array_stock[$number]);
			//GET EAN
			if (array_key_exists ("ean",$array[$i]['sizes']['size'][$a]))
			{
			$array_ean[$number][] = $array[$i]['sizes']['size'][$a]['ean'];
			//$array_ean_string = implode (";",$array_ean[$number]);
			}elseif (array_key_exists ("upc",$array[$i]['sizes']['size'][$a]))
			{
			$array_ean[$number][] = $array[$i]['sizes']['size'][$a]['upc'];
			}else
			{$array_ean[$number][] = '';}
			
			$array_ean_string = implode (";",$array_ean[$number]);
			
			
			
			$sql = ("UPDATE lexington SET
						LEX_varstring = ?, LEX_stockstring = ?, LEX_pricestring = ?, LEX_eanstring = ?,LEX_producer_id = ?
						WHERE LEX_ID = ?");
									
						$update = $db->prepare( $sql );
						$update->bind_param( 'ssssss',
						$array_sizebarcode_string,$array_stock_string,$array_sp_string,$array_ean_string,$producer_id,$number);
						$update->execute();
		
		}
	}	
		
	
	} //<-- END FOR
	

$db->close();
$end = time();
$laufzeit = $end-$start;
echo $laufzeit.' Sek'.'<br/>';

//http://localhost/lexington/php/cronjob/1-update_stock_lexington.php

?> 