-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.php
More file actions
63 lines (55 loc) · 2.59 KB
/
script.php
File metadata and controls
63 lines (55 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
require("./DB/Connection.php");
$jsonFile = file_get_contents("./products.json");
if( $jsonFile ){
$jsonArray = json_decode($jsonFile);
unset($jsonFile);
ini_set('max_execution_time', 3600);
if( $mysqli->query("DELETE FROM products WHERE sku!=0") )// wipe table
echo "Tous les enregistrements des <strong>Produits</strong> sont supprimés <br>";
else
echo "Les enregistrements de la table des<strong>Produits</strong> ne sont pas supprimés, Des erreurs d'insertion peuvent servenir ! <br>";
if( $mysqli->query("DELETE FROM categories WHERE id!=0") )// wipe table
echo "Tous les enregistrements des <strong>Categories</strong> sont supprimés <br>";
else
echo "Les enregistrements de la table des<strong>Categories</strong> ne sont pas supprimés, Des erreurs d'insertion peuvent servenir ! <br>";
$productsQuery = $mysqli->prepare("INSERT INTO products VALUES (?, ?, ?, ?, ?,?, ?, ?, ?, ?, ?)");
$productsQuery->bind_param( "issdsdsssss", $sku, $name, $type, $price, $upc, $shipping, $description, $manufacturer, $model, $url, $image);
$categoriesQuery = $mysqli->prepare("INSERT IGNORE INTO categories VALUES (?, ?)");
$categoriesQuery->bind_param( "ss", $cat_id, $cat_name);
$ProductCatsQuery = $mysqli->prepare("INSERT IGNORE INTO product_categories VALUES (?, ?)");
$ProductCatsQuery->bind_param( "is", $product_id, $category_id);
$rows = 100; // just 100
foreach ( $jsonArray as $product ){
if(!$rows) break;
$rows --;
$sku = $product->sku;
$name = $product->name;
$type = $product->type;
$price = $product->price;
$upc = $product->upc;
$shipping = $product->shipping;
$description = $product->description;
$manufacturer = $product->manufacturer?$product->manufacturer:NULL;
$model = $product->model?$product->model:NULL;
$url = $product->url;
$image = $product->image;
$productsQuery->execute();
$product_id = $sku;
foreach ( $product->category as $category){
$category_id = $category->id;
$cat_id= $category->id;
$cat_name= $category->name;
$categoriesQuery->execute();
$ProductCatsQuery->execute();
}
}
$categoriesQuery->close();
$ProductCatsQuery->close();
$productsQuery->close();
echo $rows." produits sont ajoutés avec succès + leurs catégories + affectation de chaque produit à certaines catégories !";
}else{
//error opening the file
echo "Fichier 'products.json' est introuvable !";
exit();
}