gpt4 book ai didi

PHP 数组格式的 HTML 输出

转载 作者:行者123 更新时间:2023-11-28 15:55:39 25 4
gpt4 key购买 nike

我有一个来自 MYSQL 表的数组 $eps,我想将它的内容输出为以下 html 格式。我尝试使用 foreach 循环来输出内容,但季节的值(value)正在覆盖所有细节 div。下面的代码是我只能想到的

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
(
[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

.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;
}

最佳答案

每个文档只有 1 个 ID 出现......你知道,对吧?让他们上课。

//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>";

关于PHP 数组格式的 HTML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9184164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com