2011-07-05 24 views
2

我有一個函數將數據庫中的所有日期與每條記錄進行比較。如何從數據庫中獲取日期和顯示沒有任何重複?

public function getAllYears() { 

    $collection = Mage::getModel('press/press')->getCollection()->getYears(); 

    return $collection; 

} 

,並正在顯示它:

<?php 

     $coll = $this->getAllYears(); 

    ?> 


     <?php foreach ($coll as $list): ?> 

       <?php echo $list["year"]; ?> 
     <?php endforeach; ?> 

這是給我所有的年(日期),不關心重複,而我要的是同一日期不得重複。

平均同一年不得重複。 有什麼幫助嗎?

+0

查詢您不能添加GROUPBY條款? –

+1

請參閱:http://stackoverflow.com/questions/4511314/filter-magento-collection-but-not-products-using-distinct我對Magento瞭解不多,但是你在尋找什麼意味着需要一個查詢使用DISTINCT –

+2

用您的查詢結果嘗試這種方式 - > setOrder('year','ASC') - > group('year'); –

回答

2

也許顯示的代碼更改爲:

<?php 
$years = array(); 
$coll = $this->getAllYears(); 
foreach ($coll as $list) 
    $years[] = $list['year']; 
$years = array_unique($years); 
?> 

<?php foreach ($years as $year): ?> 
<?php echo $year; ?> 
<?php endforeach; ?> 
+0

非常感謝:)爲我節省了很多時間:) – kharmato

0

如何:

<?php 
$coll = $this->getAllYears(); 

$lastyear 
foreach ($coll as $list) 
{ 
    if($list["year"] != $lastyear) { 
    echo $list["year"]; 
} 
$lastyear = $list["year"] 
} 
?> 
+3

這是假設返回的年份是有序的,這不一定是這種情況 –

相關問題