0

Is it possible to limit this loop ..just to four loops?

I got this source on another post and it's working but i just need to limit the loop

        $scraper = new DOMScraper();
       $scraper->setSite('http://anywebsite.com/testig12.aspx')->setSource();
        Class DOMScraper extends DOMDocument{
        public $site;
        private $source;
        private $dom;

    function __construct(){
    libxml_use_internal_errors(true);
    $this->preserveWhiteSpace = false;
    $this->strictErrorChecking = false;
}

function setSite($site){
    $this->site = $site;
    return $this;
}

 function setSource(){
     if(empty($this->site))return 'Error: Missing $this->site, 
     use  setSite() first';
     $this->source = $this->get_data($this->site);
     return $this;
 }

 function getInnerHTML($tag, $id=null, $nodeValue = false){
    if(empty($this->site))return 'Error: Missing $this->source, 
    use setSource() first';
    $this->loadHTML($this->source);
    $tmp = $this->getElementsByTagName($tag);
    $ret = null;
    foreach ($tmp as $v){ 
        if($id !== null){
            $attr = explode('=',$id);
            if($v->getAttribute($attr[0])==$attr[1]){
                if($nodeValue == true){
                    $ret .= trim($v->nodeValue);
                }else{
                    $ret .= $this->innerHTML($v);
                }
            }
        }else{
            if($nodeValue == true){
                $ret .= trim($v->nodeValue);
            }else{
                $ret .= $this->innerHTML($v);
            }
        }
    }
    return $ret;
 }

 function innerHTML($dom){
    $ret = "";
    $nodes = $dom->childNodes;
    foreach($nodes as $v){
        $tmp = new DOMDocument();
        $tmp->appendChild($tmp->importNode($v, true));
        $ret .= trim($tmp->saveHTML());
    }
    return $ret;
 }

  function get_data($url){
    if(function_exists('curl_init')){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }else{
        return file_get_contents($url);
    }
}
  }
  ?>

then here is where i want to limit the display becouse not it scrap all the data from that site like 2000 records.. i want to load only latest 4

 <?php echo '<div>'.$scraper->getInnerHTML('div','id=LeftDiv12').'</div>'; ?>

Thank you

Ray
  • 13
  • 1
  • 2
  • 1
    You should use a for loop instead of a foreach loop ;). – Unex Apr 30 '16 at 19:37
  • Could you explain me better please ? Thank you for your replay – Ray Apr 30 '16 at 19:39
  • ^ it's not every time suitable. make varialbe increased every loop and break when it reach needed value – splash58 Apr 30 '16 at 19:41
  • You can check here how this works : http://www.w3schools.com/php/php_looping_for.asp. You will also learn how foreach loops are made. – Unex Apr 30 '16 at 19:41
  • 1
    You can use a counter variable and check its value inside the loop (at its begin), if it is equals 4, break the loop, elsewise increase it by one. – SaidbakR Apr 30 '16 at 19:43
  • This answer maybe can help you https://stackoverflow.com/a/1656975/6883282 – Ray Coder Oct 24 '20 at 08:35

3 Answers3

4

Just use a dummy count variable like this:

$Count = 0;
foreach($Array as $Key => $Value){
    //your code

    $Count++;
    if ($Count == 4){
        break; //stop foreach loop after 4th loop
    }
}
Ghulam Ali
  • 1,935
  • 14
  • 15
0

One method you can use to limit a foreach loop is to create a variable outside of the foreach loop, and add one to the variable each time to loop is ran. Then break out of the loop after a certain amount of times. I've modified your function to do what I described above.

function getInnerHTML($tag, $id=null, $nodeValue = false){
    if(empty($this->site))return 'Error: Missing $this->source, 
    use setSource() first';
    $this->loadHTML($this->source);
    $tmp = $this->getElementsByTagName($tag);
    $ret = null;
    $i = 0;
    foreach ($tmp as $v){ 
        if($id !== null){
            $attr = explode('=',$id);
            if($v->getAttribute($attr[0])==$attr[1]){
                if($nodeValue == true){
                    $ret .= trim($v->nodeValue);
                }else{
                    $ret .= $this->innerHTML($v);
                }
            }
        }else{
            if($nodeValue == true){
                $ret .= trim($v->nodeValue);
            }else{
                $ret .= $this->innerHTML($v);
            }
        }
        if(++$i > 4) break;
    }
    return $ret;
}
Flibio
  • 149
  • 12
  • Thanks for your help man , however i don't know why it shows blank page after i edit with your code – Ray Apr 30 '16 at 19:57
  • @Ray That means there were four divs that had no content in them, try setting it to 100 and see if anything shows up. Are you using a URL that has content on it? – Flibio Apr 30 '16 at 20:45
0

Not sure which loop you are referring to, but you can create a counter outside your loop and then increment within the loop. Include an if statement to break on counter >=4

$counter = 0;
foreach($nodes as $v){
if($counter>=4){
    break;
}   
$tmp = new DOMDocument();
$tmp->appendChild($tmp->importNode($v, true));
$ret .= trim($tmp->saveHTML());
$counter++;}

Maybe a little cludgey, but it will get the job done.

dneimeier
  • 89
  • 2
  • 12