Skip to main content
Inspiring
June 16, 2011
Answered

datagrid: I can trace the data BUT the datagrid doesn't show it

  • June 16, 2011
  • 2 replies
  • 986 views

Hi

Hi - I have traced the data via php from mysql in actionscript but the datagrid doesn't show it. Here is my code.

import fl.controls.DataGrid;

import fl.controls.dataGridClasses.DataGridColumn;

import fl.data.DataProvider;

import fl.controls.ScrollPolicy;

import fl.managers.StyleManager; //needed to format text in cells

import flash.display.MovieClip;

import flash.net.URLRequest;

import flash.events.Event;

import flash.events.MouseEvent;

import flash.net.URLLoader;

import flash.net.URLVariables;

import flash.net.URLRequestMethod;

import flash.net.URLLoaderDataFormat;

// Create a new DataGrid component instance.

var aDg:DataGrid = new DataGrid();

var myDp:Array = new Array();

var myData:URLRequest = new URLRequest("http://www.cambridgekids.es/kglad/php/getUsers.php");

myData.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader();

loader.dataFormat = URLLoaderDataFormat.VARIABLES;

loader.addEventListener(Event.COMPLETE, dataOnLoad);

loader.load(myData);

function dataOnLoad(evt:Event) {

for (var i:uint=0; i<evt.target.data.cant; i++) {

myDp.push({Nombre:evt.target.data["firstname"+i], Apellido:evt.target.data["lastname"+i]});

//trace(evt.target.data["firstname"+i]);

}

myDp.dataProvider = new DataProvider(myDp);

}

aDg.dataProvider = new DataProvider(myDp);

aDg.columns = ["Nombre", "Apellido" ];

aDg.setSize(800,300);

aDg.move(150,200);

aDg.rowHeight = 40;// Allows for 2 lines of text at default text size.

aDg.columns[0].width = 80;

aDg.columns[1].width = 30;

aDg.resizableColumns = true;

aDg.verticalScrollPolicy = ScrollPolicy.AUTO;

addChild(aDg);

This topic has been closed for replies.
Correct answer Lee_Burrows

doh! i thought i was in the flex forum so ignore my previous response.

you need to set the dataProvider on the dg after you fill the dataprovider with data so move aDg.dataProvider = new DataProvider(myDp); to the end of the dataOnLoad method

2 replies

Inspiring
June 17, 2011

SO MANY THANKS

I was suffering because I knew almost all the code was correct. Know I am able to connect mysql databases through php to flash - It's so powerful.

Cheers

Inspiring
June 20, 2011

Hi Lee - sorry to be a pain BUT I am missing the first element ie: cant=0 in firstname only - really weird

I load around 300 names and surnames etc... into the datagrid. Everything works fine BUT the firstname doesn't appear for the first element alone. It DOES for the others. Even more strange is that when I change the variable in the loop to 1 instead of 0 - ALL the elements are present.

The php code is as follows:

cut short to:

$s = "SELECT * FROM ".$tableName. " WHERE school='".$school."'";
$result = mysql_query($s);
$cant = 0;
    while($row=mysql_fetch_array($result)){
        echo "firstname$cant=$row[firstname]&lastname$cant=$row[lastname]&password $cant=$row[password]&mobile$cant=$row[mobile]&email$cant=$row[email]&" ;
        $cant++;
}
echo "cant=$cant";         
mysql_close($link);
?>

ACTIONSCIRPT FUNCTION

function dataOnLoad(evt:Event) {
   
    for (var i:uint=0; i<evt.target.data.cant; i++) {
        this.myLoadedData.push({firstname:evt.target.data["firstname"+i], lastname:evt.target.data["lastname"+i], password:evt.target.data["password"+i],

         mobile:evt.target.data["mobile"+i], email:evt.target.data["email"+i]});
    }
    aDg.dataProvider = new DataProvider(myLoadedData);
}

Lee_Burrows
Participating Frequently
June 20, 2011

sorry. my php knowledge is minimal

Lee_Burrows
Participating Frequently
June 17, 2011

hi

try changing (eg)

myDataGrid.dataProvider = new DataProvider(myData);

to

myDataGrid.dataProvider = new ArrayList(myData);


Inspiring
June 17, 2011

Thank you Lee

I get the error that ArrayList is undefined - do I have to import a flash library - I don't know which one - looks like ArryList is old stuff.

Also, most tutorials use the dataprovider. Is there any way of making this work with the dataprovider - I can change anything you want. This seems very simple BUT I just don't know what is wrong.

Cheers

Lee_Burrows
Lee_BurrowsCorrect answer
Participating Frequently
June 17, 2011

doh! i thought i was in the flex forum so ignore my previous response.

you need to set the dataProvider on the dg after you fill the dataprovider with data so move aDg.dataProvider = new DataProvider(myDp); to the end of the dataOnLoad method