我的数据库中有两列,年份和内容我想按年份对内容进行分组(我有很多内容和很多年,不仅仅是2015年和2016年为例)
所以我会在html 中得到这个输出
<div class="all">
<div class="2016">
2016
<div class="content_2016">
All the content of the column that is in the same ligne as 2016 in the database
</div>
</div>
<div class="2015">
2015
<div class="content_2015">
All the content of the column that is in the same ligne as 2015 in the database
</div>
</div>
</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 $year;
echo '<br>';
$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 '<br>';
}
}
?>
它正在输出正确的年份
2016年
2015年
2014年
等等。。。
但它只显示了每年的第一行,而不是每年的所有内容。
-
运行以下查询:
Select Content From table order by year
-
类似回声的下列(或concat):
while rs.hasNext(){ if (currentYear != lastyear){ echo div #with year As Class; } echo row # a single entry the way you want it dispalyed; lastYear = currentYear; }
您所要求的是要编写的巨大数据块。以下是步骤:
-
使用下面的SQL语句创建数组,并在PHP 中获取assoc
从表中选择不同年份*
-
现在在数组上使用foreach($years作为$year)并获取内容
从表中选择内容,其中年份=$year
-
要将SQL中的代码输出夹在中间,请像一样保持连接
$html = ""; $html .= "<div class='"all'">"; foreach($years as $year){ $html .= "<div class='"content_$year'">"; //then content fetched from second sql query $html .= "</div>"; } $html .= "</div>";
如何在sql|PHP.NET MANUAL 中获取数据
如何在php|php.NET MANUAL 中加入/concat
稍后添加的内容,我自己的代码片段:
$letter = $_POST['letter'];
$query_out = "SELECT link_id,singer_url FROM letter_mapping WHERE letter = '$letter'";
if($result_out = $conn_out->query($query_out)){
if($result_out->num_rows > 0){
while($row = $result_out->fetch_assoc()) {
$link_id = $row['link_id'];
$singer_url = $row['singer_url'];
/*
foreach($row as $name=>$value){
}
*/
}
}
$result_out->free();
}
if($conn_out->close()){
// Message for disconnect
//echo "Status : DISCONNECTED<br/>";
}