par
moogli » 05 mai 2013, 17:04
salut,
je ne comprend pas trop ta question, tu veux prendre le nom du lien et le lien pour insérer dans la base ?
si oui, je ferais comme ça (à partir de ton code)
<?php
$xml = <<<xml
<div id="container_global">
<div>
<div>
<div>
<div>
<div>
<a href="lien1">titre 1</a>
</div>
<div>
<a href="lien2">titre 2</a>
</div>
</div>
</div>
</div>
</div>
</div>
xml;
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root');
$prepare = $pdo->prepare('insert into damien(titre,lien) values(:titre, :lien)');
$dom = new DOMDocument();
$dom->loadHTML($xml);
$xpath = new DOMXPath($dom);
$str_nodes1="//div[@id='container_global']/div/div/div/div/div/a";
$str_nodes2="//div[@id='container_global']/div/div/div/div/div/a/@href";
$result1 = $xpath->query($str_nodes1);
foreach ($result1 as $div1) {
$titre[]= $div1->nodeValue;
}
$r2 = $xpath->query($str_nodes2);
$i = 0;
foreach($r2 as $lien){
$prepare->bindValue(':titre',$titre[$i]);
$prepare->bindValue(':lien',$lien->nodeValue);
$prepare->execute();
$i++;
}
@+
salut,
je ne comprend pas trop ta question, tu veux prendre le nom du lien et le lien pour insérer dans la base ?
si oui, je ferais comme ça (à partir de ton code)
[php]<?php
$xml = <<<xml
<div id="container_global">
<div>
<div>
<div>
<div>
<div>
<a href="lien1">titre 1</a>
</div>
<div>
<a href="lien2">titre 2</a>
</div>
</div>
</div>
</div>
</div>
</div>
xml;
$pdo = new PDO('mysql:host=localhost;dbname=test', 'root');
$prepare = $pdo->prepare('insert into damien(titre,lien) values(:titre, :lien)');
$dom = new DOMDocument();
$dom->loadHTML($xml);
$xpath = new DOMXPath($dom);
$str_nodes1="//div[@id='container_global']/div/div/div/div/div/a";
$str_nodes2="//div[@id='container_global']/div/div/div/div/div/a/@href";
$result1 = $xpath->query($str_nodes1);
foreach ($result1 as $div1) {
$titre[]= $div1->nodeValue;
}
$r2 = $xpath->query($str_nodes2);
$i = 0;
foreach($r2 as $lien){
$prepare->bindValue(':titre',$titre[$i]);
$prepare->bindValue(':lien',$lien->nodeValue);
$prepare->execute();
$i++;
}[/php]
@+