1

I am doing some operation in URL as below.

 $paginationPages=array();
 $productCount=intval($htmlProductPage->find('div.paging span.itemcount',0)->plaintext);
 if($productCount/16>1){
    $pagecount=ceil($productCount/16);

    for($i=2;$i<=$pagecount;$i++){
        $urlSplitArray=explode('.',$productUrl);
        $urlSplitCount=count($urlSplitArray);
        $urlSplitArray[$urlSplitCount-2].="[".$i."]";
        $paginationPages[]= implode('.',$urlSplitArray)."<br>";
    }
 #print_r($paginationPages);

}

and I am getting all the links in foreach to proceed further

foreach($paginationPages as $nextUrl){
  #$nextUrl="http://www.100percent.co.nz/kitchen-and-cooking/ovens/freestanding[2].htm";
  $htmlProductPage=file_get_html($nextUrl);
  foreach($htmlProductPage->find("div.Item") as $element){ //error occurs here
    echo $element->outertext;
  }
}

The variable $nextUrl has the value

"http://www.100percent.co.nz/kitchen-and-cooking/ovens/freestanding[2].htm"

I cant find the element div.Item when I pass the link dynamically. But when I directly assign the url inside the for loop I could able to find the same element. Why this is happening?

5
  • Where is the error occurring -- is your code successfully getting the page using file_get_html? Oct 10, 2014 at 8:40
  • @ialarmedalien please check my updated question. I have mentioned where the error occurs
    – dharanbro
    Oct 10, 2014 at 8:43
  • The link is commentted inside the for each loop
    – dharanbro
    Oct 10, 2014 at 8:46
  • An example URL for this bit of code: $urlSplitArray=explode('.',$productUrl); - what is $productUrl? Oct 10, 2014 at 8:54
  • 100percent.co.nz/kitchen-and-cooking/ovens/freestanding.htm since it has pagination i need to add '[2]' before the extension
    – dharanbro
    Oct 10, 2014 at 8:57

1 Answer 1

1

The problem is occurring due to this statement:

$paginationPages[]= implode('.',$urlSplitArray)."<br>";

print_r of $paginationPages:

Array
(
    [0] => http://www.100percent.co.nz/kitchen-and-cooking/ovens/freestanding[2].htm<br>
)

You are tagging a <br> element on to the end of the page name, which then means that the page cannot be retrieved with this code:

foreach($paginationPages as $nextUrl){
    $htmlProductPage=file_get_html($nextUrl);

I suggest adding a check that file_get_html has successfully retrieved a page before parsing the page to scrape the contents.

1
  • No probs. Sometimes it takes another person to track down a typo like this! Oct 10, 2014 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.