About using Flex, dataproviders and AMFPHP

Hello for all!

(Apologies if this is wrong place to post this kind of stuff for I’m a newcomer and didn’t really find any other place where to discuss of Flex. Feel free to move this thread in more appropriate place if you like.)

I have a Flex application that uses AMFPHP to get and add data to and from database. The application displays the data in datagrid, but I would like to display it also in label or textarea. Anyway the real problem here is to delete the information from database.

I have a database with 3 colums, “id” (primary key, unique), “company” and “ceo”. The amfphp fetches all the companies I have in the database and arrays it. Then flex shows it in the datagrid. Now I click one of the companies in the datagrid and the flex enables the “delete”-button I have made. So what I want is that when I click delete, Flex would send the id of the clicked instance to the amfphp, which in its turn would find the id from the database and delete the company in question. (I have managed to delete the company from the array that I use as a dataProvider for the datagrid, but as I click the button “fetch the data from database”, the datagrid of course displays also the “deleted” information again).

As I have understood, actionscript uses native actionscript objects when getting the data into a datagrid. So I would need a method to get the id into a string (or int) and then send this string to the amfphp. If you have any easier method for this, please feel free to share it with me, but I still would like to know if there is a way to get the data into a label or textarea (I’m little irritated about the fact that there is no easy way of doing that).

The same thing goes for editing the data. If some or all fields are edited and the refresh-button is clicked, flex has to convert the objects into strings and then send them to amfphp, right?

The other question I have involves importing the fl-based libraries (such as fl.data.DataProvider). When using these I get an error “1172: Definition fl.data:DataProvider could not be found.” I know that in flash you should add the component using the dataprovider into your library, but what about flex that doesn’t have library (or does it)?

Here’s the actionscript/mxml code. I’m Finnish, so apologies for the variable names. There probably is some extra code I have used for debugging purposes, but hopefully people don’t mind.

<?xml version="1.0" encoding="utf-8"?>


<!--creationComplete="initApplication()" -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="*" layout="absolute">    
    <mx:Label text="Yrityksen nimi" x="13" y="33"/>
    <mx:TextInput id="yritysNimi" editable="true" x="193" y="31"/>
    <mx:Label text="Yrityksen toimitusjohtaja" x="13" y="74"/>
    <mx:TextInput id="yritysCEO" x="193" y="72"/>
    <mx:Button label="Lähetä tiedot" click="lisaaYritys()" x="193" y="121"/>
    <mx:DataGrid id="dg" dataProvider="{dataProvider}" click="showIndex()" width="542" x="10" y="168" editable="true">
        <mx:columns>
            <mx:DataGridColumn id="yritysid" headerText="Yrityksen id" editable="false" dataField="yritykset_id"/>
            <mx:DataGridColumn headerText="Yrityksen nimi" dataField="nimi"/>
            <mx:DataGridColumn headerText="Yrityksen toimitusjohtaja" dataField="ceo"/>
        </mx:columns>
    </mx:DataGrid>
    <mx:Button label="Hae tiedot tietokannasta" x="10" y="342" click="initApplication()"/>
    <!--<mx:Button x="102" y="92" label="insert" id="lisaa" click="insertRecord()"/> -->
    
    <mx:Label x="10" y="387" id="tietokentta" width="313"/>
    <mx:Label x="345" y="387" text="Yrityksen nimi" id="laitettuYritys"/>
    <mx:Label x="449" y="387" text="CEOn nimi " id="ceonnimi"/>
    
    
<mx:Script>
<![CDATA[

import flash.net.Responder;
import flash.net.NetConnection;
import fl.data.DataProvider;


[Bindable]
public var dataProvider:Array;
public var datantuoja:Array;
public var gateway : RemotingConnection;
//public var daattaProviideri:DataProvider;



public function initApplication()
{
    gateway = new RemotingConnection( "http://localhost/amfphp/gateway.php" );
    gateway.call( "yritykset.getYritykset", new Responder(onResult, onFault));
}

public function lisaaYritys():void{
    var netticonnu:NetConnection=new NetConnection();
    netticonnu.connect("http://localhost/amfphp/gateway.php");
    netticonnu.call("yritykset.lisaaYritys", new Responder(laitettu, eiLaitettu), yritysNimi.text, yritysCEO.text);
    
}

public function onResult( result : Array ) : void
{
    dataProvider = result;
    tietokentta.text=dataProvider[0].toString();
}
public function onFault( fault : String ) : void
{
    trace( fault );
}
public function laitettu(result : Array):void{
    tietokentta.text="Onnistunut käynti";
    laitettuYritys.text=yritysNimi.text;
    ceonnimi.text=yritysCEO.text;
    datantuoja=result;
}
public function eiLaitettu(fault : String):void{
    tietokentta.text="käynti ei onnistunut";

}

public function showIndex():void{
    //tietokentta.text=dg.selectedIndex.toString();
    poistaButton.enabled=true;
    tietokentta.text=id
}

public function poista():void{
    //daattaProviideri
    dataProvider.splice(dg.selectedIndex, 1);
    dg.dataProvider=dataProvider;

}

]]>
</mx:Script>
    <mx:Button click="poista()" id="poistaButton" enabled="false" x="487" y="342" label="Poista"/>
    
</mx:Application>

And the php:

<?php
// Create new service for PHP Remoting as Class
class yritykset
{
function yritykset ()
{
// Define the methodTable for this class in the constructor
$this->methodTable = array(
"getYritykset" => array(
"description" => "Return a list of companies",
"access" => "remote"
)
);
}



function getYritykset(){
    $mysql=mysql_connect(localhost, "root", "");
    mysql_select_db("tyoaika");
    $query="SELECT * FROM yritykset";
    $result=mysql_query($query);
    while($row=mysql_fetch_object($result)){
    $arrayofusers[] = $row;
    }
    return($arrayofusers);
}


function lisaaYritys($yritys, $ceo){
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");


    $mysql=mysql_connect(localhost, "root", "");
    mysql_select_db("tyoaika");
    $query="INSERT INTO yritykset(nimi, ceo) VALUES ('".$yritys."', '".$ceo."')";
    $result=mysql_query($query);
    $stringData = $query; //"Bobby Bopper
";
    fwrite($fh, $stringData);
    /*$stringData = $ceo; //"Tracy Tanner
";
    fwrite($fh, $stringData); */
    fclose($fh);
    
    
    $palautettava['yritys']=$yritys;
    $palautettava['ceo']=$ceo;
    if(mysql_affected_rows()>0){
    return (object)$palautettava;

    //return "Päivitetty onnistuneesti";
    }/*else{
    return "Päivitys ei onnistunut";
    }*/
    //INSERT INTO taulu(sarake1, sarake2, sarake3) VALUES (arvo1, arvo2, arvo3, jne). 

}


//http://flash-creations.com/notes/servercomm_remoting_amfphp.php
function insertScore($scoreData) {
      mysql_query("INSERT INTO highscores VALUES (NULL,
          '".addslashes($scoreData['nickname'])."', 
          '".$scoreData['dateposted']."', 
          '".$scoreData['score']."')");
      return mysql_insert_id();
   }



/*
function getUsers () {
$mysql = mysql_connect(localhost, "root", "");
mysql_select_db( "testi" );
//return a list of all the users
$Query = "SELECT * from users";
$Result = mysql_query( $Query );
while ($row = mysql_fetch_object($Result)) {
$ArrayOfUsers[] = $row;
}
return( $ArrayOfUsers );
}*/
}
?>

Thanks in advance,
Occultica