
<?php
//import.php
sleep(2);
$output = '';

if(isset($_FILES['file']['name']) &&  $_FILES['file']['name'] != '')
{
 $valid_extension = array('xml');
 $file_data = explode('.', $_FILES['file']['name']);
 $file_extension = end($file_data);
 if(in_array($file_extension, $valid_extension))
 {
  $data = simplexml_load_file($_FILES['file']['tmp_name']);
  $connect = new PDO('mysql:host=localhost;dbname=xml','root', '');
  $connect->exec("set names utf8mb4");
  $query = "
  INSERT INTO `item`(`product_id`, `model`, `name`, `description`, `sku`, `ean`, `image`, `manufacturer`, `additional_images`, `product_filter`, `meta_title`, `product_attribute`, `product_category`, `price`) VALUES (:product_id, :model, :name, :description, :sku, :ean, :image, :manufacturer, :additional_images, :product_filter, :meta_title, :product_attribute, :product_category, :price);
  ";
  $statement = $connect->prepare($query);
  for($i = 0; $i < count($data); $i++)
  {
   $statement->execute(
    array(
    ':product_id'    => $data->item[$i]->product_id,
    ':model'     => $data->item[$i]->model,
    ':name'     => $data->item[$i]->name,
    ':description'    => $data->item[$i]->description,
    ':sku'     => $data->item[$i]->sku,
    ':ean'    => $data->item[$i]->ean,
    ':image'     => $data->item[$i]->image,
	':manufacturer'    => $data->item[$i]->manufacturer,
	':additional_images'    => $data->item[$i]->additional_images,
	':product_filter'     => $data->item[$i]->product_filter,
	':meta_title'     => $data->item[$i]->meta_title,
	':product_attribute'     => $data->item[$i]->product_attribute,
	':product_category'     => $data->item[$i]->product_category->category,
	':price'     => $data->item[$i]->price
    )
   );

  }
  $result = $statement->fetchAll();
  if(isset($result))
  {
   $output = '<div class="alert alert-success">Import Data Done</div>';
  }
 }
 else
 {
  $output = '<div class="alert alert-warning">Invalid File</div>';
 }
}
else
{
 $output = '<div class="alert alert-warning">Please Select XML File</div>';
}

echo $output;

?>

