Simple HTML DOM Parser - Problem displaying a variable inside foreach loop

Hello,

I have created a simple PHP script using the simple_html_dom.php class. I fetch some information about movies from a website. I have one foreach loop inside another foreach loop. When I try to display the moviename inside the foreach loop I get the last moviename. What I want to achieve is to get each one of the unique movienames in each item. The problem is with the $movie variable.

(When i echo the $movie var on line 27 i get the correct result but I want to have each moviename inside the youtube links on line 33…)

<?php


include("simple_html_dom.php");

$tpb = 'https://tpb.party/search/2020/1/99/200';

$html = file_get_html(html_entity_decode($tpb));

foreach($html->find('tr.header') as $header) {
$header->outertext = '';
}

foreach($html->find('td') as $bottom) {
if ($bottom->colspan == '9') {
$bottom->outertext = '';
}
}

foreach($html->find('td.vertTh') as $vert) {
$vert->outertext = '';
}   


foreach($html->find("div.detName") as $movie) {
$movie = $movie->plaintext;
echo $movie;    //Works Okey, it displays each of the movietitles

foreach($html->find('img') as $img) {

if ($img->outertext == '<img src="https://tpb.party/static/img/11x11p.png" height="11" width="11">') {

$img->outertext = '&nbsp;&nbsp;<a href="https://www.youtube.com/results?search_query='. $movie /* Doesn't work, only displays one title, not one each of the 30*/ .'" target="_blank"><img src="img/youtube.png" alt="Trailer" title="Trailer" style="width:19px;" width="19" height="18" border="0"></a>';
}
}
}   

$html->save();

foreach($html->find("table") as $title) {

echo $title->outertext . '<br>';

}

?>

FXV, did you solve this one? I noticed there was not answer given to you.
First, do not do this:

The problem is that you altered $movie changing it’s object to just the plaintext.
Simply use echo $movie->plaintext; which will not change the “movie” object.
And, lastly, do not use $html->save(); This is an invalid command. It is supposed to include the name of a file which to save the data to. Something like $html->save(“my_test_file.txt”);
I am not sure why you would have just a blank save command. Thinking it was a mistake.
Hope those fixes help you solve it. Also, you did not place any comments in your list to tell us which line is #27 or #33. Hard to follow what line you are talking about… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service