简体   繁体   English

Php:While循环组输出

[英]Php: While loop group output

I have two columns in my database, year and content i want to group the content by year (i have a lot of content and many years not only 2015 and 2016 as the example ) 我的数据库中有两列,年份和内容,我想按年份对内容进行分组(我有很多内容,很多年份不仅是2015和2016,例如)

so i'll have this output in html 所以我将在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>'; } } ?> 

It is outputing the years right 输出正确的年份

2016 2016年

2015 2015年

2014 2014年

etc... 等等...

but it is only showing the first ligne of each year not all the content for each year. 但它仅显示每年的第一个ligne,而不显示每年的所有内容。

what u are asking is huge chunk to write. 您要问的是要写的大量内容。 so here are steps: 所以这是步骤:

  1. create array with this SQL statement below and fetch assoc in PHP 使用下面的此SQL语句创建数组并在PHP中获取assoc

    select distinct on year * from table 从表中选择年份*

  2. now use foreach($years as $year) on array and fetch content 现在在数组上使用foreach($ years作为$ year)并获取内容

    select content from table where year=$year 从表中选择内容,其中year = $ year

  3. to sandwich your code output in SQL keep concatenating like 将您的代码输出夹在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>"; 

how to fetch data in sql | 如何在sql中获取数据 PHP.NET MANUAL PHP.NET手册

how to join/concat in php | 如何在php中加入/ concat | PHP.NET MANUAL PHP.NET手册

LATER ADDITIONS, SNIPPET FROM MY OWN CODE : 以后的代码,摘录自我自己的代码:

$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/>";
}
  1. Run the following query: 运行以下查询:

     Select Content From table order by year 
  2. echo like following (or 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; } 

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

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