Listing items only from an specific range

Discussion in 'Programming' started by jstama, Mar 19, 2008.

  1. #1
    Hi,

    I have a list of about 100 pages, each of them using date_create variable for the date they were entered in the database; these entries are from years 2005 to 2008. I need to list the ones from the last six months only, but without hardcoding specific values, so it automatically outputs the correct list. Could somebody help me modifying the code to accomplish this?

    The code I'm using as of now is:

    <cfquery name="disp_news_arti_curr" datasource="#Request.MainDSN#" CACHEDWITHIN="#CreateTimeSpan(1,0,0,0)#">
    SELECT
    id_news_arti,url,name_pub,date_create,author_pub,title
    FROM
    data_news_arti
    WHERE
    status = 1 AND date_create >= '01 Sep 2005'
    ORDER BY
    date_create DESC
    </cfquery>

    Thanks
    jstama@hotmail.com
     
    jstama, Mar 19, 2008 IP
  2. unitedlocalbands

    unitedlocalbands Well-Known Member

    Messages:
    246
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    128
    #2
    Looks like what you want is to always only show the last six monthes of data... right?

    If so you can use a <cfset> tag to set a variable that will always generate a date six monthes ago

    
    
    [COLOR="Blue"]<cfset last_six_monthes = dateadd("m", + -6, #now()#)>[/COLOR]
    
    <cfquery name="disp_news_arti_curr" datasource="#Request.MainDSN#" CACHEDWITHIN="#CreateTimeSpan(1,0,0,0)#">
    
    SELECT id_news_arti, url, name_pub, date_create, author_pub, title
    
    FROM data_news_arti
    
    WHERE status = 1 AND date_create [COLOR="blue"]>= #last_six_monthes#[/COLOR]
    
    ORDER BY date_create DESC
    </cfquery>
    
    
    Code (markup):
    You would have to have the data stored in the database as date_time format.

    If not then you just need to re format your #last_six_monthes# var to what ever format you need to match your database column.

    check out these two links
    http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt159.htm#1103264

    http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt09.htm#1098968

    A new date format might look like this
    
    <cfset last_six_monthes = dateadd("m", + -6, #now()#)>
    
    <cfquery name="disp_news_arti_curr" datasource="#Request.MainDSN#" CACHEDWITHIN="#CreateTimeSpan(1,0,0,0)#">
    
    SELECT id_news_arti, url, name_pub, date_create, author_pub, title
    
    FROM data_news_arti
    
    WHERE status = 1 AND date_create [COLOR="blue"]>= #dateformat( last_six_monthes, 'dd-mmm-yyyy')#[/COLOR]
    
    ORDER BY date_create DESC
    </cfquery>
    
    Code (markup):
    You can use whatever mask you need.
     
    unitedlocalbands, Mar 20, 2008 IP
  3. jstama

    jstama Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Great!! ... many thanks for your help. That gave me the results I needed.
     
    jstama, Mar 21, 2008 IP