freakunleash Posted March 7, 2012 Share Posted March 7, 2012 Hi All, not able to understand I'm making miskate in CSS or PHP foreach statement. Need help with proper formating of the below code. I want to format the so that seasons divs will contain all the details about the episodes, title & plot. As of now season div is only wraping around the first episode only & ignoring remaining episodes details. Sorry I'm not able to explain this in words but if you access below code you will be able to understand. <html> <head> <style type="text/css"> .main { width: 750px; margin-right: auto; margin-left: auto; background: #cc1; clear: both; overflow:auto; } .season { margin: 5px; padding: 5px; width: 730px; background: #960; } .details { overflow:hidden; } .ep, .title, .airdate, .plot { margin: 5px; padding: 5px; width: 675px; background: #CCC; } </style> </head> <body> <?php $eps = array( array( 'year' => '2005', 'eptitle' => 'title1', 'airdate' => 'Apr 24 2009', 'epplot' => 'plot of S01E01', 'season' => '1', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title2', 'airdate' => 'May 1 2009', 'epplot' => 'plot of S01E02', 'season' => '1', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title3', 'airdate' => 'May 8 2009', 'epplot' => 'plot of S02E01', 'season' => '2', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title4', 'airdate' => 'May 12 2009', 'epplot' => 'plot of S02E02', 'season' => '2', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title5', 'airdate' => 'May 15 2009', 'epplot' => 'plot of S03E01', 'season' => '3', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title6', 'airdate' => 'May 28 2009', 'epplot' => 'plot of S03E02', 'season' => '3', 'episode' => '2'), ); //var_dump($eps); $season = 0; echo "<div class='main'>"; foreach($eps as $k){ // season check start if($season!=$k['season']) { echo "<div class='season'><b>SEASON : </b>".$k['season']; } //season check finish echo "<div class='details'>"; echo "<div class='ep'><b>EPISODE : </b>".$k['episode']."</div>"; echo "<div class='title'><b>TITLE : </b>".$k['eptitle']."</div>"; echo "<div class='airdate'><b>AIRDATE : </b>".$k['airdate']."</div>"; echo "<div class='plot'><b>PLOT : </b>".$k['epplot']."</div>"; echo "</div>"; // season check start if($season!=$k['season']) { echo "</div>"; $season = $k['season']; } } echo "</div>"; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/258460-php-array-html-formating/ Share on other sites More sharing options...
dragon_sa Posted March 7, 2012 Share Posted March 7, 2012 looking at this link, if you look at the page source I have formatted the code so its a bit easier to read, but it looks like each season is being wrapped by the season div tag to me. http://playground.saint.com.au/test/test4.php Link to comment https://forums.phpfreaks.com/topic/258460-php-array-html-formating/#findComment-1324845 Share on other sites More sharing options...
AyKay47 Posted March 7, 2012 Share Posted March 7, 2012 the problem is this line: $season = $k['season']; so if the season if 1, at the end of the first iteration of the for loop, $season will be set to 1, and the next iteration of season 1 episode 2, this line will not pass: if($season!=$k['season']) { so your credentials will not be wrapped in the season div. I'm not sure of the logic for this, but it needs changed. Link to comment https://forums.phpfreaks.com/topic/258460-php-array-html-formating/#findComment-1324848 Share on other sites More sharing options...
dragon_sa Posted March 7, 2012 Share Posted March 7, 2012 its just a matter of placement and a small adjustment , but I think this is what you want you can see it here http://playground.saint.com.au/test/test4.php <html> <head> <style type="text/css"> .main { width: 750px; margin-right: auto; margin-left: auto; background: #cc1; clear: both; overflow:auto; } .season { margin: 5px; padding: 5px; width: 730px; background: #960; } .details { overflow:hidden; } .ep, .title, .airdate, .plot { margin: 5px; padding: 5px; width: 675px; background: #CCC; } </style> </head> <body> <?php $eps = array( array( 'year' => '2005', 'eptitle' => 'title1', 'airdate' => 'Apr 24 2009', 'epplot' => 'plot of S01E01', 'season' => '1', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title2', 'airdate' => 'May 1 2009', 'epplot' => 'plot of S01E02', 'season' => '1', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title3', 'airdate' => 'May 8 2009', 'epplot' => 'plot of S02E01', 'season' => '2', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title4', 'airdate' => 'May 12 2009', 'epplot' => 'plot of S02E02', 'season' => '2', 'episode' => '2'), array( 'year' => '2005', 'eptitle' => 'title5', 'airdate' => 'May 15 2009', 'epplot' => 'plot of S03E01', 'season' => '3', 'episode' => '1'), array( 'year' => '2005', 'eptitle' => 'title6', 'airdate' => 'May 28 2009', 'epplot' => 'plot of S03E02', 'season' => '3', 'episode' => '2'), ); //var_dump($eps); $season = 1; echo "<div class='main'>\n"; foreach($eps as $k){ // season check start if($season!=$k['season']) { echo "</div>\n"; $season++; } // season check start if($k['season']==$season && $k['episode']==1) { echo "<div class='season'><b>SEASON : </b>".$k['season']."\n"; } //season check finish echo "<div class='details'>\n"; echo "<div class='ep'><b>EPISODE : </b>".$k['episode']."</div>\n"; echo "<div class='title'><b>TITLE : </b>".$k['eptitle']."</div>\n"; echo "<div class='airdate'><b>AIRDATE : </b>".$k['airdate']."</div>\n"; echo "<div class='plot'><b>PLOT : </b>".$k['epplot']."</div>\n"; echo "</div>\n"; } echo "</div>"; ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/258460-php-array-html-formating/#findComment-1324858 Share on other sites More sharing options...
freakunleash Posted March 7, 2012 Author Share Posted March 7, 2012 thanks dragon...this is what I was looking for..... I will go through code now & see what I was missing..... Link to comment https://forums.phpfreaks.com/topic/258460-php-array-html-formating/#findComment-1324868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.