2012-01-18 87 views
0

我遇到一些問題,瞭解如何解決這個循環:遞歸或簡單的PHP循環

我開發我自己一個小刮板,我試圖在2種方法,直到所有弄清楚如何循環鏈接從網站中檢索。

我已經取回從第一頁的鏈接,但問題是,我不能讓一個循環來驗證已經提取的新鏈接:

這裏是我的代碼:

$scrap->fetchlinks($url);//I scrap the links from the first page from a website 

    //for each one found I insert the url in the DB with status = "n" 
    foreach ($scrap->results as $result) { 
     if ($result) { 
      echo "$result \n"; 
      $crawler->insertUrl($result); 

      //I select all the links with status = "n" to perform a scrap the stored links 
      $urlStatusNList = $crawler->selectUrlByStatus("n"); 

      while (sizeof($urlStatusNList > 1)){ 
       foreach($urlStatusNList as $sl){ 
       $scrap->fetchlinks($sl->url); // I suppose it would retrieve all the new sublinks 
       $crawler->insertUrl($sl->url); // insert the sublinks in the db 
       $crawler->updateUrlByIdStatus($sl->id, "s"); //update the link scraped with status = "s", so I will not check these links again 

       //here I would like to return the loop for each new link in the db with status='n' until the system can not retrieve more links and stops with the script execution 
       } 
      } 
     } 
    } 

任何類型的幫助是非常受歡迎的。提前致謝 !

回答

1

在僞代碼,您正在尋找這樣的事情

do 
{ 
    grab new links and add them to database 

} while(select all not yet extracted from database > 0) 

將繼續進行下去,並在沒有遞歸...

+0

嗨,感謝您的回答:你的意思呢? do {scrap-> fetchlinks($ url); foreach($ scrap-> results as $ result){ if($ result){ echo「$ result \ n」; $ crawler-> insertUrl($ result); } } } while($ crawler-> selectUrlByStatus(「n」)> 1); 謝謝! – rpa 2012-01-18 02:47:29

+0

@rpa是的,應該是這些行之間的東西...(並沒有真正檢查代碼是否有效)。 – Frankie 2012-01-18 03:03:14