Zoo
asked on
Comparing 2 arrays
Okay. I have a table with Groups assigned to a user and a table with Groups assigned to an event.
I am storing the info from each table in a one dimensional array. (This I have working).
One array has Group ID's assigned to a user, the other has all Group ID's assigned to an event.
I want to compare the 2 arrays and see if the Group ID's associated with the user are in the Group ID's from the event.
If they are set a flag on the ones that are the same.
The purpose is to either show or hide info from the user on the display page based on whether they are in a certain group or not.
Can anyone help? Thanks in advance.
I am storing the info from each table in a one dimensional array. (This I have working).
One array has Group ID's assigned to a user, the other has all Group ID's assigned to an event.
I want to compare the 2 arrays and see if the Group ID's associated with the user are in the Group ID's from the event.
If they are set a flag on the ones that are the same.
The purpose is to either show or hide info from the user on the display page based on whether they are in a certain group or not.
Can anyone help? Thanks in advance.
ASKER
the arrays are filled with data (numbers to be exact), as in:
Array 1 - 1, 23, 56, 89 , 78
Array 2 - 1, 2, 3, 56, 78, 98
The numbers are unique ID's of groups.
I want to compare these and where they are the same print to the screen, if they are not the same do not print.
So in the case above print out the events that correspond to numbers 1, 56, and 78.
Here is my code:
Array 1:
<CFQUERY DATASOURCE="#request.MainD SN#" NAME="qryGetGroups">
SELECT group_id
FROM GroupTracking
WHERE Event_ID = #attributes.Event_ID#
</CFQUERY>
<CFSET arrGetGroups = ArrayNew(1)>
<CFSET numCount = 1>
<CFOUTPUT QUERY="qryGetGroups">
<CFSET Variables.arrGetGroups[#Va riables.nu mCount#] = #group_id#>
<CFSET Variables.numCount = Variables.numCount + 1>
</CFOUTPUT>
Array 2:
<CFQUERY DATASOURCE="#request.MainD SN#" NAME="qryUserGroups">
SELECT gm.group_id, gm.user_id, u.user_lastname, g.group_name
FROM GROUPMEMBERSHIP gm, USERS u, GROUPS g
WHERE u.user_id = #session.numUserID#
AND gm.user_id = u.user_id
AND gm.group_id = g.group_id
</CFQUERY>
<CFSET arrGetUsersGroups = ArrayNew(1)>
<CFSET numCount = 1>
<CFOUTPUT QUERY="qryUserGroups">
<CFSET Variables.arrGetUsersGroup s[#Variabl es.numCoun t#] = #group_id#>
<CFSET Variables.numCount = Variables.numCount + 1>
</CFOUTPUT>
Array 1 - 1, 23, 56, 89 , 78
Array 2 - 1, 2, 3, 56, 78, 98
The numbers are unique ID's of groups.
I want to compare these and where they are the same print to the screen, if they are not the same do not print.
So in the case above print out the events that correspond to numbers 1, 56, and 78.
Here is my code:
Array 1:
<CFQUERY DATASOURCE="#request.MainD
SELECT group_id
FROM GroupTracking
WHERE Event_ID = #attributes.Event_ID#
</CFQUERY>
<CFSET arrGetGroups = ArrayNew(1)>
<CFSET numCount = 1>
<CFOUTPUT QUERY="qryGetGroups">
<CFSET Variables.arrGetGroups[#Va
<CFSET Variables.numCount = Variables.numCount + 1>
</CFOUTPUT>
Array 2:
<CFQUERY DATASOURCE="#request.MainD
SELECT gm.group_id, gm.user_id, u.user_lastname, g.group_name
FROM GROUPMEMBERSHIP gm, USERS u, GROUPS g
WHERE u.user_id = #session.numUserID#
AND gm.user_id = u.user_id
AND gm.group_id = g.group_id
</CFQUERY>
<CFSET arrGetUsersGroups = ArrayNew(1)>
<CFSET numCount = 1>
<CFOUTPUT QUERY="qryUserGroups">
<CFSET Variables.arrGetUsersGroup
<CFSET Variables.numCount = Variables.numCount + 1>
</CFOUTPUT>
This should work:
<CFSET listUsersGroups = ArrayToList(variables.arrG etUsersGro ups)>
<CFOUTPUT>
<CFLOOP From="1" TO="#ArrayLen(Variables.ar rGetGroups )#" Index="i">
<CFSET
<CFIF ListFind(listUsersGroups,V ariables.a rrGetGroup s[i]))>
#Variables.arrGetGroups[i] #
</CFIF>
</CFLOOP>
</CFOUTPUT>
You would probably want to do something other than just print out the value, but hopefully you get the idea.
Spike
<CFSET listUsersGroups = ArrayToList(variables.arrG
<CFOUTPUT>
<CFLOOP From="1" TO="#ArrayLen(Variables.ar
<CFSET
<CFIF ListFind(listUsersGroups,V
#Variables.arrGetGroups[i]
</CFIF>
</CFLOOP>
</CFOUTPUT>
You would probably want to do something other than just print out the value, but hopefully you get the idea.
Spike
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked great. Thanks!!
what does the array index represent and what does the array data represent?
i.e. Is the groupID the array index number or is it the data for that index in the array?
Spike