Question

I have done this so when i was using xml parser but it seems that it's not working when i use simple_html_dom to pull the data from the page.

Here's my code that i have now.

    $counter = 0;
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($curl, CURLOPT_URL, 'the url of the website');  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);  
    $str = curl_exec($curl);  
    curl_close($curl);  

    $feedURL = str_get_html($str); 

            foreach($feedURL->find('li[class="videoblock"]') as $video) {
                $get_title = $video->find('span[class="title"]', 0);
                $title = $get_title->plaintext;
                echo $title . '<br/>';
            if ($counter+1 >= 3) break;
            }

    $feedURL->clear(); 
    unset($feedURL);

So it's one of the video sites, which have many videos wrapped in class="videoblock" so i set to find li element with videoblock class and loop to pull data foreach of them.

But as there can be like 20 videos(li elements with videoblock class) i want to limit loop for only 3 and stop after that.

So shouldn't

if ($counter+1 >= 3) break;

stop it once it get's to number 3. Or is it different when pulling data with simple_html_dom? Using it in function where i get data with simplexml_load_file it works.

Was it helpful?

Solution

You are not actually increasing your $counter variable, so it will always stay 0.

If you want to increase it, use something like:

if ($counter++ >= 3) break;

Although in your specific case you probably need (see the manual):

if (++$counter >= 3) break;    // first increment, return value and then compare 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top