0

Hello I'm creating a cinema blog using wordpress. I created tv episodes using serialized data with custom text boxes. My problem here is that when ever I unserialize the data I want it to display seasons, episode number, and plot. However since multiple episodes will have the same seasons, I didn't want duplicate season numbers to display multiple times. I use an if statement to get this done; however once in a while I get duplicate seasons as if it is 2 different numbers when it's exactly the same. The make sure it wasn't human error with white space or anything, I input the season numbers using a list box with numbers. Unfortunately I still have that problem.

foreach ($data as $item):
 if ($item['season']  !== $old_season) {
       echo  '<ul class="panel"><h2 class="title">Season '. $item['season'].'</h2>'.     
             '<li><h2>'.stripslashes(base64_decode($item['title'])).'</h2>'; $old_season = $item['season'];} 
           echo         '<p>Episode'. $item['number']. 'Aired'.stripslashes(base64_decode($item['airdate'])).'</p>'
                   .'<p>'.stripslashes(base64_decode($item['plot'])).'</p></li>'; 
 endforeach

Any help will be much appreciated!

25
  • 2
    Do a var_dump($data) and post the results here. Commented Apr 19, 2012 at 4:36
  • 1
    @Craig I guess the only thing left is to var_dump in each iteration of the loop and examine what exactly the values are. In fact, what is the order of the $data coming in? Given your var_dump, season 18 headers would get printed twice, as a season 2 falls in the middle of all the season 18 episodes. Commented Apr 19, 2012 at 5:21
  • 1
    @RJHill You were right! It appears sorting it was the problem. It was sorting by episode number instead of season number. Still puzzled at how duplicate seasons were able to pass my if statement. Commented Apr 19, 2012 at 5:59
  • 1
    @Craig, because the way you had it set up, it would only work if all of the seasons were in order. It set the $old_season to the current season at the end so if we just looked at that variable you'd see a bunch of season 18 which wouldn't repeat, but then a 2 comes in and the $old_season is set to 2, so that when that next season 18 comes in it's not a match to season 2 and pops up again... Commented Apr 19, 2012 at 6:02
  • 1
    @Craig It was because $old_season was constantly getting overwritten in the if statement, wiping the fact that some season 18 episodes were previously printed. Commented Apr 19, 2012 at 6:05

1 Answer 1

2

All you have to do in this case is sort the $data array and the code you have there will work just fine.

if ($item['season']  !== $old_season) {
       echo  '<ul class="panel"><h2 class="title">Season '. $item['season'].'</h2>'.     
             '<li><h2>'.stripslashes(base64_decode($item['title'])).'</h2>';
       $old_season = $item['season'];
}

The problem is the code right here:

$old_season = $item['season'];

because it only works on sequential elements.

Sign up to request clarification or add additional context in comments.

Comments

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.