dimanche 25 octobre 2015

Check for valid rows in Simple DOM Element

I am using the Simple HTML DOM Parser and in my test script it worked perfectly. I copied it over to my Seeder class in Laravel though and it started throwing errors at me. Upon looking into this, I realized that the first row and last 3 rows it pulls are invalid, where previously I had just been ignoring these.

$stats[0] returns a valid number on the rows I actually want, however the first row is just header text and $stats[0] is empty. Within my Laravel Seeder class, if I try and check the contents of $stats[0], it throws an "Undefined Offset: 0" error.

How can I check the contents of the first TD element in each row to make sure that it is valid?

$url = 'http://ift.tt/1Kts96c';
$html = file_get_html($url);

$tables = $html->find('table');

$table_defense = $tables[0];
$table_passing = $tables[1];
$table_rushing = $tables[2];

$teams = array(array());

foreach ($table_defense->find('tr') as $row)
{
    $stats = $row->find('td');

    if (empty($stats[0]) || $stats[0] == null)
    {
        continue;
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire