I have two tables "product_title" having product titles and "product_list_url" having url of product list pages. I want to fetch title from product title table and fetch and go to each url one by one and search for same title on page and if found, extract some data and save into Database.
I want to check each product on each url. but could not get required result.
here is my code..
<?php
set_time_limit(0);
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "crawl";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Error connecting to database");
mysql_select_db($dbname, $conn);
$q1 = mysql_query("SELECT * FROM `product-title`");
$q2 = mysql_query("SELECT * FROM `product-list-url`");
while($res1 = mysql_fetch_assoc($q1)){
$product_title = $res1['title'];
while($res2 = mysql_fetch_assoc($q2)){
$url = $res2['url'];
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$found = $xpath->evaluate("boolean(//span[contains(text(), '$product_title')])");
if($found == false){
echo "Not Found";
}
else{
$elements = $xpath->evaluate("//span[contains(text(), '$product_title' )]/following-sibling::div/span[@class='list_sale_price']");
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
$price = $node->nodeValue;
$price1 = preg_replace('/[^0-9-.]/','',$price);
$date = date('y-m-d');
mysql_query("INSERT INTO `prices` (`ptitle`, `price`, `date`) VALUES ('$product_title', '$price1', '$date')") or die(mysql_error());
}
}
}
}
}
}
?>
it insert only one record for 1st title.