PDA

View Full Version : AMFPHP to RecordSet in Actionscript 3.0?


Groady
03-26-2008, 02:44 AM
Can someone explain to me how to retrieve a RecordSet in an Actionscript 3.0 based file from AMFPHP.

The below example is taken from the homepage at http://www.amfphp.org/. It's showing how to retrieve a MySQL query directly from AMFPHP without first having to convert it to a string.

As you can see flash is instanciating a RecordSet to catch the returned MySQL query. Is there an equivalent to doing this in Actionscript 3.0 as RecordSet seems to be an Actionscript 2.0 class only.

Sorry if this is a stupid question.


<?php
class pizzaService {
var $ordertable = "amfphp_orders"; // the orders table
var $pizzatable = "amfphp_pizzas"; // the pizzas table
/* mysql_connect and mysql_select_db are in the constructor */
function getOrderList ()
{
$sql = "SELECT o.order_id as orderid, o.order_status as status, o.order_name as name, p.pizza_id as pizzaid, p.pizza_details as details, p.pizza_quantity as quantity FROM $this->ordertable o, $this->pizzatable p WHERE o.order_id = p.order_id AND o.order_status=1 ORDER BY o.order_time";
return mysql_query($sql);
}

/* Other methods below */}
?>



import mx.remoting.*;
import mx.rpc.*;

var gatewayUrl:String = "http://amfphp.org/amfphp/gateway.php";
service = new Service(gatewayUrl, null, "pizzaService");

var pc:PendingCall = service.getOrderList();
pc.responder = new RelayResponder(this, "handleGetOrderList", null);

function handleGetOrderList(re:ResultEvent)
{
var rs:RecordSet = RecordSet(re.result);
for(var i = 0; i < rs.length; i++) {
var item = rs.getItemAt(i);
//item is an object with keys orderid, status, etc.
}
}

hooj
03-29-2008, 12:33 AM
Try this solution

copy the following code into a new AS file called 'RecordConverter.as':


package amfphp{

public class RecordConverter {

private static var _trace:Boolean = true;

public static function convertToArray(amfObject:Object):Array {
var dump=amfObject.serverInfo;

(_trace) ? traceDump(dump) : null;

var arr = new Array();

var colNames=dump.columnNames;
var totalRecords = dump.totalCount;
var colCount=colNames.length;
var rawData=dump.initialData;

for (var r=0; r < totalRecords; r++) {
var data = rawData[r];
var obj=new Object;
for (var p=0; p < colCount; p++) {
obj[colNames[p]]=data[p];

}
arr.push(obj);
}
return arr;
}
public static function traceDump(dump:Object):void {
trace("************** DUMP INFO *************");
for (var props in dump) {
trace(props + " : " + dump[props]);
}
trace("************** END DUMP INFO *************");
}
public static function set traceMode(val:Boolean):void {
_trace = val;
}
}
}


Please note the package name at the top, make sure that will match up with wherever you'll be accessing the class from.

Now, when you get back your result object from the server try this:


function handleGetOrderList(re:ResultEvent)
{
var arr = RecordConverter.convertToArray(re);
var dp = new DataProvider(arr);
for(var i = 0; i < dp.length; i++) {
var item = dp.getItemAt(i);
//item is an object with keys orderid, status, etc.
}
}

dropthat50footer
05-30-2008, 09:55 PM
just wondering what you ended up doing to solve this problem? I'm currently porting over an app from AS 2 to AS 3 and while the above solution looks doable, there must be a shorter solution to this.

dropthat50footer
05-30-2008, 10:36 PM
Well, I had a look around, and it seems that AMF has chosen not to implement their own conversion utility for some reason, it appears that a custom class like above is the only solution I've found so far. I guess it would also be possible to convert the data within php first so as to pass an array back rather than the php resource object that is returned from the mysql query. I tried out hooj's class and it seems to work well.

bowljoman
06-21-2008, 07:27 PM
whats wrong using with fetch_object?


while ($row = mysql_fetch_object($res,"Channel"))
return( $row );




public function getChannelList(msg:Object):void {
var i:String,id:String,name:String;
var j:int=0;
for (i in msg) {
id=msg[i].id;

}
}

Chris@MSM
07-24-2008, 03:26 PM
The fetch method only returns one row of data, what if your query returns muliple rows?

Hooj's type of solution will work with multiple rows.

bowljoman
07-24-2008, 03:54 PM
I kept it simple, but it obviosly works like any other returned variable. No mystery here...



$ret=array();


while ($row = mysql_fetch_object($res))
array_push($ret,$row);
return ($ret);