Current location: Hot Scripts Forums » General Web Coding » Flash & ActionScript » Flash & XML...


Flash & XML...

Reply
  #1 (permalink)  
Old 09-20-06, 09:19 AM
scoop scoop is offline
New Member
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Flash & XML...

Hello all,

This may be a dumb question - I haven't used Flash for a very (very) long time. I've been tasked with seeing if it's possible to implement a web service called Postcode Anywhere into a Flash application.

I have php sample code (supplied by the provider) ... looking it over, it appears to simply send a request to their server [containing access keys and the actual postcode]. Their server generates an XML file of results which we read and parse for the addresses... Take a look at the code and tell me if I got this right or wrong for starters.

So the question is, can this php script be emulated in Flash? Will we run into problems with 'urlencode()' or anything else ... does Flash have a matching function? Is that even relevent?

I'm digging from the ground up, as you can see...so any insight is appreciated...

Cheers,


php code:
Code:
<html>
<head>
	<title>Postcode Anywhere Address Finder</title>
</head>
<body>

<?php

/*
  Note: This script has been developed for .php version 4.2.3.
  .php version 4.3 has more options to parse xml properly with slightly modified functions.

  This script will let you lookup an address from a postcode.
  
  !!NOTE: Since this example uses the XML service, fields will only be available in the results
  if a value is present i.e. they are not null. If this causes problems the Recordset service
  can be used instead - just modify the PrepareData function below to strip out the different
  characters in the response.
*/

//enter you account code and license key
$ACCOUNTCODE = "AAAAA11111";
$LICENSEKEY = "AA11-AA11-AA11-AA11";

function ByPostcode($SearchPostcode){

   global $ACCOUNTCODE,$LICENSEKEY;
   
   /* Build up the URL to send the request to. */
   $sURL = "http://services.postcodeanywhere.co.uk/xml.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=lookup";
   $sURL .= "&type=by_postcode";
   $sURL .= "&postcode=" . urlencode($SearchPostcode);
   
   PrepareData($sURL);

}

function FetchAddress($AddressID){
   
   global $ACCOUNTCODE, $LICENSEKEY;
   
   /* Build up the URL to request the data from. */
   $sURL = "http://services.postcodeanywhere.co.uk/xml.aspx?";
   $sURL .= "account_code=" . urlencode($ACCOUNTCODE);
   $sURL .= "&license_code=" . urlencode($LICENSEKEY);
   $sURL .= "&action=fetch";
   $sURL .= "&style=simple";
   $sURL .= "&id=" . $AddressID;

   PrepareData($sURL);

}

function PrepareData($URL){
   
   global $Data;
   
   /* Open the URL into a file */
   $ContentsFetch=file("$URL");

   foreach ($ContentsFetch as $line_num => $line) {
      if (strpos($line,"<Item ")!=false) { $Contents[]= $line;}
   }

   for ($i=0;$i<count($Contents);$i++) {

      /* Strip out "<Item " and " />" from the XML */
      $Contents[$i]=substr($Contents[$i], 6+strpos($Contents[$i],"<Item "));
      $Contents[$i]=substr($Contents[$i], 0, strlen($Contents[$i])-4);
      $breakapart=explode("\"",$Contents[$i]);

      /* Extract field names and values */
      for ($x=0;$x<count($breakapart);$x++){
         if ($x % 2 == 0){
            $k=trim(str_replace("=", "", $breakapart[$x]));
            if ($k!='') { $Data[$i][$k]=$breakapart[$x+1]; }
         }

      }

   }

}

?>

	
	<h1>Address Finder</h1>

	<form method="post" action="address_finder.php">

<?php

if (isset($_POST['address'])) {

   FetchAddress($_POST['address']);   

   /*
   $Data is populated with 
    id
    seq
    organisation_name
    line1
    line2
    line3
    line4
    line5
    post_town
    county
    postcode
    mailsort
    barcode
    is_residential
    is_small_organisation
    is_large_organisation
   */

   if(count($Data)==0){
      echo "<br><h3>Sorry, there was an error fetching your address. Please try again.</h3>";
   }

   else{

      /*
      We can loop through the records as there is only one returned.
      The address can now be stored in a database, but here we'll display it in a form.
      */
      
      echo "<table>\n";

      foreach ($Data as $keyd => $data) {
         
         $organisation = $data["organisation_name"];
         $line1 = $data["line1"];
         $line2 = $data["line2"];
         $line3 = $data["line3"];
         $line4 = $data["line4"];
         $line5 = $data["line5"];
         $town = $data["post_town"];
         $county = $data["county"];
         $postcode = $data["postcode"];
         echo "<tr>\n";
         echo "<td>Company</td><td><input type=\"text\" name=\"company\" id=\"company\" value=\"$organisation\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Line1</td><td><input type=\"text\" name=\"line1\" id=\"line1\" value=\"$line1\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Line2</td><td><input type=\"text\" name=\"line2\" id=\"line2\" value=\"$line2\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Line3</td><td><input type=\"text\" name=\"line3\" id=\"line3\" value=\"$line3\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Line4</td><td><input type=\"text\" name=\"line4\" id=\"line4\" value=\"$line4\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Line5</td><td><input type=\"text\" name=\"line5\" id=\"line5\" value=\"$line5\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Town</td><td><input type=\"text\" name=\"town\" id=\"town\" value=\"$town\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>County</td><td><input type=\"text\" name=\"county\" id=\"county\" value=\"$county\"></td>\n";
         echo "</tr>\n";
         echo "<tr>\n";
         echo "<td>Postcode</td><td><input type=\"text\" name=\"postcode\" id=\"postcode\" value=\"$postcode\"></td>\n";
         echo "</tr>\n";
         
      }

      echo "</table>";
      
   }
   
}
elseif (isset($_POST['postcode'])) {
   
   $postcode = $_POST['postcode'];

   ByPostcode("$postcode");

   /*
   $Data is populated with 
    id
    seq(uence)
    description
   */

   if(count($Data)==0){
      echo "<br><h3>No results were found for $postcode.  Please try another postcode.</h3>";
   }
   
   else{

      echo "Please select your address: \n";
      echo "<select name=\"address\" id=\"address\">\n";

      foreach ($Data as $keyd => $data) {
         echo "<option value=\"" . $data["id"] . "\">" . trim($data["description"]) . "</option>\n";
      }

      echo "</select>\n";
      echo "<br><input type=submit value=\"Select this address\">";
      
   }

}
else {

	echo "<p>Enter a postcode below and click on the 'Find' button to search for your address.</p>\n";
	echo "Postcode: <input name=\"postcode\" id=\"postcode\" value=\"\" type=\"text\" size=\"10\">\n";
	echo "<input id=\"btnFind\" type=\"submit\" value=\"Find\">\n";

}

?> 

	</form>

</body>
</html>

Flash version: 8

Last edited by scoop; 09-20-06 at 09:23 AM.
Reply With Quote
  #2 (permalink)  
Old 09-21-06, 02:53 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
there are two ways, i know of, that can solve your problem.

The first one is using LoadVars. this is used by echoing the data through php on a page, and reading it by flash. It is best that you don't send html content to the screen, as there will be problems loading it into flash.

A second way is to use the built-in XML parser. you simple create one of those, and load the contents out of an xml page or just a php page. This way you have to make sure the html you output is a valid xml data structure (all tags must be closed, only 1 root tag, no spaces in the nodenames, etc...)

If i were you, i just stripped the html of your php page, and parse the xml to flash. in flash you use the XML parser to read it, and you output the data in flash, using a css or something.

for help on XML and/or LoadVars check out the livedocs, or search this forum for "LoadVars" or "XML flash"

Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 09-25-06, 12:35 PM
scoop scoop is offline
New Member
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply; I've got it all running ok using the XMLConnector.

I'm not too impressed with Actionscript so far though, and I'm struggling with some real weirdness in many different areas.

For example, in the following script I have a button triggering an XMLConnector called "addressListLookup". The XMLConnector is bound to a List component, which I want hidden when there are less than 2 results.

When I run the script, the connector loads XML data with 71 entries in it, but addressList.length is not updated (a=0), so the List component doesn't appear. Pressing the Load button a second time updates the .length, and the List appears (a=71).

What am I dong wrong?

Code:
		

    this.addressListLookup.URL = sURL;
    this.addressListLookup.trigger();

    a = addressList.length;
    if (a < 2){
			this.addressList.visible = false;
			this.nextButton.visible = false;
			} else {
			this.addressList.visible = true;
			this.nextButton.visible = true;
		}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP to XML to Flash shadowness Script Requests 0 07-30-06 09:48 PM
xml thumbnail flash gallery problems jelen Flash & ActionScript 0 07-06-06 11:31 AM
Encrypting XML files in Flash MP3 Player mrtunes Script Requests 0 05-12-06 01:04 PM
Using variables in flash to include a XML riquelme Flash & ActionScript 3 03-30-06 02:19 PM
Creating a Navigation bar with Flash and XML Eagels14 HTML/XHTML/XML 0 12-14-04 09:14 PM


All times are GMT -5. The time now is 06:28 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.