How can I display each days events?

前端 未结 2 856
抹茶落季
抹茶落季 2021-01-24 02:42

I have an events page where I need to display the events for each day. I\'ve gotten it to this point, so I\'m making progress.

The database has 3 tables: fairdays, event

相关标签:
2条回答
  • 2021-01-24 03:14

    (This is too long for comments ...)

    To expand on Dan's answer, he is suggesting a more efficient way of producing that output by using JOIN's and cfoutput's group feature (emphasis is mine):

    ... Eliminates adjacent duplicate rows when data is sorted. Use if you retrieved a record set ordered on one or more a query columns. For example, if a record set is ordered on "Customer_ID" in the cfquery tag, you can group the output on "Customer_ID."

    For your JOIN's you will need to include all three tables to grab all of the columns you need. I cannot test it right now, but something along these lines. (Notice the results are sorted the same way you wish to display them ie by event date, type and time)

    SELECT  ev.eventDay, t.EventType, ev.EventTime
    FROM    fairdays fd 
              INNER JOIN events ev ON ev.eventDay = fd.fairdaydate
              INNER JOIN eventType t ON t.ID = ev.EventType
    
    ORDER BY ev.eventDay, t.EventType, e.EventTime
    

    Once you have the sorted results, use "group" generate the desired results. Be sure to group by the same columns, in the same order, as the sql query. Otherwise, it will not work correctly.

    <cfoutput query="yourQuery" group="EventDay">    
        <!--- display event dates --->     
         #EventDay# <hr/>    
        <!--- event types for current date --->
        <cfoutput group="EventType">
            #EventType#<br/>
            <!--- individual events --->
            <cfoutput>
                #EventTime# <br/>
            </cfoutput>
        </cfoutput> 
    </cfoutput>
    

    Update from comments:

    As discussed in the comments, if you want to retrieve all fairdays (even ones without a matching event) use outer joins instead of inner joins.

    SELECT  fd.fairDayDate, t.ID, ev.EventType, ev.EventTime
    FROM    fairdays fd 
              INNER JOIN events ev ON ev.eventDay = fd.fairDayDate
              INNER JOIN eventType t ON t.ID = ev.EventType
    
    ORDER BY fd.fairDayDate, t.ID, e.EventTime
    

    Again, since the cfoutput "group" feature requires sorted query data to work properly, if you change the ORDER BY clause, be sure to update the "group" columns to match your ORDER BY clause. ie Group by fairDayDate first, then event ID:

    <cfoutput query="yourQuery" group="fairDayDate">    
        <!--- display event dates --->     
         #fairDayDate# <hr/>    
        <!--- event types for current date --->
        <cfoutput group="ID">
            #EventType#<br/>
            <!--- individual events --->
            <cfoutput>
                #EventTime# <br/>
            </cfoutput>
        </cfoutput> 
    </cfoutput>
    
    0 讨论(0)
  • 2021-01-24 03:36

    First, you don't need the first query.

    Next, add

    order by eventday
    

    to query getfairevents. That will enable you to do this:

    <cfoutput query="getfairevents" group = "eventday">
        #eventday#
        <cfoutput>
            output other stuff here (individual events and times)
        </cfoutput>
    </cfoutput>
    
    0 讨论(0)
提交回复
热议问题