Flex, PHP and XML - a tutorial
Published by admin January 18th, 2007 in Flash Platform, TutorialsI've wanted to have a play with Flex for a while, I see the Framework and components as an easier way to build RIA's without some of the hassle of writing complex components.
The problem I've had previously when attempting to build anything was hooking up the data/back end. And it's true there is a distinct lack of tutorials online explaining how to tie Flex into a few simple PHP calls, there's plenty of AMF PHP and .net stuff.
Here's what we're going to make.
The example below uses motorbikes as a data type, I happen to be looking at buying one at the moment. So my first task was to setup my database, you can do this with the following SQL or use an app like <a href="http://www.navicat.com/" >NaviCat</a>
-
CREATE TABLE `data` (
-
`id` INT NOT NULL AUTO_INCREMENT ,
-
`year` VARCHAR( 4 ) NOT NULL ,
-
`make` VARCHAR( 30 ) NOT NULL ,
-
`model` VARCHAR( 30 ) NOT NULL ,
-
PRIMARY KEY ( `id` )
-
);
..then we can get the data flowing with some PHP. This first php file I wrote was to add bikes to my database, it has 3 main chunks, the top section is some basic HTML, the middle section does all our logic and handles our form submission. The last chunk is some more HTML laying out a simple form so we can add our data.
-
<?php
-
{
-
$year = $_POST['year'];
-
$make = $_POST['make'];
-
$model = $_POST['model'];
-
}
-
include 'library/config.php';
-
//config.php contains:
-
//$dbhost = 'localhost:3306';
-
//$dbuser = 'root';
-
//$dbpass = 'root';
-
//$dbname = 'bike';
-
-
include 'library/opendb.php';
-
//opendb.php contains:
-
//$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
-
//mysql_select_db($dbname);
-
-
$query = "INSERT INTO data (year, make , model) VALUES ('$year', '$make' , '$model')";
-
-
-
$xml_output = "<?xml version=\"1.0\"?>\n";
-
$xml_output .= "<bikes>\n";
-
for ( $x = 0 ; $x <mysql_num_rows ( $result ) ; $x ++ ) {
-
$xml_output .= "\t <bike>>" ;
-
$xml_output .= "\t\t <year>" . $row['year'] . "</year>\n";
-
$xml_output .= "\t\t <make>" . $row['make'] . "</make>\n";
-
$xml_output .= "\t\t <model>" . $row['model'] . "</model>\n";
-
$xml_output .= "\t </bike>\n";
-
}
-
$xml_output .= "</bikes>";
-
include 'library/closedb.php';
-
-
echo $xml_output;
-
}
-
?>
...so what the hells going on in there? Well, we check that we have some post data from the form. Assign that data to some variables for use later. Connect to our database. Query our database, inserting the data we collected from the POST. Grab all the information from our table. Then we start building a bit of XML, we have a for loop going through each row of the table, writing XML tags to $xml_output. Once that's finished looping we close off the XML. I've left the echo in there just to show that it's working
Ok the HTML should be fairly straight forward, important thing to note is the name and id are set to the same ref at the top of the PHP.
So assuming you've got a web server running mySQL and php5 the above script should work. The next page is going to show a stripped down version of the php and how to call it from Flex.
Pages: 1 2
cheers Jolyon, I have an idea for my first flex app and I’m sure this tut will give me a good head start. The app will be an internal video sharing system for my fellow employees. We’re all going to pool our videos/DVDs so we can borrow each others! I’ll let you know how well it works…
best paddy
Nice one Jolyon, a couple more of these and get this on MXNA!! It’s been a while since we saw comprehensive walkthrough tuts like this, a few years since these have been popular and god knows thats how I learned it, so congrats.
This is a great post and Flex-PHP example. Simple yet perfect as an example to build up from.
Best regards,
Matthew
Brisbane, Australia
hi there,
this seems a great example. But when i tried it, i got error message like :
ReferenceError: Error #1065: Variable _bikeData is not defined.
at global/flash.utils::getDefinitionByName()
at mx.utils::DescribeTypeCache$/describeType()
at mx.utils::ObjectUtil$/getClassInfo()
at mx.controls::DataGrid/::generateCols()
…
can somebody help me about this ?
thanks
Hi Bintar,
I’m sorry to hear you’re having trouble.
If you zip up the whole project and post it on a webserver somewhere I’ll take a look at your code, just post the link up as another comment.
I’ll try and get back to you as soon as possible.
Jolyon
Finally found a tut I can understand and is complete :)!
Thanks!!