julia estebanos
julia estebanos

Reputation: 3

Get Title, URL, Description and insert it to database

I'm new here and need help to fix my code.

I'm trying to make a code using DomXPath to grab title, url and description from bing search and then save it to my DB.

Here is the 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 as $node) {
     $data = $node->textContent;
     $i++;
     // insert table urlgrab
     mysql_query( "INSERT INTO urlgrab(title) Values ('$data')");
     $data2 = $node->textContent;
     $i++;
     // update table urlgrab
     dbConnect();
     mysql_query( "UPDATE urlgrab SET url='$data2' WHERE title='$data'" );
     $data3 = $node->textContent;
     $i++;
     // update table urlgrab
     dbConnect();
    mysql_query( "UPDATE urlgrab SET description='$data3' WHERE title='$data'" );
}
?>

the problem is I get same results in database for title,url,description. How to fix this code to get all data title,url and description save to my DB?

Upvotes: 0

Views: 354

Answers (1)

Smile
Smile

Reputation: 2758

As you have messed up you code so it's hard to identified. But by assumption I have generated below code which should work for 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 as $title){
    $arrTitle[] = $title->textContent;
}

$arrUrl = array();
foreach($urls as $url){
    $arrUrl[] = $url->textContent;
}

$arrDescription = array();
foreach($descriptions as $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 INTO urlgrab(`title`, `url`, `description`) Values ('$title', '$url', '$description')");
}

*Remove $i++; in loop and then run. Actually we're doing $i++ in for loop * And that will solve your issue.

Upvotes: 0

Related Questions