一年内收集所有行,而无需重复一年

时间:2019-06-23 17:44:56

标签: php mysql mysqli

我想要这个输出:

  

2018
  event1
  event2

     

2019
  event3
  event4

我的代码的输出:

  

2018
  event1
  2018
  event1
  2019
  event3
  2019
  event4

在没有功能GROUP_CONCAT()和重复年份的情况下如何实现?

我的代码:

while ($myrow = mysqli_fetch_array($result)) {
    $var = $myrow['event_date'];
    echo" 
    " . date("Y", strtotime($var)) . "
    " . date("d.m.Y", strtotime($var)) ." 
    "?> 
    <a class="link" href="article.php?event_id=<?php echo

    $myrow['event_id']; ?>"><code><?php echo $myrow['article_title']; ?></a> 
    <a class="link" href="speaker.php?speaker_id=<?php echo
    $myrow['speaker_id']; ?>"><?php
    echo" 
    ".$myrow['speaker_degree']." 
    ".$myrow['speaker_name']." 
    ".$myrow['speaker_surname']." 
    ".$myrow['speaker_patronymie']." 
    </a></b></p> 
    ";
}

我的查询:

SELECT * 
FROM event_t  
JOIN event_speaker_article_t ON event_t.event_id=event_speaker_article_t.event_id 
JOIN article_t ON event_speaker_article_t.article_id=article_t.article_id 
JOIN speaker_t ON event_speaker_article_t.speaker_id=speaker_t.speaker_id 
ORDER by event_date

1 个答案:

答案 0 :(得分:1)

您可以尝试将其放入数组:

<?php
    // Your query
    $sel = "SELECT * 
    FROM event_t  
    JOIN event_speaker_article_t ON event_t.event_id=event_speaker_article_t.event_id 
    JOIN article_t ON event_speaker_article_t.article_id=article_t.article_id 
    JOIN speaker_t ON event_speaker_article_t.speaker_id=speaker_t.speaker_id 
    ORDER by event_date";

    $result = mysqli_query($con,$sel)

    $arrData = array();
    $Count = 0;
    $OldYear = "";

    while($row = mysqli_fetch_array($result))
    {
        $FullDate = $myrow['event_date']; 
        $Year = date("Y", strtotime($FullDate));
    	
    	if($OldYear == "" || $OldYear != $Year)
    	{
    		$OldYear = $Year;
    		$Count = 0;
    	}
    	
    	$arrData[$Year][$Count]['event_id'] = $row['event_id'];
    	$arrData[$Year][$Count]['article_title'] = $row['article_title'];
    	$arrData[$Year][$Count]['speaker_id'] = $row['speaker_id'];
    	$arrData[$Year][$Count]['speaker_degree'] = $row['speaker_degree'];
    	$arrData[$Year][$Count]['speaker_name'] = $row['speaker_name'];
    	$arrData[$Year][$Count]['speaker_surname'] = $row['speaker_surname'];
    	$arrData[$Year][$Count]['speaker_patronymie'] = $row['speaker_patronymie'];
    	
    	$Count++;
    }
?>

HTML部分

 <?php
    foreach($arrData as $Year => $year)
    {
    ?>
    	<h4> <?php echo $Year . "<br />"; ?> </h4>

    	<?php
    	for($event = 0; $event < $arrData[$Year].length; $event++)
    	{
    	?>
    		<a class="link" href="article.php?event_id=<?php echo $arrData[$Year][$event]['event_id']; ?>"><?php echo $arrData[$Year][$event]['article_title']; ?></a> 
    		<a class="link" href="speaker.php?speaker_id=<?php echo $arrData[$Year][$event]['speaker_id']; ?>"><?php echo $arrData[$Year][$event]['speaker_degree' ] ."  ". $arrData[$Year][$event]['speaker_name'] ."  ". $arrData[$Year][$event]['speaker_surname'] ."  ". $arrData[$Year][$event]['speaker_patronymie']; ?></a>
    	<?php
    	}
    }
    ?>