Jump to content

PHP array HTML formating


freakunleash

Recommended Posts

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

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.

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.