开发者

Group by Date - Datetime Problem

开发者 https://www.devze.com 2023-02-12 14:41 出处:网络
Guys, I really need your help on this one. I have a list of articles using smarty I want to organize by date,

Guys, I really need your help on this one.

I have a list of articles using smarty I want to organize by date,

Date1
ArticleA
Date2
ArticleB
ArticleC

The problem I am having is that on Date2 (or if there are more than 1 articles posted on the same day) the entire Date2 shows up again...i.e

Date1
ArticleA
Date2
ArticleB
ArticleC
Date2
ArticleB
ArticleC

What I THINK the problem is, is that the Date2 is a datetime for when the article was posted, so the reason its repeating each time is because the time values are different.

I need to find a way to remove the time value from datetime or somehow manipulate it to show just the date.

Here is my code

SQL

$sql = 'select ' . TABLE_ADS . ('' . '.*' . $mlang_vars . ', ') . TABLE_ADS . '.id as adid, ' . TABLE_ADS_PICTURES . '.picture, ' . TABLE_ADS_PICTURES . '.`id` as `picture_id`, date_format(' . TABLE_ADS . ('' . '.`date_added`,\'' . $开发者_StackOverflowdate_format . '\') as date_nice, date_format(') . TABLE_ADS . ('' . '.`date_expires`,\'' . $date_format . '\') as date_expires_nice, (') . TABLE_ADS . ('' . '.date_expires < \'' . $timestamp . '\' and ') . TABLE_ADS . '.date_expires!=\'0000-00-00 00:00:00\' and ' . TABLE_ADS . '.active=0) as expired, ' . TABLE_CATEGORIES . '_lang.name as category, ' . TABLE_PACKAGES . '.amount as amount, ' . TABLE_USERS . ('' . '.username as username ' . $pri_name . ' ' . $video . ' ' . $q_vars . ' from ') . TABLE_ADS . ' 
    LEFT JOIN ' . TABLE_CATEGORIES . '_lang on (' . TABLE_ADS . '.category_id=' . TABLE_CATEGORIES . '_lang.id and ' . TABLE_CATEGORIES . ('' . '_lang.`lang_id` = \'' . $crt_lang . '\')  
    LEFT JOIN ') . TABLE_PACKAGES . ' on ' . TABLE_ADS . '.package_id=' . TABLE_PACKAGES . '.id  
    LEFT JOIN ' . TABLE_USERS . ' on ' . TABLE_ADS . '.user_id=' . TABLE_USERS . '.id  
    LEFT JOIN ' . TABLE_ADS_PICTURES . ' on ' . TABLE_ADS . '.id=' . TABLE_ADS_PICTURES . ('' . '.ad_id 
    ' . $pri_join . ' 
    ' . $video_join . ' 
    ' . $q_join . ' 
    ') . $where . ' group by ' . TABLE_ADS . '.id ' . $order_by . ' ' . $order_way . ' '; 

PHP

{foreach from=$listings_array name="date_nice" item=k}{$k.date_nice} 
{foreach from=$listings_array  name="listing" item=v} 
   {if $k.date_nice==$v.date_nice} 
        {include file="short_listing.html"} 
   {/if} 
{/foreach} 
{/foreach} 


You can change the first line of your select to

$sql = 'select ' . TABLE_ADS . ('' . '.*' . $mlang_vars . ', ') . TABLE_ADS . '.id as adid, ' . TABLE_ADS_PICTURES . '.picture, ' . TABLE_ADS_PICTURES . '.`id` as `picture_id`, date_format(' . TABLE_ADS . ('' . '.DATE(`date_added`),\'' . $date_format . '\') as date_nice, date_format(') . TABLE_ADS . ('' . '.DATE(`date_expires`),\'' . $date_format . '\') as date_expires_nice, (') . TABLE_ADS . ('' . '.date_expires < \'' . $timestamp . '\' and ') . TABLE_ADS . '.date_expires!=\'0000-00-00 00:00:00\' and ' . TABLE_ADS . '.active=0) as expired, ' . TABLE_CATEGORIES . '_lang.name as category, ' . TABLE_PACKAGES . '.amount as amount, ' . TABLE_USERS . ('' . '.username as username ' . $pri_name . ' ' . $video . ' ' . $q_vars . ' from ') . TABLE_ADS . ' 

Basically using DATE() to work with just the date portion

0

精彩评论

暂无评论...
验证码 换一张
取 消