简体   繁体   English

PHP数组格式化HTML输出

[英]PHP array formatted HTML output

I have an array $eps coming from MYSQL table and I want to output the content of it into below html format. 我有一个数组$eps来自MYSQL表,我想将其内容输出为以下html格式。 I tried to use foreach loop to output the contents but value of season is wrapping over all the details div. 我尝试使用foreach循环输出内容,但是season的值覆盖了所有details div。 Below code is what I can only think of 下面的代码是我唯一想到的

foreach($eps as $key){
    foreach($key as $k){
    echo "<div class='main'>";
        echo "<div class='season'>".$k['season'];
            echo "<div class='details'>";
                echo "<div class='ep'>".$k['episode']."</div>";
                echo "<div class='title'>".$k['title']."</div>";
                echo "<div class='airdate'>".$k['airdate']."</div>";
                echo "<div class='plot'>".$k['plot']."</div>";
            echo "</div>";
        echo "</div>";
    echo "</div>";
    }
}

Array contents 数组内容

Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [title] => Pilot: Part 1
                    [date] => Sep. 22, 2004
                    [plot] => Forty-eight survivors of an airline flight originating from Australia, bound for the 
                    [season] => 1
                    [episode] => 1
                )

            [1] => Array
                (
                    [title] => Pilot: Part 2
                    [date] => Sep. 29, 2004
                    [plot] => Jack, Kate and Charlie return to the group, but the transceiver is broken. Sayid Jarrah 
                    [season] => 1
                    [episode] => 2
                )
    [2] => Array
        (
            [0] => Array
                (
                    [title] => Man of Science, Man of Faith
                    [date] => Sep. 21, 2005
                    [plot] => One of the castaways is chosen to descend into the mysterious hatch, and Shannon stumbles 
                    [season] => 2
                    [episode] => 1
                )

            [1] => Array
                (
                    [title] => Adrift
                    [date] => Sep. 28, 2005
                    [plot] => After the raft explodes, Michael and Sawyer are left on what is left on the raft. They 
                    [season] => 2
                    [episode] => 2
                )

CSS 的CSS

.main {
    width: 750px;
    margin-right: auto;
    margin-left: auto;
    clear: both;
    overflow:auto;
}
.season {
    margin: 5px;
    padding: 5px;
    width: 730px;
    background: #960;
    clear: both;
    overflow:auto;
    display: block;
}
.details {
    display: block;
    float: right;
    border-bottom: thin solid #000;
}
.ep, .title, .airdate, .plot {
    margin: 5px;
    padding: 5px;
    width: 675px;
    background: #CCC;
}

Only 1 occurance of an ID per document.... You know that, righ? 每个文档仅出现1个ID。...您知道吗? Made them classes. 使他们上课。

//only 1 main per total result, so, outside foreach.
echo "<div class='main'>";
foreach($eps as $key){
    //one season per sub array, so outside the other foreach:
    echo "<div class='season'>".$key[0]['season'];
    foreach($key as $k){
            echo "<div class='details'>";
                echo "<div class='ep'>".$k['episode']."</div>";
                echo "<div class='title'>".$k['title']."</div>";
                echo "<div class='airdate'>".$k['airdate']."</div>";
                echo "<div class='plot'>".$k['plot']."</div>";
            echo "</div>";
    }
    echo "</div>";
}
echo "</div>";
$season = 0;
echo "<div id='main'>";
foreach($eps as $key){
    foreach($key as $k){

       // season check start
       if($season!=$k['season']) {
        echo "<div id='season'>".$k['season']; 
       }
       //season check finish
            echo "<div id='details'>";
                echo "<div id='ep'>".$k['episode']."</div>";
                echo "<div id='title'>".$k['title']."</div>";
                echo "<div id='airdate'>".$k['airdate']."</div>";
                echo "<div id='plot'>".$k['plot']."</div>";
            echo "</div>";
       // season check start
       if($season!=$k['season']) {
        echo "</div>";
        $season = $k['season'];
       }
    }
}
echo "</div>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM