Thanks for reading. I'm trying to parse an array of urls from Amazon, gather 4 variables ($name, $price, $stars, $reviews) and store the value of each of these into MySQL for every iteration in the Foreach Loop
The problem that I'm having is that only the information from the first url is being stored in MySQL (as you can see from the array I have 4 sites I'm trying to test this on.
I know that the code to get the correct values of the variables and such is correct because again this works for the first site, just doesn't go through the rest and I can't figure out why.
Any help would be greatly appreciated. Thanks!
function getSQL()
{
include('simple_html_dom.php');
$urlArray = array("http://www.amazon.com/kindle-fire-hdx-student-gaming-tablet/dp/B00BWYQ9YE/ref=sr_1_1?ie=UTF8&qid=1403276865&sr=8-1&keywords=kindle+fire+hdx", "http://www.amazon.com/Kindle-Fire-HDX-Display-Wi-Fi/dp/B00CUTT4HY/ref=sr_1_2?ie=UTF8&qid=1403276882&sr=8-2&keywords=kindle+fire+hdx",
"http://www.amazon.com/Kindle-Fire-HDX-Display-Wi-Fi/dp/B00BWYRF7E/ref=sr_1_3?ie=UTF8&qid=1403276882&sr=8-3&keywords=kindle+fire+hdx", "http://www.amazon.com/kindle-fire-hdx-best-movie-tablet-8.9/dp/B00BHJRYYS/ref=sr_1_5?ie=UTF8&qid=1403276882&sr=8-5&keywords=kindle+fire+hdx");
foreach ($testArray as $url)
{
$html = file_get_html("$url");
$name = $html->find('h1[class="parseasinTitle"]', 0)->plaintext; //line 6455
$price = $html->find('b[class=priceLarge]', 0)->plaintext; //line 6650
$string = $html->plaintext;
$wordToFind = 'stars';
$numWordsToWrap = 4;
$words = preg_split('/\s+/', $string);
if (($pos = array_search($wordToFind, $words)) !== FALSE) {
$start = ($pos - $numWordsToWrap > 0) ? $pos - $numWordsToWrap : 0;
$length = (($pos + ($numWordsToWrap + 1) < count($words)) ? $pos + 1 : count($words) - 1) - $start;
$slice = array_slice($words, $start, $length);
$stars = implode(' ', $slice);
} else echo 'I didn\'t find it';
$wordToFind2 = 'reviews';
$numWordsToWrap2 = 4;
$words2 = preg_split('/\s+/', $string);
if (($pos2 = array_search($wordToFind2, $words2)) !== FALSE) {
$start2 = ($pos2 - $numWordsToWrap2 > 0) ? $pos2 + 1 : 0;
$length2 = (($pos2 + ($numWordsToWrap2 + 1) < count($words2)) ? $pos2 + ($numWordsToWrap2 + 1) : count($words2) - 1) - $start2;
$slice2 = array_slice($words2, $start2, $length2);
$reviews = implode(' ', $slice2);
$reviews = str_replace(" ", "", $reviews);
} else echo 'I didn\'t find it';
$amazon_all_sql = "
insert into kindlefire
values('$name', '$price', '$stars', '$reviews');
";
return $amazon_all_sql;
}
}
foreach ($urlArray as $url)?returnstatement is inside yourforeachloop, so the function will return during the first iteration and never process the remaining entries.foreachtoyieldand use it likeforeach(getSql() as $sql)