Jump to content

php generated XML into flash


dub_beat

Recommended Posts

Hi,

 

I have a php script that generates an xml file from a mysql database with a list of media. I want to get flash to read the generated xml and store the data in objects to be used later.

 

How do I read the xml data with flash?

 

Ive attach my code in 3 stages, the PHP, the Flash and the XML file which the PHP generates.

 

 

 

Cheers

(CRS)

 

Attach Code

 

<?php

if(!$dbconnect = mysql_connect('localhost', 'root', 'admin')) {

  echo "Connection failed to the host 'localhost'.";

  exit;

}

if (!mysql_select_db('products')) {

  echo "Cannot connect to database 'test'";

  exit;

}

 

$table_id = 'images_xml_test';

$query = "SELECT * FROM $table_id";

$dbresult = mysql_query($query, $dbconnect);

 

// create a new XML document

$doc = new DomDocument('1.0');

 

// create root node

$root = $doc->createElement('media');

$root = $doc->appendChild($root);

$count=0;

while($row = mysql_fetch_assoc($dbresult)) {

$count++;

// add node for each row

$element=image;

  $occ = $doc->createElement($element);

  $occ = $root->appendChild($occ);

  // add a child node for each field

  foreach ($row as $fieldname => $fieldvalue) {

  $child = $doc->createAttribute($fieldname);

    $child = $occ->appendChild($child);

 

$value = $doc->createTextNode($fieldvalue);

    $value = $child->appendChild($value);

  } // foreach

} // while

 

// get completed xml document

$xml_string = $doc->saveXML();

 

 

print $xml_string;

?>

//////////////////////////////////////////////////////

My failed flash attempt

**************************************************

 

var myXML:XML = new XML();

myXML.ignoreWhite = true;

myXML.onLoad = function(success:Boolean):Void  {

if (success) {

var mediaNode:XMLNode = this.firstChild;

if (mediaNode.hasChildNodes()) {

var imageArray:Array = new Array();

for (var i:Number = 0; i != mediaNode.childNodes.length; i++) {

var node:XMLNode = mediaNode.childNodes;

var obj:Object = new Object();

obj.id=node.attributes.label;

obj.label = node.attributes.label;

obj.data = node.attributes.data;

imageArray.push(obj);

}

}

list.dataProvider = imageArray;

}

};

theXML.load("http://localhost/products/fetch_assoc1.php");

 

//**********************************************************************

 

//the structure of my xml that the php has generated

//*********************************************

 

<?xml version="1.0"?>

<media>

<image id="1" url="images/aStudyInTexture.jpg" label="StudyInTexture"/>

<image id="2" url="images/buntzenWinter.jpg" label="buntzenWinter"/>

<image id="3" url="images/flowerInDetail.jpg" label="flowerInDetail"/>

<image id="4" url="images/flowerInDetail.jpg" label="flowerInDetail"/>

</media>

Link to comment
https://forums.phpfreaks.com/topic/37243-php-generated-xml-into-flash/
Share on other sites

hey cheers for the link mate,

 

looks like a good reference point.

 

it turns out the problem with the code was I had

 

theXML.load("http://localhost/products/fetch_assoc1.php");

 

where I should of had

myXML.load("http://localhost/products/fetch_assoc1.php"); :)

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.