• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 661
  • Last Modified:

Coldfusion rating system

I get the error:
[Macromedia][SQLServer JDBC Driver][SQLServer]Column 'rating.id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

The code is:

application.cfc:
<cfcomponent
	output="false"
	hint="I define the application settings and event handlers.">
 
	<!--- Define the application. --->
	<cfset this.name = hash( getCurrentTemplatePath() ) />
	<cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) />
 
	<!--- Define the page request settings. --->
	<cfsetting showdebugoutput="false" />
 
 
	<cffunction
		name="onApplicationStart"
		access="public"
		returntype="boolean"
		output="false"
		hint="I initialize the application.">
 
		<!--- Define the application. --->
		<!---
		<cfset application.dsn = "xyz" />
		--->
 <cfset application.dsn = "mydsn" />
 <cfset application.username = "u" />
 <cfset application.password = "p" />
		<!--- Return out. --->
		<cfreturn true />
	</cffunction>
 
</cfcomponent>

Open in new window


index.cfm:
<!--- Query for images to rate. --->
<cfquery name="image" datasource="#application.dsn#" username="#application.username#" password="#application.password#">
	SELECT
		i.id,
 
		<!--- Get the current rating for the image. --->
		(
			CASE
				WHEN
					COUNT( r.rating ) > 0
				THEN
					(
						SUM( r.rating ) /
						COUNT( r.rating )
					)
				ELSE
					0
			END
		) AS rating,
 
		<!--- Query for existing rating by user. --->
		COALESCE( er.id, 0 ) AS has_existing_rating
	FROM
		(
			SELECT 1 AS id UNION ALL
			SELECT 2 AS id UNION ALL
			SELECT 3 AS id
		) AS i
 
	<!--- Join this to the rating table to get rating. --->
	LEFT OUTER JOIN
		rating r
	ON
		i.id = r.image_id
 
	<!---
		Join this to the rating table AGAIN to see if the current
		user has already rated the given image.
	--->
	LEFT OUTER JOIN
		rating er
	ON
		(
				er.image_id = i.id
			AND
				er.ip_address = <cfqueryparam value="#cgi.remote_addr#" cfsqltype="cf_sql_varchar" />
			AND
				er.user_agent = <cfqueryparam value="#cgi.http_user_agent#" cfsqltype="cf_sql_varchar" />
		)
 
	GROUP BY
		i.id,
		r.image_id
	ORDER BY
		i.id ASC
</cfquery>
 
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!---
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
--->
	<title>jQuery And ColdFusion Rating System Demo</title>
	<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
	<script type="text/javascript">
 
		// Define jquery plugin.
		jQuery.fn.rating = function( postUrl ){
			// Loop over each list to apply meta data.
			this.each(
				function( index, listNode ){
					var list = $( this );
					var metaData = list.find( "script.meta-data" );
 
					// Check to see if meta data was found.
					if (metaData.size()){
 
						// Apply meta data.
						list.data(
							"metaData",
							eval( "(" + metaData.html() + ")" )
							);
 
						// Remove the meta data node.
						metaData.remove();
 
					}
				}
				);
 
 
			// Initialize the links within the list.
			this.find( "a" )
				.attr( "href", "javascript:void( 0 )" )
				.click(
					function( clickEvent ){
						var link = $( this );
						var list = link.parents( "ul:first" );
						var metaData = list.data( "metaData" );
 
						// Post the rating.
						jQuery.ajax({
							type: "post",
							url: postUrl,
							data: {
								image_id: metaData.id,
								rating: link.text()
								},
							dataType: "json",
							success: function( apiResponse ){
								// Check to see if the API request
								// was valid.
								if (apiResponse.SUCCESS){
 
									// Replace the list with the
									// current rating.
									list
										.empty()
										.append(
											"<li>Rating: " +
											apiResponse.DATA.toFixed( 1 ) +
											"</li>"
											)
									;
 
								}
							}
							});
 
						// Cancel default event.
						return( false );
					})
			;
 
			// Return jQuery object for chaining.
			return( this );
		};
 
 
		// When the DOM is ready, initialize the plugin.
		$(function(){
			$( "ul" ).rating( "rate_image.cfm" );
		});
 
	</script>
	<style type="text/css">
 
		ul.rating {
			height: 20px ;
			list-style-type: none ;
			margin: 10px 0px 0px 0px ;
			padding: 0px 0px 0px 0px ;
			}
 
		ul.rating li {
			float: left ;
			margin: 0px 5px 0px 0px ;
			padding: 0px 0px 0px 0px ;
			}
 
		ul.rating a {
			background-color: #F0F0F0 ;
			border: 1px solid #333333 ;
			color: #333333 ;
			float: left ;
			height: 20px ;
			line-height: 20px ;
			text-align: center ;
			text-decoration: none ;
			width: 20px ;
			}
 
	</style>
</head>
<body>
 
	<h1>
		jQuery And ColdFusion Rating System Demo
	</h1>
 
	<cfoutput>
 
		<cfloop query="image">
 
			<div style="float: left ; margin-right: 20px ;">
 
				<img
					src="./images/girl#image.id#.jpg"
					width="165"
					style="display: block ;"
					/>
 
				<!--- Check to see if user has rated yet. --->
				<cfif image.has_existing_rating>
 
					<!---
						User has already rated, just show the
						current rating.
					--->
					<ul class="rating">
						<li>
							Rating: #numberFormat(
								image.rating,
								"0.0"
								)#
						</li>
					</ul>
 
				<cfelse>
 
					<!--- Show the rating options. --->
					<ul class="rating">
						<!---
							Set up the meta-data for this image.
							This data will be applied when the
							rating plugin is initialized.
						--->
						<script
							type="application/x-json"
							class="meta-data">
							{
								id: #image.id#
							}
						</script>
						<li>
							<a>1</a>
						</li>
						<li>
							<a>2</a>
						</li>
						<li>
							<a>3</a>
						</li>
						<li>
							<a>4</a>
						</li>
					</ul>
 
				</cfif>
 
			</div>
 
		</cfloop>
 
	</cfoutput>
 
</body>
</html>

Open in new window


rate_image.cfm:
<!--- Create a unified API resposne. --->
<cfset apiResponse = {
	success = true,
	errors = [],
	data = ""
	} />
 
 
<!--- Try to execute the api request / response. --->
<cftry>
 
	<!--- Param the FORM variable. --->
	<cfparam name="form.image_id" type="numeric" />
	<cfparam name="form.rating" type="numeric" />
 
 
	<!---
		Check to see if this user has already rated this image.
		We do not want to allow duplicate ratings.
	--->
	<cfquery name="existingRating" datasource="#application.dsn#"  username="#application.username#" password="#application.password#">
		SELECT
			r.id
		FROM
			rating r
		WHERE
			r.image_id = <cfqueryparam value="#form.image_id#" cfsqltype="cf_sql_integer" />
		AND
			r.ip_address = <cfqueryparam value="#cgi.remote_addr#" cfsqltype="cf_sql_varchar" />
		AND
			r.user_agent = <cfqueryparam value="#cgi.http_user_agent#" cfsqltype="cf_sql_varchar" />
	</cfquery>
 
 
	<!--- Check to see if the rating exists. --->
	<cfif existingRating.recordCount>
 
		<!--- Add error. --->
		<cfset arrayAppend(
			apiResponse.errors,
			"You have already rated this image."
			) />
 
	</cfif>
 
 
	<!--- Check to see if we have any errors. --->
	<cfif NOT arrayLen( apiResponse.errors )>
 
		<!--- Insert new rating. --->
		<cfquery name="insertRating" datasource="#application.dsn#" username="#application.username#" password="#application.password#">
			INSERT INTO rating
			(
				ip_address,
				user_agent,
				rating,
				date_created,
				image_id
			) VALUES (
				<cfqueryparam value="#cgi.remote_addr#" cfsqltype="cf_sql_varchar" />,
				<cfqueryparam value="#cgi.http_user_agent#" cfsqltype="cf_sql_varchar" />,
				<cfqueryparam value="#form.rating#" cfsqltype="cf_sql_integer" />,
				<cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp" />,
				<cfqueryparam value="#form.image_id#" cfsqltype="cf_sql_integer" />
			);
 
			<!--- Get the new overall rating. --->
			SELECT
				(
					SUM( r.rating ) /
					COUNT( r.rating )
				) AS overall_rating
			FROM
				rating r
			WHERE
				r.image_id = <cfqueryparam value="#form.image_id#" cfsqltype="cf_sql_integer" />
			;
		</cfquery>
 
 
		<!--- Set the current rating as the response data. --->
		<cfset apiResponse.data = insertRating.overall_rating />
 
	</cfif>
 
 
	<!--- Catch any api errors. --->
	<cfcatch>
 
		<!--- Set the error in our api response object. --->
		<cfset apiResponse.errors = [ cfcatch.message, cfcatch.detail ] />
 
	</cfcatch>
</cftry>
 
 
<!--- Check to see if we have any errors at this point. --->
<cfif arrayLen( apiResponse.errors )>
 
	<!--- Flag the API request as unsuccessful. --->
	<cfset apiResponse.success = false />
 
</cfif>
 
 
<!--- Searialize the API response into our JSON value. --->
<cfset jsonResponse = serializeJSON( apiResponse ) />
 
<!--- Convert the response string to binary for streaming. --->
<cfset binaryResponse = toBinary( toBase64( jsonResponse ) ) />
 
 
<!--- Stream the binary data back. --->
<cfheader
	name="content-length"
	value="#arrayLen( binaryResponse )#"
	/>
 
<cfcontent
	type="text/x-json"
	variable="#binaryResponse#"
	/>

Open in new window

0
swaggrK
Asked:
swaggrK
  • 14
  • 13
2 Solutions
 
_agx_Commented:
On what line - and which script - is the error occurring?
0
 
_agx_Commented:
Oh wait .. you need to include er.id in the GROUP BY:

ie   Do use one of these

      GROUP BY
            i.id, COALESCE( er.id, 0 )
                 
      GROUP BY
            i.id, r.image_id, COALESCE( er.id, 0 )

... instead of this

          GROUP BY
            i.id,
            r.image_id
0
 
DcpKingCommented:
I don't think you need the coalesce in the group by  -- just this should do:

group by i.id, r.image_id, er.id
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!

 
swaggrKAuthor Commented:
That seemed to load the page but now the selections 1,2,3,4 aren't firing.

http://chicagoeliteclassic.com/rating/index.cfm

Open in new window

0
 
_agx_Commented:
When I click a rating, FF's web console says there's still an error in the cfm script.  The first step is test the cfm script manually with an image_id and rating. What's the eror message? (No point adding ajax to the mix if the base cfm script doesn't work yet).

POST http://chicagoeliteclassic.com/rating/rate_image.cfm [HTTP/1.1 500 Internal Server Error 94ms]

HTTP/1.1 500 Internal Server Error
image_id=1&rating=2
0
 
swaggrKAuthor Commented:
Line 19 is...

<cfset apiResponse = {
	success = true,
	errors = [],
	data = ""
	} />

Open in new window


...not really sure why I am getting an invalid token here.
0
 
_agx_Commented:
Not all versions support nesting of implicit array/structures.  If you're using CF8 try:

<cfset apiResponse = {
      success = true,
      errors = arrayNew(1),
      data = ""
      } />
0
 
swaggrKAuthor Commented:
The server I am using for this is CF 6.1...
0
 
_agx_Commented:
Ohh, I didn't know it was an older version. CF6 doesn't support implicit syntax at all. It wasn't introduced until 8, so you've got some rewriting to do.  Replace all the implicit structures {} and arrays [] with structNew() and arrayNew(1).  

For example this:

<cfset apiResponse = {
      success = true,
      errors = arrayNew(1),
      data = ""
      } />

Would become:

<cfset apiResponse = structNew()>
<cfset apiResponse.success = true>
<cfset apiResponse.errors = arrayNew(1)>
<cfset apiResponse.data = "">
0
 
swaggrKAuthor Commented:
What about this line...
<cfset apiResponse.errors = [ cfcatch.message, cfcatch.detail ] />
0
 
_agx_Commented:
The "[...]" syntax is just shorthand for create an array

         <cfset apiResponse.errors = arrayNew(1)>

and append two values to it

         <cfset arrayAppend(apiResponse.errors, cfcatch.message)>
         <cfset arrayAppend(apiResponse.errors, cfcatch.detail )>
0
 
swaggrKAuthor Commented:
I believe this is the last one.

<cfset jsonResponse = serializeJSON( apiResponse ) />
0
 
swaggrKAuthor Commented:
The errror is "Variable SERIALIZEJSON is undefined."

<cfset jsonResponse = serializeJSON( apiResponse ) />
0
 
_agx_Commented:
Unfortunately serializeJSON wasn't introduced until CF8 either. You'll have to look for a similar function. I know there used to be one called CFJSON but I can't seem to find it right now.

BTW, most newer code assumes CF8+. So before you waste too much time w/it, always check the compatibility level 1st. It needs to be compatible w/CFMX.
0
 
swaggrKAuthor Commented:
Oh wow, thanks for the heads up. Do know of a polling solution that can be used in my CF server environment?
0
 
_agx_Commented:
No, most of the scripts I've seen require CF8+ too.  Here's one json encoder that says its CFMX compatible.

http://cfjson.riaforge.org/index.cfm?event=page.svnview&path=%2Ftrunk&file=cfjson.cfc

If yes, you could replace the serializeJSON() call with something like this:

           <cfset obj = createObject("component", "path.to.CFJSON")>
           <cfset jsonResponse = obj.encode( apiResponse ) />
0
 
swaggrKAuthor Commented:
Ok, now it is partially working. Except when you click on a number it does not change the result until you manually refresh the page

Test page:
http://chicagoeliteclassic.com/rating/index.cfm

Open in new window




index.cfm
<!--- Query for images to rate. --->
<cfquery name="image" datasource="#application.dsn#" username="#application.username#" password="#application.password#">
	SELECT
		i.id,
 
		<!--- Get the current rating for the image. --->
		(
			CASE
				WHEN
					COUNT( r.rating ) > 0
				THEN
					(
						SUM( r.rating ) /
						COUNT( r.rating )
					)
				ELSE
					0
			END
		) AS rating,
 
		<!--- Query for existing rating by user. --->
		COALESCE( er.id, 0 ) AS has_existing_rating
	FROM
		(
			SELECT 1 AS id UNION ALL
			SELECT 2 AS id UNION ALL
			SELECT 3 AS id
		) AS i
 
	<!--- Join this to the rating table to get rating. --->
	LEFT OUTER JOIN
		rating r
	ON
		i.id = r.image_id
 
	<!---
		Join this to the rating table AGAIN to see if the current
		user has already rated the given image.
	--->
	LEFT OUTER JOIN
		rating er
	ON
		(
				er.image_id = i.id
			AND
				er.ip_address = <cfqueryparam value="#cgi.remote_addr#" cfsqltype="cf_sql_varchar" />
			AND
				er.user_agent = <cfqueryparam value="#cgi.http_user_agent#" cfsqltype="cf_sql_varchar" />
		)
 
	GROUP BY
		i.id, r.image_id,er.id
	ORDER BY
		i.id ASC
</cfquery>
 
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<!---
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
--->
	<title>jQuery And ColdFusion Rating System Demo</title>
	<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
	<script type="text/javascript">
 
		// Define jquery plugin.
		jQuery.fn.rating = function( postUrl ){
			// Loop over each list to apply meta data.
			this.each(
				function( index, listNode ){
					var list = $( this );
					var metaData = list.find( "script.meta-data" );
 
					// Check to see if meta data was found.
					if (metaData.size()){
 
						// Apply meta data.
						list.data(
							"metaData",
							eval( "(" + metaData.html() + ")" )
							);
 
						// Remove the meta data node.
						metaData.remove();
 
					}
				}
				);
 
 
			// Initialize the links within the list.
			this.find( "a" )
				.attr( "href", "javascript:void( 0 )" )
				.click(
					function( clickEvent ){
						var link = $( this );
						var list = link.parents( "ul:first" );
						var metaData = list.data( "metaData" );
 
						// Post the rating.
						jQuery.ajax({
							type: "post",
							url: postUrl,
							data: {
								image_id: metaData.id,
								rating: link.text()
								},
							dataType: "json",
							success: function( apiResponse ){
								// Check to see if the API request
								// was valid.
								if (apiResponse.SUCCESS){
 
									// Replace the list with the
									// current rating.
									list
										.empty()
										.append(
											"<li>Rating: " +
											apiResponse.DATA.toFixed( 1 ) +
											"</li>"
											)
									;
 
								}
							}
							});
 
						// Cancel default event.
						return( false );
					})
			;
 
			// Return jQuery object for chaining.
			return( this );
		};
 
 
		// When the DOM is ready, initialize the plugin.
		$(function(){
			$( "ul" ).rating( "rate_image.cfm" );
		});
 
	</script>
	<style type="text/css">
 
		ul.rating {
			height: 20px ;
			list-style-type: none ;
			margin: 10px 0px 0px 0px ;
			padding: 0px 0px 0px 0px ;
			}
 
		ul.rating li {
			float: left ;
			margin: 0px 5px 0px 0px ;
			padding: 0px 0px 0px 0px ;
			}
 
		ul.rating a {
			background-color: #F0F0F0 ;
			border: 1px solid #333333 ;
			color: #333333 ;
			float: left ;
			height: 20px ;
			line-height: 20px ;
			text-align: center ;
			text-decoration: none ;
			width: 20px ;
			}
 
	</style>
</head>
<body>
 
	<h1>
		jQuery And ColdFusion Rating System Demo
	</h1>
 
	<cfoutput>
 
		<cfloop query="image">
 
			<div style="float: left ; margin-right: 20px ;">
 
				<img
					src="./images/girl#image.id#.jpg"
					width="165"
					style="display: block ;"
					/>
 
				<!--- Check to see if user has rated yet. --->
				<cfif image.has_existing_rating>
 
					<!---
						User has already rated, just show the
						current rating.
					--->
					<ul class="rating">
						<li>
							Rating: #numberFormat(
								image.rating,
								"0.0"
								)#
						</li>
					</ul>
 
				<cfelse>
 
					<!--- Show the rating options. --->
					<ul class="rating">
						<!---
							Set up the meta-data for this image.
							This data will be applied when the
							rating plugin is initialized.
						--->
						<script
							type="application/x-json"
							class="meta-data">
							{
								id: #image.id#
							}
						</script>
						<li>
							<a>1</a>
						</li>
						<li>
							<a>2</a>
						</li>
						<li>
							<a>3</a>
						</li>
						<li>
							<a>4</a>
						</li>
					</ul>
 
				</cfif>
 
			</div>
 
		</cfloop>
 
	</cfoutput>
 
</body>
</html>

Open in new window


rate_image.cfm

 <cfset apiResponse = structNew()>
<cfset apiResponse.success = true>
<cfset apiResponse.errors = arrayNew(1)>
<cfset apiResponse.data = ""> 
 
<!--- Try to execute the api request / response. --->
<cftry>
 
	<!--- Param the FORM variable. --->
	<cfparam name="form.image_id" type="numeric" />
	<cfparam name="form.rating" type="numeric" />
 
 
	<!---
		Check to see if this user has already rated this image.
		We do not want to allow duplicate ratings.
	--->
	<cfquery name="existingRating" datasource="#application.dsn#"  username="#application.username#" password="#application.password#">
		SELECT
			r.id
		FROM
			rating r
		WHERE
			r.image_id = <cfqueryparam value="#form.image_id#" cfsqltype="cf_sql_integer" />
		AND
			r.ip_address = <cfqueryparam value="#cgi.remote_addr#" cfsqltype="cf_sql_varchar" />
		AND
			r.user_agent = <cfqueryparam value="#cgi.http_user_agent#" cfsqltype="cf_sql_varchar" />
	</cfquery>
 
 
	<!--- Check to see if the rating exists. --->
	<cfif existingRating.recordCount>
 
		<!--- Add error. --->
		<cfset arrayAppend(
			apiResponse.errors,
			"You have already rated this image."
			) />
 
	</cfif>
 
 
	<!--- Check to see if we have any errors. --->
	<cfif NOT arrayLen( apiResponse.errors )>
 
		<!--- Insert new rating. --->
		<cfquery name="insertRating" datasource="#application.dsn#" username="#application.username#" password="#application.password#">
			INSERT INTO rating
			(
				ip_address,
				user_agent,
				rating,
				date_created,
				image_id
			) VALUES (
				<cfqueryparam value="#cgi.remote_addr#" cfsqltype="cf_sql_varchar" />,
				<cfqueryparam value="#cgi.http_user_agent#" cfsqltype="cf_sql_varchar" />,
				<cfqueryparam value="#form.rating#" cfsqltype="cf_sql_integer" />,
				<cfqueryparam value="#now()#" cfsqltype="cf_sql_timestamp" />,
				<cfqueryparam value="#form.image_id#" cfsqltype="cf_sql_integer" />
			);
 
			<!--- Get the new overall rating. --->
			SELECT
				(
					SUM( r.rating ) /
					COUNT( r.rating )
				) AS overall_rating
			FROM
				rating r
			WHERE
				r.image_id = <cfqueryparam value="#form.image_id#" cfsqltype="cf_sql_integer" />
			;
		</cfquery>
 
 
		<!--- Set the current rating as the response data. --->
		<cfset apiResponse.data = insertRating.overall_rating />
 
	</cfif>
 
 
	<!--- Catch any api errors. --->
	<cfcatch>
 
		<!--- Set the error in our api response object.---> 
		<!---
		<cfset apiResponse.errors = [ cfcatch.message, cfcatch.detail ] />
 --->
  <cfset arrayAppend(apiResponse.errors, cfcatch.message)>
         <cfset arrayAppend(apiResponse.errors, cfcatch.detail )> 
	</cfcatch>
</cftry>
 
 
<!--- Check to see if we have any errors at this point. --->
<cfif arrayLen( apiResponse.errors )>
 
	<!--- Flag the API request as unsuccessful. --->
	<cfset apiResponse.success = false />
 
</cfif>
 
 
<!--- Searialize the API response into our JSON value. --->
<!---
<cfset jsonResponse = serializeJSON( apiResponse ) />
 --->
 
   <cfset obj = createObject("component", "path.to.CFJSON")>
           <cfset jsonResponse = obj.encode( apiResponse ) /> 
 
 
 
<!--- Convert the response string to binary for streaming. --->
<cfset binaryResponse = toBinary( toBase64( jsonResponse ) ) />
 
 
<!--- Stream the binary data back. --->
<cfheader
	name="content-length"
	value="#arrayLen( binaryResponse )#"
	/>
 
<cfcontent
	type="text/x-json"
	variable="#binaryResponse#"
	/>

Open in new window

0
 
_agx_Commented:
Did you test the http://chicagoeliteclassic.com/rating/rate_image.cfm script manually? Because it's still returning an http 500 error. That's why it's not refreshing.  If you call the page directly it says:

       Could not find the ColdFusion Component path.to.CFJSON.

"path.to.CFJSON" is just a placeholder. Replace it with the real path where you saved the CFJSON.cfc file:

      c:\cfusionmx\wwwroot\              <=== if this is your web root and ...
      c:\cfusionmx\wwwroot\yourComponents\CFJSON.cfc    <=== you the cfc is here
      "yourComponents.CFJSON"   <=== this is the dot notation path

ie   <cfset obj = createObject("component", yourComponents.CFJSON")>
0
 
swaggrKAuthor Commented:
Interesting...
...I do not have a .cfjson file.

One was not provided at

http://www.bennadel.com/blog/1629-Ask-Ben-Building-A-jQuery-And-ColdFusion-Rating-System.htm

Open in new window

0
 
_agx_Commented:
It's not part of that site. Remember that example assumes you're running CF8+, which has JSON functions builtin. Your version doesn't. So you have to create your own.

The link I gave earlier is for a free cfc that does JSON encoding under CFMX.  You need to download the CFC first. Save it somewhere in your web directory as CFJSON.cfc

http://cfjson.riaforge.org/index.cfm?event=page.svnview&path=%2Ftrunk&file=cfjson.cfc

Then you can use this code. Just remember to change "path.to.CFJSON"

           <cfset obj = createObject("component", "path.to.CFJSON")>
           <cfset jsonResponse = obj.encode( apiResponse ) />
0
 
swaggrKAuthor Commented:
I have placed the cfjson.cfc file within the same directory but now when I attempt to nagivate to "chicagoeliteclassic.com/rating/rate_image.cfm" it wants to download the page and

<cfset obj = createObject("component", "chicagoeliteclassic.rating.CFJSON")>
0
 
_agx_Commented:
That's actually a good thing. The example uses <cfcontent> to return the JSON response. So when viewing it directly produces a download prompt. If you want to view the response, just click "open with".. notepad.

Anyway, you'll need to fix one more thing. Like I mentioned, the example assumes you're using CF8+ which has an annoying JSON bug. It automatically converts all structure keys to upper case. CFJSON.cfc doesn't do that.  So you'll need to fix the case of the javascript variables.

Change these:

            apiResponse.SUCCESS
                apiResponse.DATA

to this instead:

            apiResponse.success
                apiResponse.data
0
 
swaggrKAuthor Commented:
Ok, that worked! Great!  My last question is how can I have it so it gives the total of the clicks instead of the average.
0
 
_agx_Commented:
I can test it, but I think this part creates the average.

                <!--- Get the new overall rating. --->
            SELECT
                        (
                              SUM( r.rating ) /
                              COUNT( r.rating )
                        ) AS overall_rating
                  ....

So getting rid of the SUM should do it

             ie   SELECT COUNT( r.rating ) AS NumberOfClicks ....
0
 
swaggrKAuthor Commented:
Great! Everything is working perfectly, but I need to add more images to rate.
Currently it only shows 3 images to rate, but i would like to show up
to 8.
0
 
swaggrKAuthor Commented:
I've figured out how to add more images.

My only remaining issue is how would I add individual names to reach option.

http://chicagoeliteclassic.com/rating/index.cfm

Open in new window

0
 
_agx_Commented:
Not sure exactly what you mean.

Since that's a new question, you should open a new thread. That'll draw attention from other experts too, so you won't be waiting on me if I'm not around :)
0
 
swaggrKAuthor Commented:
http://chicagoeliteclassic.com/rating/index.cfm

Open in new window

0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 14
  • 13
Tackle projects and never again get stuck behind a technical roadblock.
Join Now