Solved

I would like to count occurencies of elements in a coldfusion 8 array.

Posted on 2008-10-28
8
876 Views
Last Modified: 2012-05-05
I'm using a form with chexboxes to feed a SQL database, the cells are feed with a string of numbers (12,34,4, 6, 12, 34, 7).
Those numbers are ratingIDs of an imageID wich is part of a gallery.
A set of imageIDs rated define the ratingIDs of a movie.
To do so, i need to concatenate and count all the strings of numbers for the gallery set (all images for a given movie)
And use occurrence of numbers to sort desc and keep the 5 highgest occurence of the ratingID.

I reached to put all the strings in an array (1), but i can't find out how to count the elements of the array and output a result.

Thanks for helping!
0
Comment
Question by:khristofe
8 Comments
 
LVL 36

Expert Comment

by:SidFishes
ID: 22821691
use listlen

<cfset test = "12,34,4, 6, 12, 34, 7">

<cfset ct = listlen(test)>

<cfoutput>#ct#</cfoutput>
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 22822343
What are the Cell names for this values: 12,34,4, 6, 12, 34, 7
Give a complete example of cell names and form element names.
0
 
LVL 8

Expert Comment

by:eszaq
ID: 22823221
Not very clear question. What are you trying to do?
Count the elements of the array?  There is a function ArrayLen().
Or sum up values of your checked boxes? There is a function ArraySum(array)

But you might be able to get same results with list functions without using arrays. To help with solution, need more info from you - form-fields names, etc, Provide your code snippet.
0
 

Author Comment

by:khristofe
ID: 22825899
This is what i have:
1 - a list of checkboxes corresponding to a list a ratings to rate an Image.
2 - I rate each image of a film gallery with a set of ratings.
3 - I upload to SQL DB table ratingsPerImage
 
table structure:
FeImRatIDs (PK)    ImageID(FK Gallery)   RatingIDs(FK Ratings)
1                             1                                12,7,5,3,2
2                             2                                5,7,34,17
3                             3                                2,12,5,7,19
4                             4                                12,2,7,8

I make a query to group the ImageIDs for a given FilmID, so it returns an array with all my ratingIDs for the set of ImageIDs of the film gallery-
1 12
2 7
3 5
4 3
5 2
6 5
7 7
8 34
9 17
10 2
11 12
12 5
13 7
14 19
15 12
16 2
17 7
18 8

then i would like to sort ratingIDs by occurrence, list len only tell me the number of elements in my list or array.
listCount inside a list loop could give me a list of occurencies like :
As MyList = (12,7,5,3,2,5,7,34,17,2,12,5,7,19,12,2,7,8)

<cfloop list="#myList#" index="x" delimiters=",">
<cfset countedRatingIDs = ListValueCount(#myNewList#, #x#, ',')>
<cfdump var="#countedRatingIDs #">
</cfloop>
the dump returns: 3 4 3 1 2 1 1 1 1
meaning that 12 has 2 occurrencies, 7 has 4, 5 has 3 etc.
but, how to sort the 5 first ratingIDs like if 7 has 4 occurrencies, then first is 7, 5, 12,2,3,17,19,8?
I was thinking about a script inside the loop, but i'm beginner and don't manage javascript at all, or at the query level using a function into SQL server to sort the concatenated list of ratingIDs per FilmID...
Thanks for helping!

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 8

Accepted Solution

by:
eszaq earned 500 total points
ID: 22843934
You can try query of queries:

<cfset MyList = "12,7,5,3,2,5,7,34,17,2,12,5,7,19,12,2,7,8">

<cfset RateThisImage = 1>

<h1>build query from list</h1>

<cfset CountedRatings=QueryNew("ImageID,RatingValue")>

<cfloop index="x" list="#MyList#" delimiters=",">

	<cfset temp = QueryAddRow(CountedRatings)>

	<cfset temp = QuerySetCell(CountedRatings, "ImageID", RateThisImage)>

	<cfset temp = QuerySetCell(CountedRatings, "RatingValue", #x#)>

</cfloop>

<cfdump var="#CountedRatings#">

<h1>get rating count</h1>

<cfquery name="getTotals" dbtype="query">

	SELECT ImageID, RatingValue, count(*) as totals

	from CountedRatings

	where ImageID=#RateThisImage#

	group by ImageID, RatingValue

	order by totals desc

</cfquery>

<cfdump var="#getTotals#">

Open in new window

0
 

Author Comment

by:khristofe
ID: 22852234
As beginner i didn't knew that we could build a query from a list, that was exactly what i was looking for, so thatnks a lot eszaq!!! ;)
0
 

Author Closing Comment

by:khristofe
ID: 31510667
Perfect! I was stuck trying to work it from the arrays, i reach to have 2 arrays, one with the ratingIDs and another one with the occurrences... but your solution was just perfect! Thanks again!!!
Christophe.
0
 
LVL 8

Expert Comment

by:eszaq
ID: 22852283
:0)
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MS SQL server Varchar and nvarchar, GMT_DATE 23 50
Delete Trigger in SQL Server2008R2 5 13
Join vs where 2 11
Error in query 3 0
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

743 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

11 Experts available now in Live!

Get 1:1 Help Now