Using Conditional Logic

I l @ ve RuBoard

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 statusyou 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 .

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. Open a Web browser and type the following address into the location window: http://127.0.0.1/Lesson7/Start/productstatus.cfm .

    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.

  6. 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.

    NOTE

    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.

    NOTE

    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.

  7. 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.

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

     <cfelse> 

    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.

  9. Add the closing </cfif> tag.

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

  10. Open a Web browser and type the following address into the location window: http://127.0.0.1/Lesson7/Start/productstatus.cfm .

    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?

  11. 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.

  12. 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.

  13. Open a Web browser and type the following address into the location window: http://127.0.0.1/Lesson7/Start/productstatus.cfm .

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

I l @ ve RuBoard


Macromedia ColdFusion 5. Training from the Source
Macromedia ColdFusion 5 Training from the Source (With CD-ROM)
ISBN: 0201758474
EAN: 2147483647
Year: 2002
Pages: 99
Authors: Kevin Schmidt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net