本文介绍了如何显示每天的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个活动页面,我需要显示每一天的活动。 这个数据库有3个表:fairdays,eventtypes,events fairdays:id,fairdaydate(datetime),daycolor,description eventtypes:id ,eventtype< - 此表用于添加事件表单 事件:id,eventname,eventtype,eventday ,eventtime(datetime) 我的目的是显示带有描述的日期, ,那么下面的每一个都是相应的事件。 我还没有弄清楚如何显示事件类型子标题,但这里是我的代码到目前为止。任何帮助将非常感激。 < cfquery datasource =fairscheduledbname =getfairdays> select * from fairdays < / cfquery> < cfquery datasource =fairscheduledbname =getfairevents> select * from events ev inner join fairdays fd on fd.fairdaydate = ev.eventday 其中ev.eventday = fd.fairdaydate < / cfquery> < cfloop query =getfairdays> < cfoutput> < div class =schedulebox> < div class =scheduleheadstyle =color:### getfairdays.daycolor#;> #dateformat(getfairdays.fairdaydate,dddd,mmmm dd)#< / div> < div class =schedulesubheadstyle =color:## ffffff; background:### getfairdays.daycolor#;>#getfairdays.description#< / div> < cfoutput query =getfairevents> < div class =scheduleitemstyle =float:left; width:75px; text-align:right;> #LCase(TimeFormat(getfairevents.eventtime,h:mmtt))#< ; / div> < div class =scheduleitemstyle =float:left; width:550px;>#getfairevents.eventname#< / div>< br / < / cfoutput> < / div> < / cfoutput> < / cfloop> 这里是什么日期和事件的列表应该是: < div class =schedulebox> < div class =scheduleheadstyle =color:#4CC417;> 2月22日星期五< / div> < div class =schedulesubheadstyle =color:#ffffff; background:#4CC417;>开幕式下午4:30在门< br /> 5:00 - 6:00 pm - 免费入场& amp;免费乘车< / div> < div class =scheduleitemtitlestyle =color:#4CC417;>漫步活动< / div> < div class =scheduleitem> 5:30pm - Scotts Magic Show< / div> < div class =scheduleitem> 6:30pm - Rock-It the Robot< / div> < div class =scheduleitem> 7:30pm - Scotts Magic Show< / div> < div class =scheduleitem> 8:30pm - Rock-It the Robot< / div> < div class =scheduleitemtitlestyle =color:#4CC417;> Acts< / div> < div class =scheduleitem> 5:30pm - 海狮表演< / div> < div class =scheduleitem> 6:00pm - Alligator Wrestling< / div> < div class =scheduleitem> 6:30pm - Petting Zoo Presentation< / div> < div class =scheduleitem> 8:00pm - Alligator Wrestling< / div> < div class =scheduleitem> 8:30pm - Petting Zoo Presentation< / div> < div class =scheduleitem> 9:00pm - 海狮表演< / div> < div class =scheduleitemtitlestyle =color:#4CC417;> Stage Acts< / div> < div class =scheduleitem> 7:00pm - Youth Royalty< / div> < div class =scheduleitemtitlestyle =color:#4CC417;> Livestock Program< / div> < div class =scheduleitem> 6:00pm - 牛肉繁殖筛选< / div> < div class =scheduleitem> 7:00pm - 马鉴定比赛< / div> < / div> 解决方案 要展开丹的回答,他建议更多通过使用JOIN和 cfoutput的组功能生成该输出的高效方式(重点是我的): ...当数据排序时,消除相邻重复行如果您检索到在一个或多个查询列上排序的记录集,请使用。对于示例,如果在cfquery 标记中的Customer_ID上对记录集进行排序,则可以将输出分组到Customer_ID。 对于你的JOIN,你需要包括所有三个表来获取你需要的所有列。我现在不能测试,但是沿着这些线。 (注意,结果按照您希望显示的方式排序,即按事件日期,类型和时间排序) eventDay,t.EventType,ev.EventTime from fairdays fd INNER JOIN事件ev ON ev.eventDay = fd.fairdaydate INNER JOIN eventType t ON t.ID = ev.EventType ORDER BY ev.eventDay,t.EventType,e.EventTime 有排序结果,使用group生成所需的结果。请确保 group 由相同的列以相同的顺序作为sql查询。否则,它将无法正常工作。 < cfoutput query =yourQuerygroup =EventDay> <!---显示事件日期---> #EventDay#< hr /> <!---当前日期的事件类型---> < cfoutput group =EventType> #EventType#< br /> <!--- individual events ---> < cfoutput> #EventTime#< br /> < / cfoutput> < / cfoutput> < / cfoutput> 从评论更新: 如评论中所述,如果您要检索所有公平日(即使没有匹配的事件),请使用外部联接而不是内部联接。 SELECT fd.fairDayDate,t.ID,ev.EventType,ev.EventTime from fairdays fd INNER JOIN事件ev ON ev.eventDay = fd.fairDayDate INNER JOIN eventType t ON t.ID = ev.EventType ORDER BY fd.fairDayDate,t.ID,e.EventTime 同样,由于cfoutputgroup功能需要排序的查询数据才能正常工作, ORDER BY子句,请务必更新组列以匹配您的 ORDER BY 子句。即 fairDayDate 分组 ID : < cfoutput query =yourQuerygroup =fairDayDate> <!---显示事件日期---> #fairDayDate#< hr /> <!---当前日期的事件类型---> < cfoutput group =ID> #EventType#< br /> <!--- individual events ---> < cfoutput> #EventTime#< br /> < / cfoutput> < / cfoutput> < / cfoutput> 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, eventtypes, eventsfairdays: id, fairdaydate (datetime), daycolor, descriptioneventtypes: id, eventtype <-- This table is for the input select in the "add event form"events: id, eventname, eventtype, eventday (datetime), eventtime (datetime)My intent is to display the day with the description, then under that is the event types, then under each of those is the corresponding events.I haven't worked out how to display the event type sub heading, then the individual events under each of those, but here's my code so far. Any help would be hugely appreciated.<cfquery datasource="fairscheduledb" name="getfairdays"> select * from fairdays</cfquery><cfquery datasource="fairscheduledb" name="getfairevents"> select * from events ev inner join fairdays fd on fd.fairdaydate = ev.eventday where ev.eventday = fd.fairdaydate</cfquery><cfloop query="getfairdays"><cfoutput> <div class="schedulebox"> <div class="schedulehead" style="color: ###getfairdays.daycolor#;">#dateformat(getfairdays.fairdaydate,"dddd, mmmm dd")#</div> <div class="schedulesubhead" style="color: ##ffffff; background: ###getfairdays.daycolor#;">#getfairdays.description#</div> <cfoutput query="getfairevents"> <div class="scheduleitem" style="float: left; width: 75px; text-align: right;">#LCase(TimeFormat(getfairevents.eventtime,"h:mmtt"))#</div> <div class="scheduleitem" style="float: left; width: 550px;">#getfairevents.eventname#</div><br/> </cfoutput> </div></cfoutput></cfloop>Here's what the list of days and events should look like:<div class="schedulebox"> <div class="schedulehead" style="color: #4CC417;">Friday, February 22</div> <div class="schedulesubhead" style="color: #ffffff; background: #4CC417;">Opening Ceremony 4:30pm at Gate<br/>5:00 - 6:00pm - Free Admission &amp; Free Rides</div> <div class="scheduleitemtitle" style="color: #4CC417;">Strolling Acts</div> <div class="scheduleitem">5:30pm - Scotts Magic Show</div> <div class="scheduleitem">6:30pm - Rock-It the Robot</div> <div class="scheduleitem">7:30pm - Scotts Magic Show</div> <div class="scheduleitem">8:30pm - Rock-It the Robot</div> <div class="scheduleitemtitle" style="color: #4CC417;">Acts</div> <div class="scheduleitem">5:30pm - Sea Lion Show</div> <div class="scheduleitem">6:00pm - Alligator Wrestling</div> <div class="scheduleitem">6:30pm - Petting Zoo Presentation </div> <div class="scheduleitem">8:00pm - Alligator Wrestling</div> <div class="scheduleitem">8:30pm - Petting Zoo Presentation </div> <div class="scheduleitem">9:00pm - Sea Lion Show</div> <div class="scheduleitemtitle" style="color: #4CC417;">Stage Acts</div> <div class="scheduleitem">7:00pm - Youth Royalty</div> <div class="scheduleitemtitle" style="color: #4CC417;">Livestock Program</div> <div class="scheduleitem">6:00pm - Beef Breeding Screening</div> <div class="scheduleitem">7:00pm - Horse Judging Competition</div></div> 解决方案 (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.EventTimeFROM fairdays fd INNER JOIN events ev ON ev.eventDay = fd.fairdaydate INNER JOIN eventType t ON t.ID = ev.EventTypeORDER BY ev.eventDay, t.EventType, e.EventTimeOnce 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.EventTimeFROM fairdays fd INNER JOIN events ev ON ev.eventDay = fd.fairDayDate INNER JOIN eventType t ON t.ID = ev.EventTypeORDER BY fd.fairDayDate, t.ID, e.EventTimeAgain, 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> 这篇关于如何显示每天的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-27 14:01