I will now start my 3 part series on connecting PHP/MySQL datasets with a Flex application. Part 1 will deal with writing the PHP back end script.
To start, you need to know the PHP command Print. this will give you the means to send along the data. You also need to know variables which will be used in the script.
To start, you need to define a variable and give it a value. The value of the variable will be the beginning of your XML code. So, the variable should be called $result. Define $result like this:
$result = "<?xml version="1.0" encoding="utf-8"?>"; without this, flex will not be able to decode this data as XML. You need to put quotes around the text in the tag or PHP will not recognize it as text.
Next, you use the function that will append data to your variable. This is easy, and involves only adding a . to the end of a variable. So, to define your first XML tag, you do something like this:
$result. = "<myTag>"; which will add the <myTag> tag to your XML script.
So, you want to put MySQL or other data into the XML? Here's how! You have to know how to access the data. You would put it into the tag like this:
$result. = "<myDataTag>".$row_my_data['data']."</myDataTag>"; Notice the periods between the text and data. These will let PHP know that the data in the $row_my_data['data'] text will come from elsewhere in the script.
To have multiple pieces of data in the same thing, like member names of an organization, all you have to do is to put a repeat region around one of the tag definitions. This will repeat the tag until there is no data left, or another user defined parameter.
To finish it off, you have to make sure that you have closed all your tags. Otherwise, there will be an error.
So now, the $result variable has all the XML data that you need. All you have to do is print it to the page. So:
print $result; will do the job.
In a few days, I will put up the second part of the series, "Receiving the Data in the Flex Application", followed by "Implementing Data in the Application".