Solved

ASP syntax question pt3

Posted on 2013-11-28
20
362 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 8
20 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39683919
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
ID: 39683972
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
ID: 39683984
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
Independent Software Vendors: 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!

 
LVL 58

Expert Comment

by:Gary
ID: 39683993
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
ID: 39684011
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
ID: 39684044
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
ID: 39684065
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
ID: 39684073
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
ID: 39684108
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
ID: 39684318
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
 
LVL 58

Expert Comment

by:Gary
ID: 39684342
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
ID: 39684404
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
ID: 39684420
8, 7, 6, 5, 4, 1 - would return false - it contains a 7
0
 
LVL 1

Author Comment

by:billium99
ID: 39684459
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
ID: 39684470
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
ID: 39684474
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
ID: 39684482
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
ID: 39684492
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
ID: 39684499
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
ID: 39684517
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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