Wilson
Wilson

Reputation: 137

How to count number of elements of attribute in simple html dom

I have this codes it is suppose to fetch data and arrange in form off HOME | DRAW | AWAY, when you look at the codes you will find that the count variable is assigned a static variable it will bring problem when number of values that are fetched will be greater than the assigned variable, So the help that i need is how would i determine or what function will i use to count number of elements of this attribute

'div[data-gamename="1X2"' 

As it seen in the code below, how to count them?

<?php
include('advanced_html_dom.php'); 
$html = file_get_html('your-url-here');

 //Here is where should be a counter to be assigned to $count variable
$count = 450;
for ($i=0; $i<$count; $i+=3){
$dot = $html->find('div[data-gamename="1X2"]',$i);
echo " Home ".$dot."<br>";

 }

//This codes will be re-written to look same like the above
//I left for reference so you can get picture what i mean
$dot2 = $html->find('div[data-gamename="1X2"]',1);
$dot3 = $html->find('div[data-gamename="1X2"]',2);

 echo "Draw ".$dot2."<br>";
  echo "Away ".$dot3."<br>";

 $html->clear();
 ?>

Any idea how to implement this?

Upvotes: 1

Views: 1307

Answers (1)

Wilson
Wilson

Reputation: 137

 $numcounter = count($dot2 = $html->find('div[data-gamename="1X2"]'));
 echo "Number of elements are ".$numcounter." ------";

This is the answer for future reference. The $numcounter variable should be assigned to $count variable and from above codes it should be like this $count = $numcounter;

Upvotes: 1

Related Questions