• User

    [2.0]Flex Guestbook con AMFPHP 1.9

    Questo è un piccolo esempio sul funzionamento delle librerie amfphp 1.9...il codice è chiaro e per qualsiasi probleema sono a disposizione...
    Quest'esempio lo avevo gia postato su infernet...ma magari puo tornare utile a qualcuno...

    l'esempio si trova qui:

    ***************
    ```o qui:
    

    
    E ora per chi fosse interessato posto i sorgenti:
    
    la pagina php avrà il seguente codice:
    
     [php] <?
    
    class Fgb{
    
    //lettura db
    function getMsgs(){
    $fd='<dati>';
    
    if(!$dbc=mysql_connect('xxx','xxx','xxx')){return 'SqlError: '.mysql_error();}
    
    if(!mysql_select_db('myDB',$dbc)){return 'Database not found.';}
    
    if(!$res=mysql_query('SELECT * FROM tabella')){return 'Error: '.mysql_error();}
    
    $i=0;
    while($rst=mysql_fetch_array($res)){
    $fd.='<ogg><nid>'.$rst[ID].'</nid><author>'.$rst[author].'</author>
    <imgurl>'.$rst[imgurl].'</imgurl><msg>'.$rst[msg].'</msg></ogg>';
    $i++;
    }
    
    $fd.='<nm>'.$i.'</nm></dati>';
    
    mysql_free_result($res);
    mysql_close($dbc);
    
    return $fd;
    
    }
    //aggiunta post
    function addMsg($au,$im,$ms){
    if(!$dbc2=mysql_connect('xxx,'xxx','xxx)){return 'SqlError: '.mysql_error();}
    
    if(!mysql_select_db('myDB',$dbc2)){return 'Database not found.';}
    
    $q=sprintf("INSERT INTO tabella VALUES('',%s,%s,%s)",$this->quote_smart((string)$au),
    $this->quote_smart((string)$im),
    $this->quote_smart((string)$ms));
    
    $rs=mysql_query($q);
    mysql_close($dbc2);
    
    return $rs;
    }
    
    //funzione Da Adobe
    function quote_smart($value)
    {
    
    if (get_magic_quotes_gpc()) {
    $value = stripslashes($value);
    }
    
    if (!is_numeric($value)) {
    $value = "'" . mysql_real_escape_string($value) . "'";
    }
    return $value;
    }
    }
    
    ?>  [/php]codice semplice non occorrono spiegazioni...
    
    la parte in flex:
    root
       |
       |__ FxGuestbook.mxml (applicazione principale!)
       |__ comps(folder & package)
                  |__ backgr.mxml (componente dinamico)
                  |__ record.mxml (componente includente il messaggio)
       |__ gstyle.css (foglio di stile);
       |__ services-config.xml (configurazione amf);
    
    FxGuesbook.mxml:
    
    

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#2b2d39, #455574]">
    mx:Script
    <![CDATA[
    import mx.rpc.events.ResultEvent;
    import mx.events.CloseEvent;
    import comps.backgr;
    import mx.containers.TitleWindow;
    import comps.*;
    import mx.managers.PopUpManager;
    public var flag:Boolean=true;

    public function showHide(f:Boolean):void{
    switch(f){
    case true:
    show();
    break;
    case false:
    hide();
    break;
    }
    }

    public var g:backgr=new backgr();
    public function show():void{
    g.showCloseButton=true;
    PopUpManager.addPopUp(g,this);
    PopUpManager.centerPopUp(g);

    pls.label='Hide GuestBook';
    g.addEventListener(CloseEvent.CLOSE,onClose);
    function onClose():void{
    PopUpManager.removePopUp(g);
    flag=true;
    pls.label='Show GuestBook';
    }
    }
    public function hide():void{
    PopUpManager.removePopUp(g);
    pls.label='Show GuestBook';
    }

    public function newPost():void{
    if(flag){showHide(true);}
    g.resizee(1);
    }
    ]]>
    </mx:Script>
    <mx:Style source="gstyle.css"/>
    <mx:HBox creationComplete="showHide(flag);flag=!flag">
    <mx:Button id="pls" click="showHide(flag);flag=!flag;"
    width="128"/>
    <mx:Button id="newp" label="New post" click="newPost();"/>
    </mx:HBox>
    </mx:Application>

    
    

    <?xml version="1.0" encoding="utf-8"?>
    <mx:TitleWindow xmlns:mx="adobe.com/2006/mxml" layout="absolute" xmlns:comps="comps." height="554">
    mx:title<![CDATA[Flex GuestBook v 0.1 by GreyFox]]></mx:title>
    mx:Script
    <![CDATA[
    import mx.rpc.events.ResultEvent;
    import comps.
    ;
    import mx.controls.Alert;

    public function showResult(ev:ResultEvent):void{
    var el:XML=new XML(ev.result);

    var g:Boolean=true;
    desk.removeAllChildren();
    for(var i:Number=0;i<Number(el.nm);i++){

    var ob:record=new record()
    desk.addChildAt(ob,0);

    ob.cImg(String(el.ogg*.imgurl));
    ob.nome.text=String(el.ogg*.author);
    ob.msg.text=String(el.ogg*.msg);

    if(g){ob.setStyle('backgroundColor',0x7f7f7f);}else
    {ob.setStyle('backgroundColor',0x000000);}
    g=!g;
    }
    }

    public function npost():void{
    if(nm.text.length>=1 && iu.text.length>=1 && ms.text.length>=1){
    bridge.addMsg(nm.text,iu.text,ms.text);
    }else{Alert.show('Please type all the fields!');}
    }

    public function addedmsg(e:ResultEvent):void{
    if(Boolean(e.result)){Alert.show('Message saved!')}else{Alert.show('Error saving yuor message!')}
    bridge.getMsgs();
    resizee(0);
    }

    public function resizee(r:Number):void{
    var t:Array=new Array(desk,bbar);
    switch(r){
    case 1:
    desk.verticalScrollPolicy='off';
    rbox.widthTo=0;
    rbox.play(t);
    rbox.widthTo=565;
    rbox.target=newpost;
    rbox.play();
    break;
    case 0:
    desk.verticalScrollPolicy='on';
    rbox.widthTo=565;
    rbox.play(t);
    rbox.widthTo=0;
    rbox.target=newpost;
    rbox.play();
    break;
    }
    }
    ]]>
    </mx:Script>

    <mx:RemoteObject id="bridge" concurrency="last"
    source="<source>" destination="amfphp" showBusyCursor="true"
    fault="Alert.show(event.fault.faultDetail.toString());">
    <mx:method name="getMsgs" result="showResult(event);"/>
    <mx:method name="addMsg" result="addedmsg(event);"/>
    </mx:RemoteObject>

    <mx:Resize id="rbox" duration="2000"/>

    <mx:HBox horizontalGap="0" width="565">
    <mx:VBox verticalGap="0">
    <mx:VBox id="desk" creationComplete="bridge.getOperation('getMsgs').send()"
    height="506" width="565" horizontalScrollPolicy="off" verticalScrollPolicy="on">
    </mx:VBox>
    <mx:HBox id="bbar" width="565" backgroundAlpha="0.8" backgroundColor="#000000">
    <mx:Button label="Add New Message" click="resizee(1);"/>
    </mx:HBox>
    </mx:VBox>

    <mx:VBox id="newpost" height="506" width="0" horizontalScrollPolicy="off" >
    <mx:Label text="Please type all the fields" fontWeight="bold" fontSize="12"/>
    mx:HBox<mx:Label text="Name:"/><mx:Spacer width="16"/><mx:TextInput id="nm"/></mx:HBox>
    mx:HBox<mx:Label text="Image Url:"/><mx:TextInput id="iu"/></mx:HBox>
    mx:VBox<mx:Label text="Meggage:"/><mx:TextArea id="ms" width="532" height="154" verticalScrollPolicy="on"/></mx:VBox>
    mx:HBox
    <mx:Button label="OK" click="npost();"/>
    <mx:Button label="Clear" click="nm.text='';iu.text='';ms.text='';"/>
    <mx:Button label="Return to the posts" click="resizee(0);"/>
    </mx:HBox>
    </mx:VBox>
    </mx:HBox>
    </mx:TitleWindow>

    
    

    <?xml version="1.0" encoding="utf-8"?>
    <mx:HBox xmlns:mx="adobe.com/2006/mxml"
    width="548" height="122" backgroundAlpha="0.53">
    mx:Script
    <![CDATA[
    public function cImg(m:String):void{
    img.load(m);
    }
    ]]>
    </mx:Script>

    <mx:VBox width="106" height="122" horizontalAlign="center" verticalAlign="middle" verticalGap="2">
    <mx:Box borderStyle="solid" borderColor="#2d96ff">
    <mx:Image id="img" source="@Embed(source='../Sagoma.gif')" width="67" height="74"/>
    </mx:Box>
    <mx:Label id="nome" color="#a6bbdd"/>
    </mx:VBox>
    mx:VBox
    <mx:Spacer height="4"/>
    <mx:TextArea id="msg" width="429" height="104" verticalScrollPolicy="auto" maxChars="500" editable="false"/>
    </mx:VBox>

    </mx:HBox>

    
    

    Application{
    backgroundImage:'./sfondo.jpg';
    }
    TitleWindow{
    borderThicknessBottom:2;
    borderThicknessLeft:2;
    borderThicknessRight:2;
    paddingLeft:0;
    paddingBottom:0;
    paddingRight:0;
    paddingTop:0;
    headerHeight:22;
    borderThicknessTop:2;
    backgroundColor:#000000;
    backgroundAlpha:.6;
    cornerRadius:6;
    verticalAlign:top;
    headerColors:#a9afbc, #30353f;
    borderAlpha:1;
    highlightAlphas:.2,.1;
    borderColor:#30353f;
    color:#ceccd2;
    }

    Label{
    color:#62b0ff;
    fontFamily:Verdana;
    }
    TextInput{
    color:#62b0ff;
    fontFamily:Verdana;
    backgroundColor:#000000;
    borderColor:#3e9eff;
    backgroundAlpha:0.6;
    }
    TextArea{
    color:#62b0ff;
    fontFamily:Verdana;
    backgroundColor:#000000;
    borderColor:#3e9eff;
    backgroundAlpha:0.6;
    }

    Button{
    cornerRadius:0;
    fillColors:#a9afbc,;
    highlightAlphas:.2,.2;
    fillAlphas:1.0, 1.0;
    color:#1d2332;
    alpha:.8;
    }

    
    

    <?xml version="1.0" encoding="UTF-8"?>
    <services-config>
    <services>
    <service id="amfphp-flashremoting-service" class="flex.messaging.services.RemotingService" messageTypes="flex.messaging.messages.RemotingMessage">
    <destination id="amfphp">
    <channels>
    <channel ref="my-amfphp"/>
    </channels>
    <properties>
    <source>*</source>
    </properties>
    </destination>
    </service>
    </services>
    <channels>
    <channel-definition id="my-amfphp" class="mx.messaging.channels.AMFChannel">
    <endpoint uri="<indirizzo gateway>" class="flex.messaging.endpoints.AMFEndpoint"/>
    </channel-definition>
    </channels>
    </services-config>

    
    PS per usare amfphp dovete scaricare la cartella contenente le librerie da sourceforge e metterla sul vostro dominio poi compilare il .swf con il parametro:
    
     Codice:
    
    -services "services-config.xml"
    
      
    
    ah se non funziona l'esempio è perchè  avrete 
    superato il limite di queries sul database standard di altervista...
    provate l'ora successiva!!!
    e se c'è qualche problema con gli url di destinazione negli mxml è per il fatto che ho dovuto troncarli perche non sono ìutente premium' ...
     
    spero possa interessare a qualcuno ciao