2015-02-05 35 views
0

這裏我有一個循環,從數據庫中檢索值。我的問題是我想跳過重複日期值,此代碼完美的作品,但 日期值顯示爲所有文章我只想要日期只顯示 一次。不檢索重複值

例如:

DATE: 5-2-2015 
article 1 
DATE: 5-2-2015 
article 2 
DATE: 4-2-2015 
article 3 

這是我的代碼是如何顯示

,但我想這樣

DATE: 5-2-2015 
article 1 
article 2 
DATE: 4-2-2015 
article 3 




      <?php 
      $postResults = posts(); 
      foreach ($postResults as $postResult) 
      { 
       $date = strtotime($postResult['post_added_time']); 
       echo $date = date('j M Y', $date); 
       ?> 
       <?php 
       if ($postResult['post_university'] == 'JNTU HYDERABAD') 
       { 
       ?> 
        <div class="posts"><img class="arrow" src="<?php echo $site['config']['themeUrl'];?>/assets/images/arrow.GIF"/><a target="_blank" class="green" href="<?php echo $postResult['post_slug']; ?>"><?php echo $postResult['post_university'] . " : " . $postResult['post_title'] ;?></a> 
         <?php 
         if (strtotime($postResult['post_added_time']) > strtotime('-3 days')) 
         { 
         ?> 
          <span class="new"></span> 
         <?php 
         } 
         ?> 
        </div> 
       <?php 
       } 
      } 
      ?> 
+0

你正在運行什麼查詢。 –

+0

功能帖子() { \t global $ dbConnect;我們可以通過下面的例子來說明如何使用SQL查詢語句來查詢數據庫表中的數據。 \t $ query-> bindValue(1,'publish'); \t $ query-> execute(); \t $ results = $ query-> fetchAll(); \t \t return $ results; } – Arvind

回答

0

我通常圍繞

//initialise date first 
date = ''; 

for loop{ 

    //Check for condition as date from loop is not equal to date in loop 
    if(date != date from loop){ 
     date = date from loop 
     echo date; 
    } 

rest of the code of displaying article details will be as below 
... 


} 

希望這樣這可以幫助你。

0

你可以試試下面的查詢。

SELECT distinct date,* FROM " . POSTS . " WHERE status = ? ORDER BY pid DESC 
0
$new = array(); 
     $postResults = array (
       array (
         'post_added_time' => '2015-01-01', 
         'post_title' => 'title 1' 
       ), 
       array (
         'post_added_time' => '2015-01-02', 
         'post_title' => 'title 2' 
       ), 
       array (
         'post_added_time' => '2015-01-03', 
         'post_title' => 'title 3' 
       ), 
       array(
         'post_added_time' => '2015-01-03', 
         'post_title' => 'title 4' 
       ), 
     ); 

     foreach ($postResults as $postResult) { 
      $date = $postResult ['post_added_time']; 
      $new [$date] [] = $postResult; 
     } 

     echo "<pre>"; 
     print_r($new); 
     die; 

它會打印:

Array 
(
    [2015-01-01] => Array 
     (
      [0] => Array 
       (
        [post_added_time] => 2015-01-01 
        [post_title] => title 1 
       ) 

     ) 

    [2015-01-02] => Array 
     (
      [0] => Array 
       (
        [post_added_time] => 2015-01-02 
        [post_title] => title 2 
       ) 

     ) 

    [2015-01-03] => Array 
     (
      [0] => Array 
       (
        [post_added_time] => 2015-01-03 
        [post_title] => title 3 
       ) 

      [1] => Array 
       (
        [post_added_time] => 2015-01-03 
        [post_title] => title 4 
       ) 

     ) 

) 

您可以透過美元的新陣列打印值按您的要求。