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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| // (....)
$data_json = '{
"produits": [
{
"grams": 0,
"product_id": 11111,
"name": "titi",
"product_exists": true
},
{
"grams": 1000,
"product_id": 11112,
"name": "titiqqq",
"product_exists": true
},
{
"grams": 1000,
"product_id": 11114,
"name": "toto",
"product_exists": true
},
{
"grams": 2050,
"product_id": 11113,
"name": "tata",
"product_exists": true
},
{
"grams": 1000,
"product_id": 11162,
"name": "tataaa",
"product_exists": true
},
{
"grams": 1000,
"product_id": 11115,
"name": "tatasss",
"product_exists": true
}
]
}';
$data_obj = json_decode($data_json);
$count = count($data_obj->produits);
$i = 0;
while ($i < $count)
{
foreach ($data_obj->produits as $obj)
{
$nom_produit = $obj->name;
$produit_id = $obj->product_id;
//$critere_nom = array("titiqqq", "toto", "tataaa", "tatasss",);
/* construction mon array : $critere_nom --- */
try
{
$query = 'SELECT '. $champ_nom_critere. '
FROM '. $tableData_critere . '
';
$req = $bdd->prepare($query);
$req->execute(array('nom'));
$critere_nom = $req->fetchAll(PDO::FETCH_COLUMN, 0);
//PDOStatement::fetchAll
//fetch_style
//Pour retourner un tableau contenant toutes les valeurs d'une seule colonne depuis le jeu de résultats, spécifiez PDO::FETCH_COLUMN. Vous pouvez spécifier quelle colonne vous voulez avec le paramètre column-index.
//L'exemple suivant montre comment retourner toutes les valeurs d'une seule colonne depuis un jeu de résultats, même si la requête SQL retourne plusieurs colonnes par lignes.
//http://php.net/manual/fr/pdostatement.fetchall.php
}
catch(Exception $e)
{
throw $e;
}
// /* *** */ // in_array Indique si une valeur appartient à un tableau (ici vient de MySql : $critere_nom
if (!in_array($obj->name, $critere_nom)) // selon les cretere on ne les met pas dans mySql
{
$stmt = $bdd->prepare("INSERT INTO $tableData ($champ_nom,
$champ_product_id)
VALUES (:champ_nom,
:champ_product_id)");
$stmt->execute(array(':champ_nom'=>$nom_produit,
':champ_product_id'=>$produit_id
));
}
$i++;
}
} |
Partager