PDA

View Full Version : Flash & CFML


billythehamster
04-15-2003, 09:39 PM
Hi

I am new to these forums. I have a query and I was wondering if someone out there would be able to help.

I know Flash really well (apart from Actionscipting which I have a basic knowledge of). I also know CFML really well. However I am having difficulty getting them to talk to each other.

I am using MX for both of them and here is the problem.

I have a database full of news content. The fields are ID, TITLE, CONTENT, IMAGENAME, IMAGETYPE. I would like to run a query on the database from flash that will return a list of all the titles. Then when I click the TITLE I want the full article (CONTENT) to appear in a seprate windows (pref Java Script Popup) with an image in the article.

It's a doddle to do this in with CFM pages but I haven't got a clue in Flash. I have crawled the net trying to find a tutorial but there doesn't seem to be any good ones for Flash CFM.

Please help.

adi_ady_ade
04-18-2003, 12:14 PM
Hi BH, I'm a CF programmer too and I've started down this same route too. Try the following...

First write a cfc...


<cfcomponent>

<cffunction name="helloPeople" returns="string" access="remote">

<cfreturn "Hello People">

</cffunction>

</cfcomponent>


Then in your .fla file...


#include "NetServices.as"
#include "NetDebug.as"

var myString;

function Result()
{
this.onResult = function(result)
{
myString = result;
trace(myString);
}

this.onStatus = function(error)
{
trace("Error : " + error.description);
}
}

NetServices.setDefaultGatewayUrl("http://localhost/flashservices/gateway");
var gw = NetServices.createGatewayConnection();
var server = gw.getService("path to cfc", new Result());
server.helloWorld();



This is just a simple test, once you have this up and running try putting a query or SP in the cfc. You'll then need to look into using the ResultSet object in flash.

BTW, if you get an error trying to include the two .as files at the top of the actionscript, you'll probably need to install this:
http://www.macromedia.com/software/flashremoting/downloads/components/

Let me know how you get on.

Ade

retrotron
04-26-2003, 03:43 PM
Since you're a CFMLer, try looking at the chapter on ColdFusion and Flash in Forta's "ColdFusion MX Application Development". It's very helpful re how to get a query from ColdFusion into ActionScript.

The easiest way to get a query into an ActionScript RecordSet is to create a .cfm page that runs the query and then sets a variable called "flash.result" equal to the name of the query:

<cfquery name="myQuery" datasource="#myDatasource#">
SELECT *
FROM myTable
</cfquery>

<cfset flash.result = myQuery>


Then include this actionscript in your Flash movie:

#include "NetServices.as"


// --------------------------------------------------
// Application initialization
// --------------------------------------------------

if (inited == null) { // the parser will prefrom this code only once
inited = true;
// the URL must be changed to whatever your FlashRemoting Gateway is
NetServices.setDefaultGatewayUrl("http://localhost:8500/flashservices/gateway");
gateway_conn = NetServices.createGatewayConnection(); // connect to the gateway
myService = gateway_conn.getService("flash.news", this);
// "myService" is a reference to a specific directory on your web server. In this
// case it is "flash/news/" (written in ActionScript as "flash.news"),
// but it can be whatever you want
} // end app initialization

// --------------------------------------------------
// Handlers for user interaction events
// --------------------------------------------------

// This gets called when the "myBtn" button is clicked
function myBtn_Clicked() {
// ... put code here
// For example, you could call "runQuery.cfm" page like this:
myService.runQuery();
// Flash sees .cfm pages as a "service", so it calls them by name
// as if they were functions. "myService" refers to the connection
// to the ColdFusion Flash Remoting Gateway established above.
} // end myBtn_Clicked()

// --------------------------------------------------
// Handlers for data coming in from server
// --------------------------------------------------

// This gets called Flash receives the results of runQuery.cfm.
function runQuery_Result(result) {
// ... put code here, it can be anything:
// a loop going through your records,
// or maybe simply call a function which will then
// loop through and display all your records.
} // end runQuery_Result()

// stop the movie from playing
stop();


As you can see, the first bit of code establishes a connection (via the Flash Remoting Gateway) to a specific directory on your web server (in this case, "/flash/news/", but it can be whatever you want it to be) which contains the .cfm files. That connection is named "myService". To connect to any .cfm page, you simply write "myService.myCFMLPage();". The parser will then go to the directory specified in "myService" and look for "myCFMLPage.cfm".

You can also include variables inside the brackets if you want: "myService.myCFMLPage({thisDate:now});", where "thisDate" is the name of the variable (in your .cfm page you can access the variable with the name "flash.thisDate") and "now" is the value of that variable. Separate multiple name-value variable pairs with a single comma.

Then the "myBtn_Clicked()" function calls the .cfm page, and the result is returned by the same function name with "_Result(result)" added onto the end.

Since you set flash.result equal to a query in your .cfm page, the result is automatically transformed into a Flash RecordSet object. Now you can use ActionScript to access all the values:

for (i=0; i < result.getLength(); i++) { // loop through all records
var thisRecord = result.getItemAt(i);
trace(thisRecord.ID);
trace(thisRecord.TITLE);
trace(thisRecord.CONTENT);
// etc.
// you could include a button that when clicked loads a
// javascript popup window with thisRecord.CONTENT
} // end looping through records

This is by far the simplest way of working with ColdFusion queries. If you're just working with News, then all the manipulation of the query RecordSet could be carried out in ActionScript and the page would probably be very fast.

Hope some of this helps. Re your question about Flash and CFML, try http://www.flashCFM.com.

Pax et bonum,
retrotron