Only placing first result into database.

+2 Gavin Lee · December 23, 2015
Currently I am using a tool called simple_html_dom.php to pull the contents from a web page, the pulling process works perfectly with the code displayed below however when I go to put the content inside of my database it only shows the last result which is on the page within my database. Demo: http://imgclacks.com/scrape/test.php    
    

$html = file_get_html('http://www.rapbasement.com/rap-news/'.$page);
foreach($html->find('#main div h3 a') as $element)
{
print '<br><br>';
echo $url = ''.$element->href;
$html2 = file_get_html($url);
print '<br><br>';

$image = $html2->find('meta[property=og:image]',0);
print $image = $image->content;
print '<br><br>';
// Ending The Featured Image
$title = $html2->find(' header > h1',0);
print $title = $title->plaintext;

print '<br>';
// Ending the titles
print '<br>';
$article = $html2->find('section.entry-content > p',0);
print $article = $article->plaintext;
print '<br><br>';
#post> div:nth-child(2) > header > p > time
$date = $html2->find('header > p > time',0);
$date = $date->plaintext;

$dexp = explode(', ',$date);

print $date = $dexp[0].', '.$dexp[1];

print '<br><br>';

$genre = "news";
print '<br>';

$sql = "INSERT INTO wp_scraped_news SET hash = '".$title."', title = '".$title."', image = '".$image."', content = '".$article."'";
}



The second one is the article that it submitted, the first one is something I had just for testing, but it looks like it will also duplicate when you load the page again, which is something else I don't want.. any help is very welcome thanks. 
/images/forum/upload/2015-12-23/5bb40ed5b2559e76bc898ca611b63dae.png

Post a Reply

Replies

Oldest  Newest  Rating
0 Darren Reeder · January 4, 2016
Where is the mysql_query() function?

are you executing the query outside of the foreach loop? 
  • 1

PHP

129,896 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators