domxpath - Get Title, URL, Description and insert it to database -
i'm new here , need fix code.
i'm trying make code using domxpath
grab title, url , description bing search , save db.
here code :
<?php $s="something-words"; $keywords = strstr($s, '-') ? str_replace('-', '+', $s) : $s; $html5 = new domdocument(); @$html5->loadhtmlfile('http://www.bing.com/search?q='.$keywords.'&go=&qs=bs&filt=all'); $xpath5 = new domxpath($html5); $nodes = $xpath5->query('//div[@class="sb_tlst"]/h3'); $nodes = $xpath5->query('//div[@class="sb_meta"]/cite'); $nodes = $xpath5->query('//div[@id="results"]/ul[@id="wg0"]/li/div/div/p'); $data = array(); $data2 = array(); $data3 = array(); $i = 0; foreach ($nodes $node) { $data = $node->textcontent; $i++; // insert table urlgrab mysql_query( "insert urlgrab(title) values ('$data')"); $data2 = $node->textcontent; $i++; // update table urlgrab dbconnect(); mysql_query( "update urlgrab set url='$data2' title='$data'" ); $data3 = $node->textcontent; $i++; // update table urlgrab dbconnect(); mysql_query( "update urlgrab set description='$data3' title='$data'" ); } ?>
the problem same results in database title,url,description. how fix code data title,url , description save db?
as have messed code it's hard identified. assumption have generated below code should work you.
$titles = $xpath5->query('//div[@class="sb_tlst"]/h3'); $urls = $xpath5->query('//div[@class="sb_meta"]/cite'); $descriptions = $xpath5->query('//div[@id="results"]/ul[@id="wg0"]/li/div/div/p'); $arrtitle = array(); foreach($titles $title){ $arrtitle[] = $title->textcontent; } $arrurl = array(); foreach($urls $url){ $arrurl[] = $url->textcontent; } $arrdescription = array(); foreach($descriptions $description){ $arrdescription[] = $description->textcontent; } $i = 0; dbconnect(); foreach ($i=0; $i < count($arrtitle); $i++) { $title = $arrtitle[$i]; $url = $arrurl[$i]; $description = $arrdescription[$i]; mysql_query( "insert urlgrab(`title`, `url`, `description`) values ('$title', '$url', '$description')"); }
*remove $i++;
in loop , run. we're doing $i++
in for
loop * , solve issue.
Comments
Post a Comment