Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 374
  • Last Modified:

ASP syntax question pt3

Hi,

Happy Holidays!

I have users in a system and when they log in, we retrieve a UserTypes session variable that is a comma delimited list. I have a menu item that I only want to show to certain user types if they are not also one particular user type.

For example, I have a user with this value for UserTypes:

8, 7, 5, 3, 2

One of the experts here helped me create a check for "CONTAINS this NOT this" by using the following script:

<%
' functions are typically at the top or bottom of your page.
function checkIT(user_type,valueToCheckInclude, valueToCheckExclude)
     checkInclude=false
     checkExclude=true
     arrUsertype=split(user_type,",")
     for each number in arrUsertype
          if cstr(number)=cstr(valueToCheckInclude) then
               checkInclude=true
         end if
         if cstr(number)=cstr(valueToCheckExclude) then
               checkExclude=false
         end if

    next
    if checkInclude = true And checkExclude = true then
     checkIT = true
   else
     checkIT = false
   End if
end function

%>

Open in new window


And then when I want to show a menu item  if you are 2 but not 1 or 7, I do it like this and it works great:

<% IF checkIT(Session("UserTypes"),"2","1") or checkIT(Session("UserTypes"),"2","7") THEN %>

Open in new window

           


But now I'm trying to check for numerous usertypes and always not the same usertype and I can't get this to work:

<% IF checkIT(Session("UserTypes"),"2","7") or checkIT(Session("UserTypes"),"3","7") or checkIT(Session("UserTypes"),"4","7") or checkIT(Session("UserTypes"),"5","7") or checkIT(Session("UserTypes"),"6","7") or checkIT(Session("UserTypes"),"8","7") THEN %>

Open in new window


Is there something wrong with my syntax or will this checkIT function not work for this purpose?

Thank you

Bill
0
billium99
Asked:
billium99
  • 12
  • 8
1 Solution
 
GaryCommented:
Not sure what you are wanting to happen.
According to your check above...
If the user type contains 2, 3, 4, 5, 6 or 8 (valueToCheckInclude) and contains 7 (valueToCheckExclude) then you condition will return false

So 8, 7, 5, 3, 2 - will be false - it contains either 2, 3, 4, 5, 6 or 8 but it also contains 7
0
 
billium99Author Commented:
Hi Gary,

My understanding is that the second check in each statement is a NOT CONTAINS.

So this:

<% IF checkIT(Session("UserTypes"),"2","1") or checkIT(Session("UserTypes"),"2","7") THEN %>

...is saying if UserTypes contains 2 and NOT 1 OR if UserTypes contains 2 and NOT 7 then...

Again this is working great - I have a section where 2's but not 1's or 7's should go and only 2's see this menu item, thanks to the code above.

But I can't seem to get it to work if I'm saying if UserTypes contains 2 or 3 or 4 or 5 or 6 or 8 but NOT 7 using that last code snippet in my question. If there something wrong with my syntax? Or am I misunderstanding your answer?

Thanks for your time

Bill
0
 
GaryCommented:
That's what I said I think, take your example
User type is 2,3,7 or user type is 1,2,3
then
<% IF checkIT(Session("UserTypes"),"2","1") or checkIT(Session("UserTypes"),"2","7") THEN %>

will return false

User type is 2,3,6 or user type is 2,3,8
then
<% IF checkIT(Session("UserTypes"),"2","1") or checkIT(Session("UserTypes"),"2","7") THEN %>

will return true
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GaryCommented:
Your code here
<% IF checkIT(Session("UserTypes"),"2","7") or checkIT(Session("UserTypes"),"3","7") or checkIT(Session("UserTypes"),"4","7") or checkIT(Session("UserTypes"),"5","7") or checkIT(Session("UserTypes"),"6","7") or checkIT(Session("UserTypes"),"8","7") THEN %>

will continue with that code block if any one of those conditions is true
0
 
billium99Author Commented:
Yes exactly, but that's not what's happening.

If UserType contains 2 and not 7 or 3 and not 7 or 4 and not 7 etc THEN ...


But my UserType value of 8, 7, 5, 3, 2 is still coming back as true but NOT 7 is always false because 7 is contained in my comma delimited UserTypes value. So the NOT 7 should be failing the test and not showing my menu item, but it's there.

Any suggestion? Does it have anything to do with the fact that our delimited list is descending in value? but my check is ascending? I didn't think that would make a difference...

Thanks again

Bill
0
 
GaryCommented:
Try this function instead

function checkIT(user_type,valueToCheckInclude, $valueToCheckExclude){
	if instr(","& user_type &",",","& valueToCheckInclude &",")>0 AND instr(","& user_type &",",","& valueToCheckExclude &",")<0 then
		checkIT=true
	else
		checkIT=false
	end if
end function

Open in new window

0
 
billium99Author Commented:
Thanks again for your time!

So this code I just had to remove the $ and { - was getting syntax error - but now it doesn't seem to ever reach true, even with a UserType value of just "2", which should definitely pass this test...

In case your new function would break the parts that I have working correctly with the old function, I renamed the elements within your function like so:

function checkIT2(user_type2,valueToCheckInclude2, valueToCheckExclude2)
	if instr(","& user_type2 &",",","& valueToCheckInclude2 &",")>0 AND instr(","& user_type2 &",",","& valueToCheckExclude2 &",")<0 then
		checkIT2=true
	else
		checkIT2=false
	end if
end function 
%>

Open in new window


...then I used the existing check code, also renamed:

 <% IF checkIT2(Session("UserTypes"),"2","7") or checkIT2(Session("UserTypes"),"3","7") or checkIT2(Session("UserTypes"),"4","7") or checkIT2(Session("UserTypes"),"5","7") or checkIT2(Session("UserTypes"),"6","7") or checkIT2(Session("UserTypes"),"8","7") THEN %>

Open in new window


Is this user error on my part? Or did we need that $ and { along with some others?

Bill
0
 
GaryCommented:
Can you give me an example user type string you are using

The $ and { were a typo on my part converting from PHP to ASP
0
 
GaryCommented:
If I use 2 for the user type I get true - which is what I would expect
If I use 2,7 then I get false - which is also correct since every condition in your OR's requires it doesn't have 7 in it.
0
 
billium99Author Commented:
And yet the menu item is displaying when my user type is 8, 7, 5, 3, 2 and it should not be. As you said, it should show as false, since 7 is present.
0
 
GaryCommented:
Are you using my function?
It will need this amendment to remove spaces.

function checkIT(user_type,valueToCheckInclude, $valueToCheckExclude){
user_type=replace(user_type," ","")
	if instr(","& user_type &",",","& valueToCheckInclude &",")>0 AND instr(","& user_type &",",","& valueToCheckExclude &",")<0 then
		checkIT=true
	else
		checkIT=false
	end if
end function

Open in new window

0
 
billium99Author Commented:
So now I'm back to not seeing the menu item in ANY scenario.

Here is my modification to your code:

function checkIT2(user_type2,valueToCheckInclude2, valueToCheckExclude2)
user_type2=replace(user_type2," ","")
	if instr(","& user_type2 &",",","& valueToCheckInclude2 &",")>0 AND instr(","& user_type2 &",",","& valueToCheckExclude2 &",")<0 then
		checkIT2=true
	else
		checkIT2=false
	end if
end function

Open in new window


As you can see, I renamed the elements, but then use the renamed values in my check.

First with my 8, 7, 6, 5, 4, 1 then I tried with just a value of 2 only. Just like two messages ago - I now see nothing, even when the test should be returning true.

Any ideas?

Here is the check code snippet as well:

<% IF checkIT2(Session("UserTypes"),"2","7") or checkIT2(Session("UserTypes"),"3","7") or checkIT2(Session("UserTypes"),"4","7") or checkIT2(Session("UserTypes"),"5","7") or checkIT2(Session("UserTypes"),"6","7") or checkIT2(Session("UserTypes"),"8","7") THEN %>

Open in new window


Thanks
0
 
GaryCommented:
8, 7, 6, 5, 4, 1 - would return false - it contains a 7
0
 
billium99Author Commented:
Yes I know. I'm trying with 2.

And it's still false.

Only with 2 - not with 8, 7, 6, 5, 4, 1 - but only with 2 it still doesn't return true.
0
 
GaryCommented:
I don't know what you are doing

2 returns true for me
2,7 returns false
2,9 returns true

No matter what I put in it always returns correct.
0
 
GaryCommented:
D'oh

function checkIT2(user_type2,valueToCheckInclude2, valueToCheckExclude2)
user_type2=replace(user_type2," ","")
	if instr(","& user_type2 &",",","& valueToCheckInclude2 &",")>0 AND (instr(","& user_type2 &",",","& valueToCheckExclude2 &",")=0 OR instr(","& user_type2 &",",","& valueToCheckExclude2 &",")=Null) then
		checkIT2=true
	else
		checkIT2=false
	end if
end function 

Open in new window


Edited to check for Null - since you are not passing a second param.
0
 
GaryCommented:
Actually that wouldn't matter as I am creating a string.

Add to the function

response.write instr(","& user_type2 &",",","& valueToCheckInclude2 &",") & "-" & instr(","& user_type2 &",",","& valueToCheckExclude2 &",") & "<br>"

and see what is printed out.
0
 
billium99Author Commented:
OK using 8, 7, 5, 3, 2

I get:

9-3
7-3
0-3
5-3
0-3
1-3

Using 2 only, I get:

1-0
0-0
0-0
0-0
0-0
0-0
0
 
GaryCommented:
So the first one should return false and the second should be true.

Hmmm is ASP fussy about case? A long time since I've used it - just in case change.

function checkIT2(user_type2,valueToCheckInclude2, valueToCheckExclude2)
user_type2=replace(user_type2," ","")
	if instr(","& user_type2 &",",","& valueToCheckInclude2 &",")>0 AND instr(","& user_type2 &",",","& valueToCheckExclude2 &",")=0 then
		checkIT2=True
	else
		checkIT2=False
	end if
end function  

Open in new window


If that doesn't work then change your check code to

<% IF checkIT2(Session("UserTypes"),"2","7")=True or checkIT2(Session("UserTypes"),"3","7")=True or checkIT2(Session("UserTypes"),"4","7")=True or checkIT2(Session("UserTypes"),"5","7")=True or checkIT2(Session("UserTypes"),"6","7")=True or checkIT2(Session("UserTypes"),"8","7")=True THEN %>

Open in new window

0
 
billium99Author Commented:
Made the first change - not the second one - and now things are behaving as expected.

Thanks so much for the assistance!

Bill
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 12
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now