Thursday, April 16, 2009

Using PHP with Flex

TABLE STRUCTURE

Product

id integer primary key auto_increment,name varchar(50),price decimal(10,2),category_id integer,imagepath varchar(100),updateDay date

FLEX CODE

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="{this.nativeWindow.maximize()}">
<mx:Script source="assets/dbUpdaterscript.as"/>
<mx:Panel width="589" height="610" layout="absolute" title="Product Details" horizontalCenter="3" verticalCenter="15">
<mx:Grid width="506.5" height="376" horizontalCenter="0" verticalCenter="-60">
<mx:GridRow width="100%" height="100%">
<mx:GridItem width="141" height="100%" verticalAlign="middle">
<mx:Label text="Product Name"/>
</mx:GridItem>
<mx:GridItem width="100%" height="100%" verticalAlign="middle" horizontalAlign="left">
<mx:TextInput id="nameTB" width="247"/>
</mx:GridItem>
</mx:GridRow>
<mx:GridRow width="100%" height="100%">
<mx:GridItem width="100%" height="100%" verticalAlign="middle">
<mx:Label text="Product Price"/>
</mx:GridItem>
<mx:GridItem width="100%" height="100%" verticalAlign="middle" horizontalAlign="left">
<mx:TextInput id="priceTB" width="247"/>
</mx:GridItem>
</mx:GridRow>
<mx:GridRow width="100%" height="100%">
<mx:GridItem width="100%" height="100%" verticalAlign="middle">
<mx:Label text="Product Category"/>
</mx:GridItem>
<mx:GridItem width="100%" height="100%" verticalAlign="middle" horizontalAlign="left">
<mx:TextInput id="categoryIdTB" width="247"/>
</mx:GridItem>
</mx:GridRow>
<mx:GridRow width="100%" height="100%">
<mx:GridItem width="100%" height="100%" verticalAlign="middle">
<mx:Label text="Last Updated"/>
</mx:GridItem>
<mx:GridItem width="100%" height="100%" verticalAlign="middle" horizontalAlign="left">
<mx:DateField showToday="true" id="dateSelecter" width="247" formatString="YYYY-MM-DD" change="onDateChange(event)"/>
</mx:GridItem>
</mx:GridRow>
<mx:GridRow width="100%" height="100%">
<mx:GridItem width="100%" height="100%" verticalAlign="middle">
<mx:Label text="Product Image"/>
</mx:GridItem>
<mx:GridItem width="100%" height="100%" verticalAlign="middle">
<mx:TextInput id="imagePathTB" width="165"/>
<mx:Button label="Browse" id="browseButton" click="browseForFile(event)" width="74"/>
</mx:GridItem>
</mx:GridRow>
</mx:Grid>
<mx:Button x="233" y="443" label="SUBMIT INFO" click="submitData(event)"/>
</mx:Panel>
</mx:WindowedApplication>

ACTIONSCRIPT CODE (databaseupdaterscript.as)

// ActionScript file
import flash.events.Event;
import flash.filesystem.File;
import flash.net.FileFilter;

import mx.rpc.http.HTTPService;
private var dayStr:String;
private var monthStr:String;
private var yearStr:String;
[Bindable]
private var dateStr:String;
private var file:File;
private var fileFilter:FileFilter;
private var phpInvoker:HTTPService=new HTTPService();
public function browseForFile(e:Event):void
{
file=new File();
fileFilter=new FileFilter("Image","*.jpg;*.png;*.tiff;*.gif;");
file.browse([fileFilter]);
file.addEventListener(Event.SELECT,fileSelectHandler);
}
private function fileSelectHandler(e:Event):void
{
imagePathTB.text=file.url.substr(8,file.url.length);
}
public function onDateChange(e:Event):void
{
yearStr=e.currentTarget.selectedDate.getFullYear();
dayStr=e.currentTarget.selectedDate.getDate();
monthStr=(e.currentTarget.selectedDate.getMonth()+1);
dateStr=yearStr+"/"+monthStr+"/"+dayStr;
}
public function submitData(e:Event):void
{
var postData:Object={nameVal:nameTB.text,priceVal:priceTB.text,imagepathVal:imagePathTB.text
,updateDayVal:dateStr,categoryIdVal:categoryIdTB.text}
phpInvoker.url="http://localhost/test/dbUpdater.php";
phpInvoker.method="POST";
phpInvoker.send(postData);

}

PHP CODE (dbupdater.php)

<?php
include("../database/db.php");
$nameVal=$_POST["nameVal"];
$categoryVal=$_POST["categoryVal"];
$priceLevel0=$_POST["priceLevel0"];
$priceLevel1=$_POST["priceLevel1"];
$priceLevel2=$_POST["priceLevel2"];
$imagepathVal=$_POST["imagepathVal"];
$descriptionVal=$_POST["descriptionTxt"];
$today=date("Y-m-d");

$query="select category_id from category where category_name='$categoryVal'";
$result=mysql_query($query,$db);
if($row=mysql_fetch_array($result))
{
$categoryVal=$row['category_id'];
}
$query ="insert into product(`product_name`, `category_id`, `price_level0`, `price_level1`, `price_level2`, `product_image`,
`update_date`, `description`) values('$nameVal',$categoryVal,$priceLevel0,$priceLevel1,$priceLevel2,'$imagepathVal','$today','$descriptionVal')";
echo "OK";
mysql_query($query,$db);
$query="commit";
mysql_query($query,$db);
mysql_close($db);
?>

No comments:

Post a Comment