Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Limit a while loop

Guest
Feb 10, 2010 Feb 10, 2010

Is there a way to write this where it will limit the number of times that it runs - say 20 times?

while($p_link = array_search("0", $links))

   {        

      $links[$p_link] = "1";

// do this

}

TOPICS
Server side applications
699
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 11, 2010 Feb 11, 2010

It uses the ternary operator.

Translate
LEGEND ,
Feb 10, 2010 Feb 10, 2010

Coming from a non php coder...most languages allow you to include more than one condition in a while loop. So initialize a counter variable that you increment within the loop. And (not sure of the AND operator in php) it with the other condition. Something like:

$counter=0

while($p_link = array_search("0", $links) && $counter <20)

   {        

      $links[$p_link] = "1";

      $counter++

// do this

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2010 Feb 11, 2010

We'll make a PHP coder of you yet.

The only thing that's wrong is the semicolon missing from the end of each statement:

$counter=0;

while($p_link = array_search("0", $links) && $counter <20)

{        $links[$p_link] = "1";

  $counter++;

// do this

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 11, 2010 Feb 11, 2010

Thank you, I just could not figure this out as I wanted to add an entirely new while loop.  However, this still is not giving me the results I want.

What I have is code that will get all the internal links from a url, parse through those links for certain content (set by the preg_match_all($search, $contents, $search_results)), then add that content to an array ($gold).

However, I want to limit the number of links that it parse through to say 20 links.

Using the information your provider earlier ($counter) – it skips the parsing through the links and just seems to skip over the get content and following code.

I have tried putting the counter in other places or even using additional while loops but have gotten nowhere.  Can you tell me where I should be trying to limit the number of links to parse?

Code is attached

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2010 Feb 11, 2010

I don't have time to go through nearly 300 lines of code. However, since you want to limit $gold to 20 items, it seems as though you could alter the following section of code:

$num_results = count($search_results[2]);
       
// split each matched phrase into words and add to the array - David Powers
for ($i = 0; $i < $num_results; $i++)
{
    $gold = array_merge($gold, explode(' ', $search_results[2][$i]));
}

Change it to this:

$num_results = count($search_results[2]);

// limit the number of results to a maximum of 20
$num_results = ($num_results > 20) ? 20 : $num_results;
       
// split each matched phrase into words and add to the array - David Powers
for ($i = 0; $i < $num_results; $i++)
{
    $gold = array_merge($gold, explode(' ', $search_results[2][$i]));
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 11, 2010 Feb 11, 2010

If you do get some time, can you explain this:

num_results = ($num_results > 20) ? 20 : $num_results;

Not sure I understand the ? 20 : $num_results; part.

Thanks

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 11, 2010 Feb 11, 2010

It uses the ternary operator.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 11, 2010 Feb 11, 2010
LATEST

Thanks - that makes sense.  It is still causing me problems.  But, I am trying something else - we will see if this works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines