0

I'm trying to parse DOM with Simple HTML DOM Parser in PHP. Parsed content are movies, so I want to get every genre there is, but when i run my code I only get the last genre, not all. My code looks like this:

if ($obj) {
    foreach($obj as $key => $data) {
        $item['url'] = 'http://geo.saitebi.ge/movie/' . $page;
        $item['poster'] = 'http://geo.saitebi.ge/web/ka/img/movies/' . $page . '/240x340/' . $page . '.jpg';
        $item['geotitle'] = $data->find('div.movie-item-title', 0)->plaintext;
        $item['englishtitle'] = $data->find('div.movie-item-title-en', 0)->plaintext;
        $item['year'] = $data->find('div.movie-item-year', 0)->plaintext;
        foreach($data->find('a.movie-genre-item') as $genre) {
            $item['genres'] = $genre->plaintext . ', ';
        }
        $item['description'] = $data->find('div.movie-desctiption-more', 0)->plaintext;
        $item['imdb_rating'] = $data->find('a.imdb_vote', 0)->plaintext;
        $item['imdb_id'] = trim(substr($data->find('a.imdb_vote',0)->href, strrpos($data->find('a.imdb_vote',0)->href, '/') + 1));
    }
}

As you see I'm getting content as array. Then inside it I run another foreach loop to get all genre items but it only gets last genre item. What is wrong in my code?

3 Answers 3

1

You are just overwriting the last set of data each time. You need to set it to blank and then append it using .= each time, like...

$item['genres'] = '';
foreach($data->find('a.movie-genre-item') as $genre) {
   $item['genres'] .= $genre->plaintext . ', ';
}
Sign up to request clarification or add additional context in comments.

Comments

0

The code are saving the same '$genre->plaintext' in $item with key 'genres' so the same key replace the values every loop.

 foreach($data->find('a.movie-genre-item') as $genre) {
            $item['genres'] = $genre->plaintext . ', ';
 }

May $item['genres] can be an associative array.. i mean:

foreach($data->find('a.movie-genre-item') as $genre) {
             if ( !isset($item[$genre]) ) {
                      $item[$genre] = array();
              }
          array_push($item[$genre],$genre->plaintext);
     }

1 Comment

No need to do this. I was just overwriting the last set of data each time, so I needed to set it to blank and then append it using .= each time. Above answer solved the problem :) Thank you!
0

Here is genre code which you have to update

    // Here is you genre code 
    $movie_genre='';
    foreach($data->find('a.movie-genre-item') as $genre) {
        $movie_genre .= $genre->plaintext . ',';
    }

    // Here you can use rtrim for removing last comma from genre
    $item['genres'] = rtrim($movie_genre,',');

1 Comment

Thank you a lot for providing fix for removing last comma from genre. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.