• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Lesson 7. Using Conditional Logic > Using Conditional Logic

Using Conditional Logic

When working with a product database, each product is going to have a lot of variables. The quantity that you have on hand, the price, whether the item is in stock, and, if not, its status—you need to be able to inform your customers about each of these unique pieces of information. By using fields in the database and ColdFusion's <cfif> tag, you can display the status of each product to the user.

Open X:\Lesson7\Start\productstatus.cfm.

X is the letter corresponding to your CD drive.

This page will be almost identical to the page where you initially displayed your products in Lesson 5. But this time, when you output the information, you are going to use conditional logic to make the information more useful to the user.

Find the code on line 111 and beyond where the product information is being output.

Currently, the only information that is being output is the product name, product description, and product price. You want to add a line to show a quantity available. And if the quantity happens to be zero, then you want to let the user know what the item's status is.

After the code that displays the price, add the following:

<br> Quantity: #ProductQty# 

Now when you display your template, it will have the product quantity displayed as well. Of course, seeing is believing, so let's view this as a customer would.

Save the productstatus.cfm file to your hard drive, in a folder called Lesson7/Start/ in the Inetpub/wwwroot folder. Once again, don't forget to move the images and the style sheet over as well.

Open a Web browser and type the following address into the location window:

You should now the see that the quantity has been displayed. There are a few items, however, that have a quantity of 0. For these items, you'll also want to display their status so the user can know if the item has been backordered or is discontinued.

On the line above where you added the quantity information, add the following code:

<cfif ProductQty eq 0> 

This checks to see whether the product quantity is zero. If it is, you need to tell the customer the item is out of stock and let them know the item's status.


Both the is and eq operands will perform equality comparisons. That means that the same <cfif> statement could have been written <cfif ProductQty is 0>. That won't work, however, if you use a regular “=” sign.


As you see above, it's not necessary to enclose the variable in # signs within the <cfif> statement. ColdFusion knows that this is a variable.

After your <cfif> statement, add the following code:

<font color="red">This item is out of stock and has been: #ProductStatus#</font> 


You have now specified the message to be output when the product quantity is zero, so that takes care of those items. But when the quantity is not zero, of course you still need to output the quantity variable for the user to see.

After your message for when the quantity is zero, add the following code:


The <cfelse> statement tells ColdFusion what to do when the quantity is not zero. In this case, you are simply telling ColdFusion to output the quantity.

Add the closing </cfif> tag.

Your <cfif> statement is now finished and is ready to be tested.

Open a Web browser and type the following address into the location window:

The page is very similar to before, but this time notice that the products that have a quantity of zero now display the proper message.

Now, what if you wanted to display a different message for when there was only one item remaining?

Before the <cfelse> tag add the following:

<cfelseif ProductQty eq 1> 

The <cfelseif> tag adds another step to your <cfif> tag. ColdFusion is checking the first condition: is the quantity 0? If it is not 0, then ColdFusion moves to next step. ColdFusion checks to see if the quantity is 1. If it is, ColdFusion will output whatever code follows the <cfelseif> tag.

After the <cfelseif> tag add the following line:

#ProductQty# <font color="red">Hurry, only 1 left</font> 

Now you have output the product quantity and given the customer a little reminder to not miss out on the last item in stock.

Open a Web browser and type the following address into the location window:

When you open the browser this time you should see the hurry-up message!



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint