在我的数据库中,我在此表中有一个名为 publi 的表格,我有两列 pub_year 和 pub_publi
表 publi 内容的示例:
<table border="1">
<tr>
<td>2016</td>
<td>content_2016_1</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_2</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_3</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_1</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_2</td>
</tr>
</table>
我想要这个输出
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
content_2016_2 <br>
content_2016_3 <br>
</div>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
content_2015_2 <br>
</div>
这是我的代码:
<?php
$query = "SELECT * FROM publi where pub_type=0 order by pub_year DESC, pub_publi ";
$result = mysqli_query($connection, $query);
$previous =0;
while ($val = mysqli_fetch_array($result))
{
if ($previous <> $val['pub_year'])
{
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#';
echo $year;
echo '">';
echo $year;
echo '</button>';
echo '<div id="';
echo $year;
echo '" class="collapse">';
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo '<a target=blank href="http://www.test.com/query.f?term=' . $val['pub_pubmed'] . '";)><img border="0" src="img/test.gif" align=MIDDLE alt="Search in for ' . $val['pub_publi'] . '"></a>';
echo $Temp;
echo '</div>';
}
}
?>
但我得到的结果是:
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
</div>
content_2016_2
content_2016_3
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
</div>
content_2015_2 <br>
答案 0 :(得分:0)
您通过pub_year对数据进行分组的方式是正确的,但是从您的示例中,您只是在组更改时输出信息。
我会提出类似的建议:
<?php
$previous = false;
while ($val = mysqli_fetch_array($result)) {
// when group is changing, end previous and start new
if ($previous <> $val['pub_year']) {
// end previous group html markup
if ($previous !== false) {
echo '</div>';
}
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#' . $year . '>' . $year . '</button>';
echo '<div id="' . $year . '" class="collapse">';
}
// always output data inside group
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo $Temp . '<br>';
}
// end last group html markup
if ($previous !== false) {
echo '</div>';
}
?>