Well Done. :)

-----Original Message-----
From: Peter Boughton [mailto:bought...@gmail.com] 
Sent: Thursday, June 25, 2009 5:54 PM
To: cf-talk
Subject: Re: PHP to CF pseudo code...


Not sure it's how I'd write a rating widget, but here's a fairly direct
translation.

(Untested, so may contain stupid bugs.)


<cffunction name="in_range" returntype="Numeric" output="false">
        <cfargument name="Val"  type="Numeric" />
        <cfargument name="From" type="Numeric" default="0" />
        <cfargument name="To"   type="Numeric" default="100" />

        <cfreturn min( Arguments.To , max( Arguments.From , Arguments.Val )
) />
</cffunction>

<cffunction name="get_dbfile" returntype="string" output="false">
        <!--- I *think* __FILE__ from original is a global constant... not
really sure? I've plonked it into Application --->
        <cfreturn rereplace( Application.File , '\.php$' , '.dat' ) />
</cffunction>


<cffunction name="get_votes" returntype="Struct" output="false">
        <cfset var dbfile = get_dbfile() />
        <cfif FileExists(dbfile)>
                <cfreturn deserializeJson( FileRead(dbfile) )/>
        <cfelse>
                <cfreturn { votes : 0 , sum : 0 , avg : 0 } />
        </cfif>
</cffunction>


<cffunction name="save_vote" returntype="Struct" output="false">
        <cfargument name="Vote" type="Numeric" />

        <cfscript>
                var db = get_votes() ;
                db.votes++ ;
                db.sum += Arguments.Vote ;
                db.avg = round( 100 * db.sum / db.votes ) / 100 ;
                FileWrite( get_dbfile() , serializeJson( db ) ) ;
        </cfscript>

        <cfreturn db />
</cffunction>


That needs CF8 or Railo 3.

If you've got Railo, you can do serialize/deserialize without using JSON,
and also use the terniary if operator (as the PHP version does).




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:323967
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4

Reply via email to