Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

creating an if else within cfscript

Posted on 2005-04-22
5
Medium Priority
?
499 Views
Last Modified: 2013-12-24
hi,
having a little trouble on a cfscript within my cf app

i'm using a script to populate a cell with an image based on which category level (subcat1 or subcat2) is defined - the images are populating fine for subcat 2, but subcat 1 all go to the default?  any help is appreciated

<cfif IsDefined("attributes.ContentSubCat1") AND NOT IsDefined("ContentSubCat2")>
<cfset cat1 = attributes.ContentSubCat1>
<cfset cat2 = 0>
<cfelseif IsDefined("attributes.ContentSubCat1") AND IsDefined("ContentSubCat2")>
<cfset cat1 = attributes.ContentSubCat1>
<cfset cat2 = attributes.ContentSubCat2>
</cfif>

...

<cfif IsDefined("attributes.ContentSubCat1") AND NOT IsDefined("ContentSubCat2")>
<img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG(cat1)#</cfoutput>.jpg" width="178" height="109" />
<cfelseif IsDefined("attributes.ContentSubCat1") AND IsDefined("ContentSubCat2")>
<img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG(cat2)#</cfoutput>.jpg" width="178" height="109" />
</cfif>

...

<cfscript>
function chooseIMG(cat1)
{
      switch(cat1){
              case "9":
                image = "statePlan_Left";
                break;
              case "11":
                image = "strongerNeighborhoods_Left";
                break;
            case "12":
                image = "openLands_Left";
                break;
            case "13":
                image = "transportation_left";
                break;
            
            default:
                  image = "about_left";
            } //end switch

switch(cat2){
              case "1":
                image = "futureFacts_Left";
                break;
              case "2":
                image = "Newsletter_Left";
                break;
            
            default:
                  image = "about_left";
            } //end switch
    return image;
}

</cfscript>
0
Comment
Question by:karmalarm
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:rob_lorentz
ID: 13845754


inside you function chooseIMG(cat1) you refer to cat2 in the second switch/case. it doesnt look like thats ever defined.
0
 
LVL 10

Accepted Solution

by:
rob_lorentz earned 1500 total points
ID: 13845778


also you could simplify your code a bit.

<cfif IsDefined("attributes.ContentSubCat1") AND NOT IsDefined("ContentSubCat2")>
<cfset cat1 = attributes.ContentSubCat1>
<cfset cat2 = 0>
<cfelseif IsDefined("attributes.ContentSubCat1") AND IsDefined("ContentSubCat2")>
<cfset cat1 = attributes.ContentSubCat1>
<cfset cat2 = attributes.ContentSubCat2>
</cfif>

...

<cfif cat2 neq 0>
  <img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG(cat2)#</cfoutput>.jpg" width="178" height="109" />
<cfelse>
  <img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG(cat1)#</cfoutput>.jpg" width="178" height="109" />
</cfif>
0
 
LVL 9

Expert Comment

by:Jerry_Pang
ID: 13847752
cat1 is never called.
create 2 functions intead.

<cfif IsDefined("attributes.ContentSubCat1") AND NOT IsDefined("ContentSubCat2")>
<img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG(cat1)#</cfoutput>.jpg" width="178" height="109" />
<cfelseif IsDefined("attributes.ContentSubCat1") AND IsDefined("ContentSubCat2")>
<img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG2(cat2)#</cfoutput>.jpg" width="178" height="109" />
</cfif>

...

<cfscript>
function chooseIMG(cat1)
{
     switch(cat1){
            case "9":
              image = "statePlan_Left";
              break;
            case "11":
              image = "strongerNeighborhoods_Left";
              break;
          case "12":
              image = "openLands_Left";
              break;
          case "13":
              image = "transportation_left";
              break;
         
          default:
               image = "about_left";
          } //end switch
    return image;
}

function chooseIMG2(cat2)
{
switch(cat2){
            case "1":
              image = "futureFacts_Left";
              break;
            case "2":
              image = "Newsletter_Left";
              break;
         
          default:
               image = "about_left";
          } //end switch
    return image;
}

</cfscript>
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 13848223
Hi karmalarm!

Let me revise your code first.

:: REVISED CODE ::

<cfif IsDefined("attributes.ContentSubCat1")>
  <cfset cat1 = attributes.ContentSubCat1>
  <cfif IsDefined("ContentSubCat2")>
    <cfset cat2 = attributes.ContentSubCat2>
  <cfelse>
    <cfset cat2 = 0>
  </cfif>
</cfif>

...

<cfif cat2 EQ 0>
  <img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG(cat1)#</cfoutput>.jpg" width="178" height="109" />
<cfelse>
  <img src="<cfoutput>#attributes.SiteGraphicPath##chooseIMG(cat2)#</cfoutput>.jpg" width="178" height="109" />
</cfif>

<!--- On your function, you only have one parameter.  So, you only need to switch one variable --->
<cfscript>
function chooseIMG(cat1)
{
  switch(cat1)
  {
    case "1":
      image = "futureFacts_Left";
      break;
    case "2":
      image = "Newsletter_Left";
      break;
    case "9":
      image = "statePlan_Left";
      break;
    case "11":
      image = "strongerNeighborhoods_Left";
      break;
    case "12":
      image = "openLands_Left";
      break;
    case "13":
      image = "transportation_left";
      break;
    default:
      image = "about_left";
  }
 
  return image;
}

Hope this helps you.  Just try it.


Goodluck!
eNTRANCE2002 :-)
0
 
LVL 14

Expert Comment

by:Renante Entera
ID: 13863492
Hi karmalarm!

I think that doesn't solve the problem.  Can you evaluate the comments that we have posted in here ???  Give it a try before accepting an answer.

Try the comment of Jerry_Pang, that's another alternative.  And of course, my comment will really help you a lot.

Perhaps, rob_lorentz and Jerry_Pang will agree with me.


Regards!
eNTRANCE2002 :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Lease-to-own eliminates the expenditure of hardware replacement and allows you to pay off the server over time. Usually, this is much cheaper than leasing servers. Think of lease-to-own as credit without interest.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question