Solved

ASP syntax question pt3

Posted on 2013-11-28
20
348 Views
Last Modified: 2013-11-28
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
Comment
Question by:billium99
  • 12
  • 8
20 Comments
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 1

Author Comment

by:billium99
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 1

Author Comment

by:billium99
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 1

Author Comment

by:billium99
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 1

Author Comment

by:billium99
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 1

Author Comment

by:billium99
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
8, 7, 6, 5, 4, 1 - would return false - it contains a 7
0
 
LVL 1

Author Comment

by:billium99
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
 
LVL 1

Author Comment

by:billium99
Comment Utility
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
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
Comment Utility
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
 
LVL 1

Author Comment

by:billium99
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now