2016-01-13 34 views
0

這裏有點難以解釋,但我會盡我所能包含所有相關信息。如果你不明白,你可以問我。從數據庫連接相同的數組(數字)

enter image description here

以上是我得到的數據,我希望向相同數量(例如2016)結合,如下所示進行顯示。

enter image description here

這是我的代碼。

<?php 
    $years = mysql_query("SELECT DISTINCT YEAR(event_date) as years, DATE_FORMAT(event_date,'%b') as months from news WHERE status<>'deleted' and status<>'draft' ORDER BY years DESC"); 

    while($page=mysql_fetch_array($years)) 
    { ?> 
     <div class="date"> 
      <div class="year"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $page['years'] ?>&month=<?php echo $page['months'] ?>"><?php echo $page['years']; ?></a></div> 

      <div class="month"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $page['years'] ?>&month=<?php echo $page['months'] ?>"><?php echo $page['months']; ?></a></div> 
     </div> 
<?php } ?> 

這是我的數據庫 enter image description here enter image description here

我想知道如何在今年結合?我使用「GROUP BY」但不能,它隱藏了第二行數據。或者我應該比較價值併合並它?但我不知道如何做到這一點。請幫忙,謝謝。

回答

1

試試這個編碼...

您是GET結果值,並在你的HTML內容適用。

 <?php 
     $years = mysql_query("SELECT DISTINCT YEAR(event_date) as years, DATE_FORMAT(event_date,'%b') as months from news"); 

     $result = array(); 

     while($page=mysql_fetch_array($years)) 
     { 

      $year = $page['years']; 
      $month = $page['months']; 

      $result[$year][] = $page['months']; 

     } 

     if(isset($result)) { 

     foreach($result as $key=>$year_array) { ?> 

      <div class="year"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $key; ?>&month=<?php echo $month; ?>"><?php echo $key; ?></a></div> 
      <?php 
      if(isset($year_array) && count($year_array) != 0) { 
       foreach($year_array as $month) { ?> 

        <div class="month"><a href="<?php echo $sys_domain; ?>/news/index.php?year=<?php echo $key; ?>&month=<?php echo $month; ?>"><?php echo $month; ?></a></div> 

      <?php } } 
     } } ?> 
+0

我得到了錯誤信息 - >函數名必須是字符串 – Eelyn

+0

什麼行顯示錯誤併發布更新後的編碼? – msvairam

+0

$ print_r($ result); – Eelyn

0

更新你的代碼是這樣的:

$years = mysql_query("SELECT YEAR(event_date) as years, GROUP_CONCAT(DATE_FORMAT(event_date,'%b') SEPARATOR '<br />') as months from news GROUP BY YEAR(event_date)"); 
while($page=mysql_fetch_array($years)) 
{ ?> 
    <div class="year"><?php echo $page['years']; ?></div> 
    <div class="month"><?php echo $page['months']; ?></div> <?php 
} ?> 
+0

對不起,因爲我沒有完全包含我的代碼。我需要通過點擊月份來過濾信息。所以我的代碼如上所示(編輯)。 當我點擊過濾月份時,我的網址反映了'index.php?year = 2016&month = Feb Jan' – Eelyn

+0

檢查代碼是否獲取所需的格式。你可以綁定Html根據你的要求 –