Join Candyce Mairs for an in-depth discussion in this video Outputting query results, part of Learning ColdFusion 10.
What I now want to do is take query data and output it onto the page. So that's what I'm going to set up. And in order to do that, I'm going to open up the build query page. I'm also going to open up the output query page. The output query page is going to display my results and the reason I want the build query page is so that I don't have to retype my query. So on the building a query page, I'm going to copy my query code. Go over to my output query and paste it.
Now I have my cursor between a paragraph tag and that really doesn't make sense because all I'm doing is running a query on a data base and then corefusion has the database information. I'm not putting it onto the page at all, and quite often you want the query to run before the page begins to be put together. So what I'm going to do is place this query up at the very top of the page above the doc type So that the first thing to run is going to be my query.
And I'll clean up this extra space here just a little bit. Make my code a little bit neater. So that it's easier for you to see what's going on. As I'm building my pages, it's a good plan to always clean things up that you may see. Now, I'm going to run my query, but notice this query only has a data source attribute. I have no way to reference it. So, this query needs a name, and I'm going to reference it as QTours. So, I'll give my query a name called QTours.
Now when I go to output that content, I have a way to reference it. So down below this h one I do have these two p tags. And what I'm going to say is pound hue tours dot record count. Because that is the number of records that are being returned. And I will say, records returned from the Q Tours query.
Okay. So I have the number of records being returned. Now what I need to do is actually put the information within this area. And because I want to output it onto the page, I'm going to use the CF output tag. So, there's my CF output tag opening and I will do my closing as well. You can see how CF builder kind of pops it right back into the left there.
And I'm going to output my query within this tag. So how do I do that? Well, I reference it using #, Q Tours dot. And then I reference it based on the actual column in the database. So let me reference tour name first. Then I'm going to put a break in here. And next, I will do tour description. So I'll put my # sign, qtourist., wait for this to pop up. You can see there's a slight delay there to our description. Add my closing pound sign.
For some reason it didn't get added there. So that is something to watch. And I will do the same thing, pound Q tours, dot the last 1 is total days, put in my pound sign. And here's my break. Now I technically don't need a break, I'll leave it off of that last one. So here is my CF output tag. Now this break is an error. I'll go ahead and fix that.
Let's take a look, at what happens when I run this query. Now you can see my record count value was not pulled out. I see my pound signs on the page and that's one of the most common errors when you get use to working with Cold Fusion. If this happens where you see your Cold Fusion code What that means is nothing is processing the value of that and that's because we don't have the CF output tag surrounding it.
So, let me g back to source and the first thing I want to do is move my opening CF output tag. I'm going to click and drag it above the record count. Anything inside the CF upload, what you're doing is telling ColdFusion to process the information. Now that I have that set up, get rid of the space here, and let's take a look. I'll save that, go back into Firefox, now look what happened.
There's only one query record being returned. The reason for that is within my CF output, it's only running once. So, the first record returned from this tours query is the only thing that's showing on the page. The way you get around that is in your CF output tag, notice there's a query attribute. And what I do here is tell it which query to output.
What that is going to do is tell cold fusion keep running this until there are no more queries. And I will go ahead and put this inside a paragraph tag. And I'll put the closing paragraph tag here. Now, sometimes it can get difficult to kind of see what's going on. If you find this font is a little bit too small to be able to view easily, there is a way to be able to enlarge the font. But let's run this query first.
I'll go ahead and save Put it into FireFox, and now you can see, I'm getting all of my query records returned, but, I'm also getting this repeated. Over, and over, and over, each time. That's because, every time it runs the query, (no period) It's also running my record count. And this is an example of an area leaving its own CF output tag.
So what I am going to do is take the CF output above, move it back down to where I started with it. And this cutetours.org could count needs it own CF Output Tag. And I'm going to let CF builder wrap that for me. This little what looks like a computer monitor if I have something selected and I click that. You can see it adds that code for me, which is really handy.
I'll save. I'll put this back into the browser one more time and now you can see each individual query is listed, the three pieces of information and the number of records is only listed once. So, that is how you can output a query. You use the CF output tag with the query attribute that lists the name of the query and that code will keep running for every record returned from the query and that will be placed onto the page exactly as you reference it within that CF output tag.
So that is how you can output data to a web page from a query.
- Understanding static vs. dynamic requests
- Using the ColdFusion Administrator
- Creating, displaying, and scoping variables
- Working with include files
- Pulling data from a database
- Displaying and modifying database data
- Passing data between pages
- Working with form data
- Populating form fields with query data