0

Hi there I'm new here.

Trying to make this little code to loop over pages. And scrape off links of headings .

Scraping part works just fine but i cant make it to loop to the next page. It keeps looping on the same page.

<?php
include('../simple_html_dom.php');

// start at page 1
$xder = 1;
do {

// web page + page number (should change with every loop)

$html = file_get_html('https://webpage.com/stuff/page/$xder');

foreach($html->find('h3') as $h3) 
{
       foreach($h3->find('a') as $element) 
       {
            echo $element->href . '<br>';
      }
}

    $xder++;

} while ($xder <= 5);
?>

I'm expecting to get list of links from all 5 pages, but I only get list of links from 1st page repeating 5 times.

I think the problem is here "/stuff/page/$xder');" I'm not sure how to add a variable to the back of an URL it doesn't appear to work.

Tried methods here:

Converting an integer to a string in PHP

Getting frustrated with this. Not sure what I'm missing here. Thanks for any thoughts :)

Aurimas
  • 3
  • 1

1 Answers1

0

Php variables are treated as variables only if you use ", and not '

Change

$html = file_get_html('https://webpage.com/stuff/page/$xder');

to

$html = file_get_html("https://webpage.com/stuff/page/{$xder}");
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Eriks Klotins
  • 4,042
  • 1
  • 12
  • 26