【问题标题】:how to get date from database and display without any repetition?如何从数据库中获取日期并在不重复的情况下显示?
【发布时间】:2011-09-28 20:21:38
【问题描述】:

我有一个函数可以根据每条记录从数据库中提取所有日期。

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; ?>

它给了我所有的年份(日期),不关心重复,而我想要的是同一个日期不能重复。

意味着同一年不得重复。 有什么帮助吗?

【问题讨论】:

  • 不能在查询中添加groupby子句?
  • 请参阅:stackoverflow.com/questions/4511314/… 我对 Magento 了解不多,但您正在寻找的内容意味着需要使用 DISTINCT 进行查询
  • 用你的查询试试这个 ->setOrder('year', 'ASC')->group('year');
  • @Shashank 它可能甚至不需要订单,因为group 会自动排序。

标签: php mysql zend-framework magento magento-1.4


【解决方案1】:

或许将显示代码改为:

<?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; ?>

【讨论】:

    【解决方案2】:

    怎么样:

    <?php  
    $coll =  $this->getAllYears();
    
    $lastyear
    foreach ($coll as $list)
    {
        if($list["year"] != $lastyear) {
        echo $list["year"];
    }
    $lastyear = $list["year"]
    }
    ?>
    

    【讨论】:

    • 假设返回的年份是有序的,但不一定如此
    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 2014-05-22
    相关资源
    最近更新 更多