Hello. I have a new blog.

I've moved this blog to the following URL Kerkness.ca. Thank you for visiting, please update your bookmarks.

Showing posts with label php-to-flex. Show all posts
Showing posts with label php-to-flex. Show all posts

Saturday, September 6, 2008

PHP trim() function in Actionscript for Flex

Here is PHP's trim() function reproduced in Actionscript for use in a Flex application

public function trim(str:String):String
{
for(var i:Number = 0; str.charCodeAt(i) < 33; i++);
for(var j:Number = str.length-1; str.charCodeAt(j) < 33; j--);
return str.substring(i, j+1);
}

UPDATE: Thanks to Almog Kurtser, I now know that Flex has a utility to handle trimming strings. Here is an alternative example
import mx.utils.StringUtil;

public function trim(str:String):String
{
return StringUtil.trim(str);
}


Of course it is redundant to actually create a function called trim just to call the method StringUtil.trim() but you get the idea.

Thursday, April 17, 2008

PHP explode() function in Actionscript for Flex

If you're a long time PHP programmer who has made the leap to building Flex based RIA applications in Flex you are probably asking yourself from time to time "how do I do a xxxxx function in actionscript". Well if you were recently wondering this about the PHP explode() function, here's your answer.

public function explode( delimiter:String, str:String ):Array
{
return str.split( delimiter );
}

Wednesday, February 27, 2008

Use a remote HTTPService in Flex with a PHP Proxy

One way to get around the hassle of a common security error when trying to call a HTTPService running on a different domain than your Flex application is to use a simple and local PHP script as a proxy to call the HTTPService for you. Perhaps not a solution you would want to use if you need to load massive piles of data but in most cases it works just fine and takes seconds to set up.

proxy.php

$content = file_get_contents(str_replace(' ','%20',$_GET['url']));
if ($content !== false) {
echo($content);
} else {
// there was an error
}

HTTPService in MXML
<mx:HTTPService id="myService"
url="http://www.localdomain.com/proxy.php?url=http://www.remote.com/api"/>

Thursday, February 21, 2008

PHP in_array() function in ActionScript for Flex

Another common PHP function is in_array() used to determine if a value exsists in an array. Here is the function recreated in actionscript for flex.

public function in_array( needle:*, haystack:Array ):Boolean
{
var itemIndex:int = haystack.indexOf( needle );
return ( itemIndex < 0 ) ? false : true;
}

Monday, February 18, 2008

PHP ucfirst() and ucwords() in Flex

Another common PHP function converted to ActionScript for use in a Flex application.

public function ucfirst( str:String ):String
{
return str.substr(0,1).toUpperCase()+str.substr(1);
}
public function ucwords( str:String ):String
{
var myArr:Array = str.split(' ');
for (var i:int in myArr ) {
myArr[i] = ucfirst( myArr[i] );
}
return myArr.join(' ');
}

PHP strtoupper() and strtolower() in Flex

Having been a PHP programmer for many years and only now in the last month started to learn Flex and ActionScript I find I'm always looking for ways to get something done in ActionScript which I know how to do in PHP. These are pretty straight forward and simple functions but sometimes it's nice to have a reference.

PHP strtoupper( $string ) and strtolower( $string ) in Flex

public function strtoupper( str:String ):void
{
return str.toUpperCase();
}
public function strtolower( str:String ):void
{
return str.toLowerCase();
}

Sunday, February 17, 2008

Flex / JSON / PHP Example Application

While trying to teach myself how to best use PHP and Flex together I build the following example application. Source code and details on how the application works are included.

The example application uses JSON (JavaScript Object Notation) to facilitate a smooth and simple way of providing PHP Arrays that can be used by a Flex Application. A more complete HowTo is included in the application.

Click here to launch the application
(right click to view source)

Requirements

This application requires a php class called json.php which encodes a PHP array and the corelib libraries available from Adobe Labs used to decode the array in Flex. You'll also need a mysql database with a single table called 'people' with the fields 'firstname','lastname' and 'category'.

To install the corelib libraries I used a post at Mike Chambers blog.
The json.php class originally comes from M. Migurski.

MXML Summary

Right click the example application to view the MXML source

  • FlexPhp.mxml : Main Application file. Contains a navigation tab and loads the components PhpDataGrid.mxml and PhpForm.mxml
  • PhpDataGrid.mxml : Contains a simple DataGrid and ActionScript for calling read.php
  • PhpForm.mxml : Contains a simple Flex form and ActionScript for calling write.php
Description of PHP Files

read.php : File queries the database and returns a JSON encoded array to the Flex application.
write.php : File accepts data posted from Flex form, updates the database and sends a JSON encoded response back to the flex application.
json.php : Class file used to encode PHP arrays into a JSON formatted string.
mysql.php : Class file for accessing and updating the database.
conf.php : Simple configuration file for keeping database access information and debugging.


Friday, February 15, 2008

String Replace Function in Flex

I'm not sure if this is a redundant function or not. I know that Flex String objects have a replace() method but as far as I can tell this only replaces the first match and not every match in a string.

Here is an actionscript function for flex which works the same as the php str_replace() function.

str_replace( needle, replacement, haystack ) in ActionScript

public function str_replace(needle:String, replacement:String, haystack:String):String
{
var strArr:Array = haystack.split(needle);
return strArr.join(replacement);
}


If this is a pointless function and there is an easier way to accomplish this built into flex I would love to hear about it. I looked over the language reference documents and couldn't find something.