for($i = 1; $i <= 10; $i++)
{
$nom ='';
if(!empty($photo1) && $i == 1) $nom = $photo1;
elseif(!empty($photo2) && $i == 2) $nom = $photo2;
elseif(!empty($photo3) && $i == 3) $nom = $photo3;
elseif(!empty($photo4) && $i == 4) $nom = $photo4;
elseif(!empty($photo5) && $i == 5) $nom = $photo5;
elseif(!empty($photo6) && $i == 6) $nom = $photo6;
elseif(!empty($photo7) && $i == 7) $nom = $photo7;
elseif(!empty($photo8) && $i == 8) $nom = $photo8;
elseif(!empty($photo9) && $i == 9) $nom = $photo9;
elseif(!empty($photo10) && $i == 10) $nom = $photo10;
if(!empty($nom))
{
$sql_img = "INSERT INTO ". PREFIX ."annonces_images (id_ann, nom) VALUES (:id_ann, :nom)";
$req = $bdd->prepare($sql_img);
$req->bindValue('id_ann', $id, PDO::PARAM_INT);
$req->bindValue('nom', $nom, PDO::PARAM_STR);
$req->execute();
$req->closeCursor();
$g1 = 0;
$g = $g1;
for (; ; ) {
if ($g > 0) {
break;
}
$count = 0;
/* On cree la table */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $adresse[$g]['lien']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contenu = curl_exec($curl);
$xml ='';
$xml = new SimpleXMLElement($contenu);
foreach($xml->annonce as $v) {
foreach($v->photos as $values)
$values = $v->photos->children();
$code_pos = $v->codepostal;
$sqldep="SELECT ville_departement FROM PAG_villes_france WHERE ville_code_postal = '$code_pos'";
$req = $bdd->prepare($sqldep);
$req->execute();
$row = $req->fetch();
$s = $row['ville_departement'];
$sqldep2="SELECT id_dep, id_reg FROM PAG_departements WHERE cp = '$s'";
$req2 = $bdd->prepare($sqldep2);
$req2->execute();
$row2 = $req2->fetch();
$id_reg = $row2['id_reg'];
$id_dep = $row2['id_dep'];
$id_cat = 5;
$email = '***@***';
$password = 'volcane02';
$ville = ($v->ville);
$statut = 2;
$societe = ($v->agence);
$siret = '';
$type = 1;
$nom = ($v->agence);
$tel = 0000000000000;
$titre = ($v->titre);
$texte = ($v->descriptif_fr);
$prix = $v->prix;
$time = time();
$tel_cache = 'Y';
$ip = $_SERVER['REMOTE_ADDR'];
$xml1 = $v->id;
$key = 'aktimo21';
$xml_key = "$key$xml1";
// echo $values;
// $phot1= 'http://www.aktifimmo.com/photosp/6094/6094931_1.jpg';
// echo $photo1 = (!empty($phot1)) ? ($phot1) : '';
// echo $photo2 = (!empty($values->photo[1])) ? ($values->photo[1]) : '';
// echo $photo3 = (!empty($values->photo[2])) ? ($values->photo[2]) : '';
// echo $photo4 = (!empty($values->photo[3])) ? ($values->photo[3]) : '';
// echo $photo5 = (!empty($values->photo[4])) ? ($values->photo[4]) : '';
// $photo6 = (!empty($values->photo[5])) ? ($values->photo[5]) : '';
// $photo7 = (!empty($values->photo[6])) ? ($values->photo[6]) : '';
// $photo8 = (!empty($values[7])) ? ($values[7]) : '';
// $photo9 = (!empty($values[8])) ? ($values[8]) : '';
// $photo10 = (!empty($values[9])) ? ($values[9]) : '';
/* Ajouter un nouvel enregistrement dans la table */
$sql = "INSERT INTO ". PREFIX ."annonces (email, password, ville, nom, tel, tel_cache, ip, xml_key) VALUES
(:email, :password, :ville, :nom, :tel, :tel_cache, :ip, :xml_key)
ON DUPLICATE KEY UPDATE email = VALUES(email), ville = VALUES(ville), nom = VALUES(nom), tel = VALUES(tel), tel_cache = VALUES(tel_cache)";
$req = $bdd->prepare($sql);
$req->bindValue('email', $email, PDO::PARAM_STR);
$req->bindValue('password', $password, PDO::PARAM_STR);
$req->bindValue('ville', $ville, PDO::PARAM_STR);
$req->bindValue('nom', $nom, PDO::PARAM_STR);
$req->bindValue('tel', $tel, PDO::PARAM_STR);
$req->bindValue('tel_cache', $tel_cache, PDO::PARAM_STR);
$req->bindValue('ip', $ip, PDO::PARAM_STR);
$req->bindValue('xml_key', $xml_key, PDO::PARAM_STR);
$req->execute();
$id = $bdd->lastInsertId();
$req->closeCursor();
if(!empty($id))
{
$sql = "INSERT INTO ". PREFIX ."annonces_search (id_ann, id_reg, id_dep, id_cat, code_pos, status, type, titre, ann, prix, etat, date) VALUES";
$sql .= "(:id_ann, :id_reg, :id_dep, :id_cat, :code_pos, :statut, :type, :titre, :texte, :prix, :etat, :time)";
$sql .= " ON DUPLICATE KEY UPDATE id_reg = VALUES(id_reg), id_dep = VALUES(id_dep), id_cat = VALUES(id_cat), code_pos = VALUES(code_pos), titre = VALUES(titre), ann = VALUES(ann), prix = VALUES(prix)";
$req = $bdd->prepare($sql);
$req->bindValue('id_ann', $id, PDO::PARAM_INT);
$req->bindValue('id_reg', $id_reg, PDO::PARAM_INT);
$req->bindValue('id_dep', $id_dep, PDO::PARAM_INT);
$req->bindValue('id_cat', $id_cat, PDO::PARAM_INT);
$req->bindValue('code_pos', $code_pos, PDO::PARAM_STR);
$req->bindValue('statut', $statut, PDO::PARAM_INT);
$req->bindValue('type', $type, PDO::PARAM_INT);
$req->bindValue('titre', $titre, PDO::PARAM_STR);
$req->bindValue('texte', $texte, PDO::PARAM_STR);
$req->bindValue('prix', $prix, PDO::PARAM_STR);
$req->bindValue('etat', 2, PDO::PARAM_INT);
$req->bindValue('time', $time, PDO::PARAM_INT);
$req->execute();
$req->closeCursor();
if($societe != '')
{
$sql_soc = "INSERT INTO ". PREFIX ."annonces_societe (id_ann, nom_societe, siret) VALUES";
$sql_soc .= "(:id_ann, :societe, :siret) ON DUPLICATE KEY UPDATE nom_societe = VALUES(nom_societe), siret = VALUES(siret)";
$req = $bdd->prepare($sql_soc);
$req->bindValue('id_ann', $id, PDO::PARAM_INT);
$req->bindValue('societe', $societe, PDO::PARAM_STR);
$req->bindValue('siret', $siret, PDO::PARAM_STR);
$req->execute();
$req->closeCursor();
}
print_r($values->photo);
for($i = 1; $i <= 10; $i++)
{
$nom = $values->photo[$i];
if(!empty($nom))
{
echo $nom;
$sql_img = "INSERT INTO ". PREFIX ."annonces_images (id_ann, nom) VALUES (:id_ann, :nom)";
$req = $bdd->prepare($sql_img);
$req->bindValue('id_ann', $id, PDO::PARAM_INT);
$req->bindValue('nom', $nom, PDO::PARAM_STR);
$req->execute();
}
}
echo "<br>Importation termine avec succes";
/* Fermeture */
//MYSQL_CLOSE();
echo $g;
}
}
$g++;
}
?>
foreach($v->photos as $values) $values = $v->photos->children();
foreach($v->photos as $values) { $values = $v->photos->children(); . . . }</gras> echo "<br>Importation termine avec succes";
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionforeach($v->photos as $values)) donc du coup plus d'accolade.Bref
<?php $count = 0; /* On cree la table */ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $adresse[$g]['lien']); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); $contenu = curl_exec($curl); $xml =''; $xml = new SimpleXMLElement($contenu); foreach($xml->annonce as $v) { $values = $v->photos->children(); $code_pos = $v->codepostal; $sqldep="SELECT ville_departement FROM PAG_villes_france WHERE ville_code_postal = '$code_pos'"; $req = $bdd->prepare($sqldep); $req->execute(); $row = $req->fetch(); $s = $row['ville_departement']; $sqldep2="SELECT id_dep, id_reg FROM PAG_departements WHERE cp = '$s'"; $req2 = $bdd->prepare($sqldep2); $req2->execute(); $row2 = $req2->fetch(); $id_reg = $row2['id_reg']; $id_dep = $row2['id_dep']; $id_cat = 5; $email = '***@***'; $password = 'volcane02'; $ville = ($v->ville); $statut = 2; $societe = ($v->agence); $siret = ''; $type = 1; $nom = ($v->agence); $tel = 0000000000000; $titre = ($v->titre); $texte = ($v->descriptif_fr); $prix = $v->prix; $time = time(); $tel_cache = 'Y'; $ip = $_SERVER['REMOTE_ADDR']; $xml1 = $v->id; $key = 'aktimo21'; $xml_key = "$key$xml1"; /* Ajouter un nouvel enregistrement dans la table */ $sql = "INSERT INTO ". PREFIX ."annonces (email, password, ville, nom, tel, tel_cache, ip, xml_key) VALUES (:email, :password, :ville, :nom, :tel, :tel_cache, :ip, :xml_key) ON DUPLICATE KEY UPDATE email = VALUES(email), ville = VALUES(ville), nom = VALUES(nom), tel = VALUES(tel), tel_cache = VALUES(tel_cache)"; $req = $bdd->prepare($sql); $req->bindValue('email', $email, PDO::PARAM_STR); $req->bindValue('password', $password, PDO::PARAM_STR); $req->bindValue('ville', $ville, PDO::PARAM_STR); $req->bindValue('nom', $nom, PDO::PARAM_STR); $req->bindValue('tel', $tel, PDO::PARAM_STR); $req->bindValue('tel_cache', $tel_cache, PDO::PARAM_STR); $req->bindValue('ip', $ip, PDO::PARAM_STR); $req->bindValue('xml_key', $xml_key, PDO::PARAM_STR); $req->execute(); $id = $bdd->lastInsertId(); echo $id; $req->closeCursor(); if(!empty($id)) { $sql = "INSERT INTO ". PREFIX ."annonces_search (id_ann, id_reg, id_dep, id_cat, code_pos, status, type, titre, ann, prix, etat, date) VALUES"; $sql .= "(:id_ann, :id_reg, :id_dep, :id_cat, :code_pos, :statut, :type, :titre, :texte, :prix, :etat, :time)"; $sql .= " ON DUPLICATE KEY UPDATE id_reg = VALUES(id_reg), id_dep = VALUES(id_dep), id_cat = VALUES(id_cat), code_pos = VALUES(code_pos), titre = VALUES(titre), ann = VALUES(ann), prix = VALUES(prix)"; $req = $bdd->prepare($sql); $req->bindValue('id_ann', $id, PDO::PARAM_INT); $req->bindValue('id_reg', $id_reg, PDO::PARAM_INT); $req->bindValue('id_dep', $id_dep, PDO::PARAM_INT); $req->bindValue('id_cat', $id_cat, PDO::PARAM_INT); $req->bindValue('code_pos', $code_pos, PDO::PARAM_STR); $req->bindValue('statut', $statut, PDO::PARAM_INT); $req->bindValue('type', $type, PDO::PARAM_INT); $req->bindValue('titre', $titre, PDO::PARAM_STR); $req->bindValue('texte', $texte, PDO::PARAM_STR); $req->bindValue('prix', $prix, PDO::PARAM_STR); $req->bindValue('etat', 2, PDO::PARAM_INT); $req->bindValue('time', $time, PDO::PARAM_INT); $req->execute(); $req->closeCursor(); if($societe != '') { $sql_soc = "INSERT INTO ". PREFIX ."annonces_societe (id_ann, nom_societe, siret) VALUES"; $sql_soc .= "(:id_ann, :societe, :siret) ON DUPLICATE KEY UPDATE nom_societe = VALUES(nom_societe), siret = VALUES(siret)"; $req = $bdd->prepare($sql_soc); $req->bindValue('id_ann', $id, PDO::PARAM_INT); $req->bindValue('societe', $societe, PDO::PARAM_STR); $req->bindValue('siret', $siret, PDO::PARAM_STR); $req->execute(); $req->closeCursor(); } for($i = 1; $i <= 10; $i++) { $nom = $values->photo[$i]; if(!empty($nom)) { echo $nom; $sql_img = "INSERT INTO ". PREFIX ."annonces_images (id_ann, nom) VALUES (:id_ann, :nom)"; $req = $bdd->prepare($sql_img); $req->bindValue('id_ann', $id, PDO::PARAM_INT); $req->bindValue('nom', $nom, PDO::PARAM_STR); $req->execute(); echo $sql_img; } } echo "<br>Importation termine avec succes"; } } ?>
echo $id;et à la place du for
for($i = 1; $i <= 10; $i++){ ... }écris :
for($i = 1; $i <= 10; $i++) { echo "<br /> photos->photo :" . $i . " : " . $v->photos->photo[$i]; $nom = $v->photos->photo[$i]; if(!empty($nom)) { $sql_img = "INSERT INTO ". PREFIX ."annonces_images (id_ann, nom) VALUES (:id_ann, :nom)"; $req = $bdd->prepare($sql_img); $req->bindValue('id_ann', $id, PDO::PARAM_INT); $req->bindValue('nom', $nom, PDO::PARAM_STR); $req->execute(); echo $sql_img; } else { echo "#EMPTY"; } }
if(!empty($nom))
if(!($nom==''))à la place
for($i = 0; $i < 10; $i++)
{
.
.
.
}
2 mai 2014 à 15:15
dans le premier message tu mettais :
Pourquoi ne pas simplement faire :
=> closeCursor n'est pas necessaire en mysql (et encore plus lors d'un insert)
=> pourquoi s'embêter avec des variables ($photo) quand le tableau contient dejà tout ?
=> afficher le tableau permettra de checker le contenu de base et l'écho chaque iteration.
Dis moi ce que ca donne !
naga
3 mai 2014 à 10:24