hi..
in flex 4,
i m trying to store RSS feeds in mySQL database throgh PHP using Zend
framework.
here is my code..

getFeeds.mxml

<?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/halo"
xmlns:feedservice="services.feedservice.*"
xmlns:valueObjects="valueObjects.*"
xmlns:employeeservicepaged="services.employeeservicepaged.*"
xmlns:feedservicepaged="services.feedservicepaged.*">
        <fx:Script>
                <![CDATA[
                        import com.adobe.xml.syndication.rss.Item20;
                        import com.adobe.xml.syndication.rss.RSS20;
                        import mx.collections.ArrayCollection;
                        import mx.controls.Alert;
                        import mx.events.FlexEvent;
                        import mx.events.ValidationResultEvent;
                        import mx.validators.*;
                        [Bindable]
                        private var feedString:String="";
                        private var loader:URLLoader;
                        [Bindable]
                        private var storeFeeds:ArrayCollection;

                        public var rss:RSS20 = new RSS20();
                        //url of rss 2.0 feed
                        private static const RSS_URL:String = 
"http://www.billboard.com/rss/
charts/digital-songs";

//----------------Parsing------------------------

                        // Write the results to the
                        private function 
handleResult(eventObj:ValidationResultEvent):void
{
                                if (eventObj.type == 
ValidationResultEvent.VALID) {
                                        // For valid events, the results Array 
contains
                                        // RegExpValidationResult objects.
                                        var xResult:RegExpValidationResult;

                                        for (var i:uint = 0; i < 
eventObj.results.length; i++) {
                                                xResult = eventObj.results[i];
                                                feedString = feedString + 
xResult.matchedString;
                                        }
                                } else {
                                        Alert.show("nop");
                                }
                        }
                        //----------------Parsing------------------------


                        //called when user presses the button to load feed
                        private function onLoadPress():void
                        {
                                loader = new URLLoader();

                                //request pointing to feed
                                var request:URLRequest = new 
URLRequest(RSS_URL);
                                request.method = URLRequestMethod.GET;

                                //listen for when the data loads
                                loader.addEventListener(Event.COMPLETE, 
onDataLoad);

                                //listen for error events
                                loader.addEventListener(IOErrorEvent.IO_ERROR, 
onIOError);
                                
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,
onSecurityError);

                                //load the feed data
                                loader.load(request);
                        }

                        //called once the data has loaded from the feed
                        private function onDataLoad(e:Event):void
                        {
                                //get the raw string data from the feed
                                var rawRSS:String = URLLoader(e.target).data;

                                //parse it as RSS
                                parseRSS(rawRSS);

                        }

                        //parses RSS 2.0 feed and prints out the feed titles 
into
                        //the text area
                        private function parseRSS(data:String):void
                        {

                                //XMLSyndicationLibrary does not validate that 
the data contains
valid
                                //XML, so you need to validate that the data is 
valid XML.
                                //We use the XMLUtil.isValidXML API from the 
corelib library.
                                /*if(!XMLUtil.isValidXML(data))
                                {
                                writeOutput("Feed does not contain valid XML.");
                                return;
                                }*/

                                //create RSS20 instance


                                //parse the raw rss data
                                rss.parse(data);

                                //get all of the items within the feed
                                var items:Array = rss.items;
                                storeFeeds=new ArrayCollection(items);


                                //loop through each item in the feed
                                for each(var item:Item20 in items)
                                {
                                        //print out the title of each item
                                        writeOutput(item.title);

                                         feedVO.title=item.title;


                                        feedServicePaged.createItem(feedVO);

                                }
                        }

                        private function writeOutput(data:String):void
                        {
                                outputField.text += data + "\n";
                        }

                        private function onIOError(e:IOErrorEvent):void
                        {
                                writeOutput("IOError : " + e.text);
                        }

                        private function 
onSecurityError(e:SecurityErrorEvent):void
                        {
                                writeOutput("SecurityError : " + e.text);
                        }

                        protected function dataGrid_creationCompleteHandler
(event:FlexEvent):void
                        {
                                getAllItemsResult.token = 
feedServicePaged.getAllItems();
                        }


                        protected function 
button_clickHandler(event:MouseEvent):void
                        {
                                createItemResult.token = 
feedServicePaged.createItem(feedVO);
                        }

                ]]>
        </fx:Script>

        <fx:Declarations>
                <s:CallResponder id="getAllItemsResult"/>
                <feedservicepaged:FeedServicePaged id="feedServicePaged"
fault="Alert.show(event.fault.faultString + '\n' +
event.fault.faultDetail)" showBusyCursor="true"/>
                <valueObjects:FeedVO fx:id="feedVO"/>
                <s:CallResponder id="createItemResult"/>

    <!-- Regular Expresion for Parsing -->
                <mx:RegExpValidator id="regExpV"
                                                        source="{feedString}" 
property="text"
                                                        flags="g" 
expression="{feedString}"
                                                        
valid="handleResult(event)"
                                                        
invalid="handleResult(event)"
                                                        trigger="{button}"
                                                        triggerEvent="click"/>

        </fx:Declarations>



        <mx:TextArea left="20" top="10" bottom="40" right="10"
id="outputField"/>
        <mx:Button label="Load RSS" right="10" bottom="10" click="onLoadPress
()"/>
        <mx:DataGrid x="260" y="28" id="dataGrid"
creationComplete="dataGrid_creationCompleteHandler(event)"
dataProvider="{getAllItemsResult.lastResult}">
                <mx:columns>
                        <mx:DataGridColumn headerText="id" dataField="id"/>
                        <mx:DataGridColumn headerText="title" 
dataField="title"/>
                </mx:columns>
        </mx:DataGrid>
        <mx:Form defaultButton="{button}" x="260" y="178">
                <!--<mx:FormItem label="Id">
                        <s:TextInput id="idTextInput" text="@{feedVO.id}"/>
                </mx:FormItem>-->
                <mx:FormItem label="Title">
                        <s:TextInput id="titleTextInput" 
text="@{feedVO.title}"/>
                </mx:FormItem>
                <s:Button label="CreateItem" id="button" 
click="button_clickHandler
(event)"/>
        </mx:Form>


</s:Application>

------------------------------------

feedServicePaged.php
<?php

    /* FeedServiceDM.php */

    class FeedServicePaged {

    private $connection;

    public function __construct() {
        $this->connection = mysqli_connect("localhost",  "root",
"mohsin", "work",  "3306")
            or die(mysqli_connect_error());
    }

    public function getAllItems() {
        $sql = "SELECT * FROM billbord_digital_song";
        $result = mysqli_query($this->connection, $sql)
                or die('Query failed: ' . mysqli_error($this-
>connection));

        $rows = array();
        while ($row = mysqli_fetch_object($result)) {
            $rows[] = $row;
        }

        mysqli_free_result($result);
        mysqli_close($this->connection);

        return $rows;
    }

    public function getItem($itemID) {

        $itemID = mysqli_real_escape_string($this->connection,
$itemID);
        $sql = "SELECT * FROM contact where id=$itemID";

        $result = mysqli_query($this->connection, $sql)
                    or die('Query failed: ' . mysqli_error($this-
>connection));

        $rows = array();
        while ($row = mysqli_fetch_object($result)) {
            $rows[] = $row;
        }

        mysqli_free_result($result);
        mysqli_close($this->connection);

        return $rows;
    }

    public function createItem($item) {

$stmt = mysqli_prepare($this->connection, "INSERT INTO
Billbord_digital_song (id, title) VALUES (?, ?)");


                mysqli_bind_param($stmt, 'is', $item->id, $item->title);


                mysqli_stmt_execute($stmt);


                $autoid = mysqli_stmt_insert_id($stmt);

                mysqli_stmt_free_result($stmt);
                mysqli_close($this->connection);

                return $autoid;

}

  }
?>

-----------------------------
you have to add xmlsyndication.swc (used for RSS library) in lib
folder..
anyone have better(soft code) for this one???

Regards..
_MJ.

--

You received this message because you are subscribed to the Google Groups "Flex 
India Community" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/flex_india?hl=en.


Reply via email to