0 Replies Latest reply on Aug 3, 2010 11:32 PM by Deh Bontempos

    whats the best practice to simply manage data using php sql?

    Deh Bontempos

      Hi

       

      I'm trying to follow some tutorials but there is too much old material and I'm getting confused.

       

      This is basic. I have a db and I want to movement data from flex.

      So, this is how I'm trying to do so. As a newbie, I will try to be clear.

       

      I have created a simple table called "teste" using myphpadmin with only 2 fields for testing.  (id, name).

      I have created the services automaticly using flex to generate a php code.

       

      This code is a simple exercise with buttons to add, update, and delete a Item.

      Although there is a lot of auto-generated code, I had a lot of work (due to my ignorance) to make it work.

       

      It fairly works, but I know veteran users would make it better, smarter, shorter, and ALL I WANT is a better (and simple) example to follow

       

       

      Please.

      Thanks in advance

       


      ps. I have set my table field "name" to the 'utf8_unicode_ci' while creating it and I it seems the adobe's generated php can't handle latin chars.

       

       

      THE CODE AS FOLLOWS:

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                        xmlns:s="library://ns.adobe.com/flex/spark" 
                        xmlns:mx="library://ns.adobe.com/flex/mx"
                        minWidth="955" minHeight="600"
                        xmlns:valueObjects="valueObjects.*"
                        xmlns:testeservice="services.testeservice.*"
                        creationComplete="application1_creationCompleteHandler()" >
           
           
      <fx:Script>
                <![CDATA[
                     import mx.collections.ArrayCollection;
                     import mx.controls.Alert;
                     import mx.events.FlexEvent;
                     import mx.events.ListEvent;               
                     import spark.events.IndexChangeEvent;
                     import spark.events.TextOperationEvent;               
                     
                     //selected item
                     public var selId:int;
                     
                     
                     //------GET ITEM (init)------------------------------------------------------
                     protected function application1_creationCompleteHandler():void
                     {
                          tx_edit.addEventListener(KeyboardEvent.KEY_UP,parallelEdit)
                          getAllTesteResult.token = testeService.getAllTeste();                    
                     }               
                     
                     
                     
                     //------SELECTED ITEM ID------------------------------------------------------          
                     
                     protected function list_changeHandler(event:IndexChangeEvent):void
                     {
                          
                          selId = event.currentTarget.selectedItem.id;
                          
                          lb_selectedId.text = "sel id: "+ selId;
                          
                          if(tb_edit.selected) tx_edit.text = event.currentTarget.selectedItem.name;
                          
                     }
                     
                                    
                     //------ADD ITEM-----------------------------------------------------
                     protected function button_clickHandler(event:MouseEvent):void
                     {
                          var teste2:Teste     =     new Teste();
                          
                          teste2.name                = nameTextInput.text;
                          createTesteResult.token = testeService.createTeste(teste2);
                          
                          application1_creationCompleteHandler();
                     }
                     
                     
                     //------UPDATE ITEM (in parallel)-----------------------------------------------------
                     private function parallelEdit(e:KeyboardEvent):void
                     {
                          if(!isNaN(selId) && selId > 0){
                               if(tb_edit.selected){
                                    
                                    //uptadating
                                    teste2.id                     = selId;
                                    teste2.name                = tx_edit.text;
                                    updateTesteResult.token = testeService.updateTeste(teste2);
                               
                               }
                          }
                     }
                     //------UPDATE ITEM (button click)-----------------------------------------------------
                     protected function button3_clickHandler(event:MouseEvent):void
                     {
                          
                          teste2.id                     = parseInt(idTextInput2.text);
                          teste2.name                = nameTextInput2.text;
                          updateTesteResult.token = testeService.updateTeste(teste2);
                     }
                     
                     
      
                     
      
                     //------DELETE ITEM------------------------------------------------------     
                     
                     protected function button2_clickHandler(event:MouseEvent):void
                     {
                          if(!isNaN(selId) && selId > 0)     deleteTesteResult.token = testeService.deleteTeste(selId);
                     }
                     
                     
                     
                ]]>
           
           </fx:Script>
           
           
           
           <fx:Declarations>
                
                <!-- this part was mostly auto generated -->
                
                <valueObjects:Teste id="teste"/>
                <testeservice:TesteService id="testeService" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
                <s:CallResponder id="createTesteResult"/>          
                <s:CallResponder id="getAllTesteResult"/>
                <s:CallResponder id="deleteTesteResult"/>
                <s:CallResponder id="updateTesteResult"/>
                <valueObjects:Teste id="teste2"/>  
                
           </fx:Declarations>
           
                <!-- this are just visual objects. Renamed only necessary for this example 
                      most lines were auto-generated-->
                     
           
           <!--Create form -->
           
           <mx:Form defaultButton="{button}">
                
                <mx:FormItem label="Name">
                     <s:TextInput id="nameTextInput" text="{teste.name}"/>
                </mx:FormItem>
                
                <s:Button label="CreateTeste" id="button" click="button_clickHandler(event)"/>
                
           </mx:Form>     
           
           <!--Create Result -->
           
           <mx:Form x="10" y="117">
                <mx:FormItem label="CreateTeste">
                     <s:TextInput id="createTesteTextInput" text="{createTesteResult.lastResult as int}" />
                </mx:FormItem>
           </mx:Form>
           
           
           <!--List -->
           <s:List x="10" y="179" id="list" labelField="name" change="list_changeHandler(event)">
           
                <s:AsyncListView  id="anta" list="{getAllTesteResult.lastResult}" />
           
           </s:List>
           
           <!--Update in parallel -->
           <s:Label x="147" y="179" id="lb_selectedId"/>
           <s:Button x="147" y="222" label="Remove" id="button2" click="button2_clickHandler(event)"/>
           <s:TextInput x="225" y="257" id="tx_edit"/>
           <s:ToggleButton x="147" y="257" label="Edit" id="tb_edit"  />
           
           
           <!--Update with button click -->
           <mx:Form defaultButton="{button3}" x="220" y="0">
                <mx:FormItem label="Id">
                     <s:TextInput id="idTextInput2" text="{teste2.id}"/>
                </mx:FormItem>
                <mx:FormItem label="Name">
                     <s:TextInput id="nameTextInput2" text="{teste2.name}"/>
                </mx:FormItem>
                <s:Button label="UpdateTeste" id="button3" click="button3_clickHandler(event)"/>
           </mx:Form>
           
      </s:Application>