Flash_and_Flex_03_2009

Miniblog Part 2

Developing the PHP Code The PHP code is setup to be easily editable, but also clearly understand what is happening. The first bit disables error_ reporting in PHP, to ensure bad data is not passed back to Flex. The $tableName variable holds a reference to the name of the MySQL table. The $dbLink variable is setup to connect to the database, and select the database that the blog data is stored in. The response variable is used to store the XML that will be returned to Flex. The database query is a simple SELECT on the table the blog contents are stored in. In a larger application I would recommend some more optimized SQL, but that won't be a problem at this time, see (Listing 12). The second half of the PHP code is used to setup the XML response and a while loop that takes each row from the SQL table and builds an XML node. The last part of this PHP code takes the $xml response and passes it back to Flex, see (Listing 13). Wrap up At this point you have completed the Flex front-end and the PHP code that creates the XML response. The final step is to return to Flex and test the application. You can now add categories or even more features, the choice is yours. Either way you should now have a fully working mini blog using Flex and PHP. In future articles I will cover more advanced uses of Flex and PHP. If you have questions or comments, please drop me a message at matt@mkeefedesign.com .

the single view. The next section creates a new blogItem instance, adds it to the display list and passes in values for the specific blog post, see (Listing 9). The fullPostHandler() method is called when the user clicks on the LinkButton in the BlogItem instance. This method hides the blog list and displays the single post view, see (Listing 10).

The last method is responsible for hiding the single post view and displaying the list of posts, essentially returning the application to the starting state, see (Listing 11). At this point you have now completed the Flex portion of this application. The last section is the PHP code, that will be used to return an XML list of blog posts.

Listing 12. The first section of the PHP code

// disable error reporting, errors/warnings // can confuse Flash

error_reporting(0);

// name of the sql table, used in the 'SELECTS' and 'INSERTS'

$tableName = "miniblog_posts";

// load database

$dbLink = mysql_connect("localhost", "username", "password"); mysql_select_db("minblog", $dbLink);

// response, will be returned to Flash

$response = "";

// select all posts in the "miniblog_posts" table

$query = mysql_query("SELECT * FROM " . $tableName);

Listing 13. The second section of the PHP code, responsible for building the XML response

// create xml node

$xml = "\n";

// loop through posts and create xml structure

while ($row = mysql_fetch_array($query)) { $xml .= " \n";

$xml .= " " . $row['id'] . "\n"; $xml .= " " . $row['postTitle'] . "\n"; $xml .= " \n"; $xml .= " \n";

}

MATTHEW KEEFE Matthew Keefe is a new-media designer, developer, with a strong background in application development for the web and offline platforms. His work has been published in How To Wow with Flash. He was also technical editor for Essential ActionScript 3 and most recently wrote the Flash and PHP Bible for Wiley. Matt runs the online scripting community Scriptplayground.com and his work can be found at his personal portfolio site: mkeefe.com

$xml .= "\n";

$response = $xml;

print $response;

?>

03/2009 (5)

67

Made with FlippingBook HTML5