seergiue
seergiue

Reputation: 326

Display all the same elements Simple HTML DOM Parser

I have a problem, i'm parsing IMDB web page using Simple HTML DOM Parser, and my code is the next one:

<?php
require('../simple_html_dom.php');

$url = 'http://www.imdb.com/search/name?gender=female';
$html = file_get_html($url);

foreach ($html->find('table.results tbody') as $div) {
    $extractname = $div->find('tr.detailed td.name a', 0);
    $extractimg = $div->find('tr.detailed td.image', 0);
    $name = $extractname->innertext;
    $img = $extractimg->innertext;
    echo $img, $name;
};
?>

This script returns me that:

Link to image

Well so my problem is that i don't know why my script only returns me one element, and not all the elements.

Thanks!

Upvotes: 1

Views: 675

Answers (2)

PHP Addict
PHP Addict

Reputation: 136

I usually use XPATH to do things like this, so forgive me if I am wrong.

To me it looks like find() gets an array, you should be looping over the $extractname as an array of elements just as you are doing with the $html find for tbody tags, and the same with $extractimg.

So to me a) you find all the tbody tags and loop them b) inside each tbody you are looking for the other elements which become their own arrays.

Upvotes: 0

jmeinlschmidt
jmeinlschmidt

Reputation: 1476

You are getting one element because <tbody> is only one on that page. You probably want to get result for each tr row.

foreach ($html->find('table.results tbody tr') as $div) {}

Upvotes: 1

Related Questions