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
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);
} 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.