我有一个活动页面,我需要显示每天的活动。我已经到了这一点,所以我正在取得进展。
数据库有3个表:fairdays,eventtypes,events
fairdays:id,fairdaydate(datetime),daycolor,description
eventtypes:id,eventtype< - 此表用于“添加事件表单”中的输入选择
事件:id,eventname,eventtype,eventday(datetime),eventtime(datetime)
我的目的是显示带描述的日期,然后在那下面是事件类型,然后在每个下面是相应的事件。
我还没有弄清楚如何显示事件类型子标题,然后是每个事件下的各个事件,但这是我目前为止的代码。任何帮助都将非常感激。
<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>
以下是日期和事件列表的样子:
<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 & 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>
答案 0 :(得分:4)
首先,您不需要第一个查询。
接下来,添加
order by eventday
查询getfairevents。这将使您能够这样做:
<cfoutput query="getfairevents" group = "eventday">
#eventday#
<cfoutput>
output other stuff here (individual events and times)
</cfoutput>
</cfoutput>
答案 1 :(得分:0)
(这对评论来说太长了......)
为了扩展Dan's answer,他建议使用JOIN和cfoutput's group feature(强调是我的)来更有效地产生输出:
...在数据分类时消除相邻的重复行。如果你使用 检索在一个或多个查询列上排序的记录集。对于 例如,如果在cfquery中的“Customer_ID”上订购了记录集 您可以在“Customer_ID”上对输出进行分组。
对于你的JOIN,你需要包括所有三个表来获取你需要的所有列。我现在无法测试它,但是沿着这些方向做了些什么。 (请注意,结果的排序方式与您希望显示的方式相同,即按事件日期,类型和时间)
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
获得分类结果后,使用“group”生成所需结果。请确保{s}查询的group
与相同的列以相同的顺序。否则,它将无法正常工作。
<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>
从评论中更新:
如评论中所述,如果要检索所有fairdays(即使没有匹配事件的那些),请使用outer 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
同样,由于cfoutput“group”功能要求已排序的查询数据正常工作,因此如果更改ORDER BY子句,请务必更新“group”列以匹配您的ORDER BY
子句。即首先按fairDayDate
分组,然后按事件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>