Ok this trick is intended for the newer ColdFusion programmers as most experience programmers will either know this one, or feel silly admitting they didn’t. Let’s assume you have a table of users and each user is assigned to a particular department. For the sake of simplicity we’ll assume the department ID is stored in the user’s table as only one user can be in one department. So you might have a query which looks like this
好的,此技巧是为较新的ColdFusion程序员设计的,因为大多数经验丰富的程序员要么知道这一点,要么傻傻地承认自己不知道。 假设您有一个用户表,并且每个用户都分配到一个特定部门。 为简单起见,我们假设部门ID存储在用户表中,因为一个部门中只能有一个用户。 因此,您可能会有一个看起来像这样的查询
SELECT u.firstName, u.lastName, u.email, d.departmentName, d.departmentID FROM users u JOIN department d on d.departmentid = u.departmentid ORDER BY u.lastname
SELECT u.firstName, u.lastName, u.email, d.departmentName, d.departmentID FROM users u JOIN department d on d.departmentid = u.departmentid ORDER BY u.lastname
This SQL code will return a listing of all the users in your table and they will be ordered by their last names. Now let’s assume that when you started out there were only a handful of employees and two departments. So you might just do something like this for the output:
此SQL代码将返回表中所有用户的列表,并且将按其姓氏对其进行排序。 现在,假设您刚开始时只有几个员工和两个部门。 因此,您可以对输出执行以下操作:
Employee Name Employee Email Employee Department #lastName#, #firstName##email##departmentName# 员工姓名 员工邮箱 员工部 #lastName#,#firstName# #电子邮件# #部门名称#Now you could fancy it up and make it so users could click on a column heading and sort by that column if you want, but we’ll skip that for now. Now fast forward say 2 years, your company has been growing by leaps and bounds. You now have 50 employees and 5 different departments. Your little employee listing app just isn’t cutting it anymore. You boss says he likes having all the employees on one page but he’d rather see them grouped together by department. You take that guidance and head back to your office. Now your first inclination might be to query the DB for departments then loop over each department and grab those respective employees. This is perfectly acceptable but let me show you a way to do it faster, and with less code. Take your previous query and change it to look like this
现在您可以对其进行修饰,以便用户可以单击列标题并根据需要对该列进行排序,但是我们现在将其跳过。 现在快进说了2年,您的公司一直在飞速发展。 您现在拥有50名员工和5个不同的部门。 您的小员工列表应用程序不再有用了。 您的老板说他喜欢将所有员工都放在一页上,但他宁愿按部门将他们分组。 您接受该指导,然后回到您的办公室。 现在,您的第一个倾向可能是查询数据库中的部门,然后遍历每个部门并抓住这些员工。 这是完全可以接受的,但让我向您展示一种以更少的代码更快地完成此操作的方法。 接受您以前的查询并将其更改为如下所示
SELECT u.firstName, u.lastName, u.email, d.departmentName, d.departmentID FROM users u JOIN department d on d.departmentid = u.departmentid ORDER BY d.departmentName
SELECT u.firstName, u.lastName, u.email, d.departmentName, d.departmentID FROM users u JOIN department d on d.departmentid = u.departmentid ORDER BY d.departmentName
Now you’ll notice all we did is change the order by clause. This becomes important later on. Now for the fun part! Let’s get this displayed the way the boss wants it. Take a look at this code below.
现在您会注意到我们所做的只是更改order by子句。 稍后,这变得很重要。 现在是有趣的部分! 让我们以老板想要的方式来显示它。 看下面的这段代码。
#departmentName##lastName#, #firstName##email# #部门名称# #lastName#,#firstName# #电子邮件#So how does it work? The key is the order by clause, and the placement of your cfoutput tags. First you’ll notice we got rid of our cfloop tag and replaced it with a cfoutput tag with the query and group attributes. Now what you may or may not know is when you do a
那么它是怎样工作的? 关键是order by子句以及cfoutput标签的位置。 首先,您会注意到我们摆脱了cfloop标记,并将其替换为带有查询和组属性的cfoutput标记。 现在,您可能会或可能不知道的是何时进行
the ColdFusion server will loop over the results of the query and output the code between the start and end tag for each row in your result set. Now with this logic in mind you might be thinking we are going to get one ugly output with the department name being repeated before each employee. Not with ColdFusion! You see the extra cfoutput tag pair later on in the table (at the start and end of the HTML code for our employee data row). ColdFusion will execute everything on the outside of these tags once and everything on the inside it will repeat for each row, BUT it will only output the rows which have employees in that department. So to put it into perspective the ColdFusion server will execute this code once
ColdFusion服务器将循环查询结果,并在结果集中每一行的开始和结束标记之间输出代码。 现在考虑到这一逻辑,您可能会认为我们将得到一个丑陋的输出,在每个员工之前重复使用部门名称。 不支持ColdFusion! 您稍后会在表格中看到额外的cfoutput标签对(在我们的员工数据行HTML代码的开头和结尾处)。 ColdFusion将在这些标签外部执行一次所有操作,并且内部所有内容将针对每一行重复执行一次,但是它将仅输出该部门中拥有员工的行。 因此,从整体上看,ColdFusion服务器将执行一次此代码
then it will loop over this code for each employee record, making sure it only outputs those employees which belong to the department displayed above
and then it will output this code:
#departmentName##lastName#, #firstName##email#然后它将为每个员工记录遍历此代码,确保仅输出属于上面显示的部门的那些员工
然后它将输出以下代码:
#部门名称# #lastName#,#firstName# #电子邮件#To finish the process up. In short the ColdFusion server is going do to the double looping for you. The caveat here is that your group=”” value in your cfoutput tag must be the same as your order by clause in your SQL statement.
完成该过程。 简而言之,ColdFusion服务器将为您完成双循环。 需要注意的是,cfoutput标记中的group =””值必须与SQL语句中的order by子句相同。
Now wasn’t that a bit easier than doing all the looping yourself?
现在难道不是比自己完成所有循环容易吗?
翻译自: https://www.sitepoint.com/trick-for-the-newbies/