Jump to content

[SOLVED] Loops without writing them?


Michdd

Recommended Posts

I guess this question is somewhat about spidering.. I'm writing something that will grab certain things from a web page, it then uses that info to request another web page and get information from that. Repeating this process. I have a function with grabs the necessary stuff so I just do something like this:

 

foreach($something as $part)
{
     $sub = getInfo($part);
     ...
     foreach($sub as $subsub)
     {
     ...
     }
     ...
}

And I could keep repeating those loops inside each other to go down more and more. But is there a way to do that without actually writing the loops? Say I wanted to go down 10 levels, I don't want to write 10 loops.

Link to comment
https://forums.phpfreaks.com/topic/168081-solved-loops-without-writing-them/
Share on other sites

You would use recursive methods, so the spider would be a function, and if it finds a link it returns itself (the function) passing that link as a parameter.

 

EDIT:

 

Sample:

 

function sum_series ($sum, $i) {
$sum = $sum + $i;
if ($i>=10000) {
return $sum;
}else{
return sum_series($sum, ($i+1);
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.