phoenixx Posted September 24, 2008 Share Posted September 24, 2008 Thought it would be simple but ... here's what I'm doing I'm scraping an external page and would like it to go to through the pagination setup on the site I'm scraping which sets a new page for every 100 listings. I have a loop counter that increases by 1 for every loop but here's what I need it to do. If the loop equals 100 or 200 or 300 or 400 or so on and so on, then one of the variables needs to have a value of 100 or 200 or 300, etc... Also I need the loop to stop if it finds no data without dying. Kind of a long code but there is only 3 places where I need help and I've highlighted them with // HELP NEEDED HERE code. Oh, I show loops through 1000, but should just find that it is at end of sites paginated data <? // Let's Extract Page Links $loopcount=(1); WHILE ($loopcount <10001){ $data = @file_get_contents("http://xxxxx.com/$urldirectory/$looppage"); // HELP NEEDED HERE (if EOF then change $urldirectory) preg_match_all('/<p>.*?<a href="([^"]*)".*?<p>.*?href="([^"]*)"/is',$data,$out); $d = array_combine($out[1], $out[2]); foreach($d as $k=>$v){ $loopcount++; echo $k . "<br>"; if ($loopcount == (100)){ $looppage=("index100.html"); // HELP NEEDED HERE should just be simple lines of code instead of multiples below, right? } if ($loopcount == (200)){ $looppage=("index200.html"); } if ($loopcount == (300)){ $looppage=("index300.html"); } if ($loopcount == (400)){ $looppage=("index400.html"); } if ($loopcount == (500)){ $looppage=("index500.html"); } if ($loopcount == (600)){ $looppage=("index600.html"); } if ($loopcount == (700)){ $looppage=("index700.html"); } if ($loopcount == (800)){ $looppage=("index800.html"); } if ($loopcount == (900)){ $looppage=("index900.html"); } if ($loopcount == (1000)){ $looppage=("index1000.html"); } // HELP NEEDED HERE if the scraper page has reached end of pagination then change $urldirectory $loopcount++; echo $v . "<br>"; if ($loopcount == (100)){ $looppage=("index100.html"); } if ($loopcount == (200)){ $looppage=("index200.html"); } if ($loopcount == (300)){ $looppage=("index300.html"); } if ($loopcount == (400)){ $looppage=("index400.html"); } if ($loopcount == (500)){ $looppage=("index500.html"); } if ($loopcount == (600)){ $looppage=("index600.html"); } if ($loopcount == (700)){ $looppage=("index700.html"); } if ($loopcount == (800)){ $looppage=("index800.html"); } if ($loopcount == (900)){ $looppage=("index900.html"); } if ($loopcount == (1000)){ $looppage=("index1000.html"); // HELP NEEDED HERE if the scraper page has reached end of pagination then change $urldirectory } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125624-loop-while-the-condition-has-data/ Share on other sites More sharing options...
CroNiX Posted September 24, 2008 Share Posted September 24, 2008 I suppose you could use the modulus operator to replace the many IF checks. if($loopcount % 100 == 0) $looppage = "index" . $loopcount . ".html"; Or something close to that Quote Link to comment https://forums.phpfreaks.com/topic/125624-loop-while-the-condition-has-data/#findComment-649872 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.