4 Replies Latest reply on Dec 30, 2007 8:56 AM by Chaos7703

    SQL -> PHP -> XML -> Flex 2

    Chaos7703
      Howdy everyone!

      I've only been playing with Flex for about 6 weeks or so and I've figured out lots and lots from the online tutorials and help system. However the project that I'm trying to put together requires that I can pass data back and forth to Db's. I haven't even tried to write into a database yet because for the last 3 weeks I've been trying every combination (except the working one's) to write my Db data into XML and use an HTTPService tag to retrieve it for use as dataProviders. I've tried everything remotely related on this site. Several others I've found online and modified the resultFormat and my variable types in every combination (resultFormat = "[' ', Object, xml, e4x, text, array]" & myDP:[ArrayCollection, Array, XML, XMLList, XMLListCollection]) and have received as many errors & nonworking results for my efforts. I'm not sure if perhaps there's a PHP/MySQL setting on my server that's not set how it should be? or if I just have failed to notice some simple detail.

      Here's the PHP version I currently believe most promising:
      quote:

      <?php require_once('Connections/peaceheartconnection.php'); header("Content-type: text/xml");


      mysql_select_db($database_peaceheartconnection, $peaceheartconnection);
      $query_topics = "SELECT * FROM p_insight_topics";
      $topics = mysql_query($query_topics, $peaceheartconnection) or die(mysql_error());
      $row_topics = mysql_fetch_assoc($topics);
      $totalRows_topics = mysql_num_rows($topics);

      mysql_select_db($database_peaceheartconnection, $peaceheartconnection);
      $query_categories = "SELECT * FROM p_insight_types";
      $categories = mysql_query($query_categories, $peaceheartconnection) or die(mysql_error());
      $row_categories = mysql_fetch_assoc($categories);
      $totalRows_categories = mysql_num_rows($categories);

      print("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
      $dataXML = '';

      $dataXML .= "<data><categories>";

      while($cat = mysql_fetch_object($categories))
      {
      $dataXML .= "<category>$cat->type</category>" ;
      }
      $dataXML .= "</categories>";

      $dataXML .= "<topics>";

      while($topic = mysql_fetch_object($topics))
      {
      $dataXML .="<topic>$topic->topic</topic>";
      }
      $dataXML .= "</topics></data>";
      ?>

      <?php
      echo $dataXML;

      ?>

      <?php



      mysql_free_result($topics);

      mysql_free_result($categories);
      ?>


      This gives me results that show up in the browser identical to An XML file from "Flex 2 Training from the Source" (I event get the "no associated style information ... document tree is shown" message from Firefox).

      Here are the relevant parts of the Flex app:
      quote:

      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:ns1="Components.*" layout="absolute" autoLayout="true" horizontalScrollPolicy="off" verticalScrollPolicy="off" creationComplete=" getData()" >

      <!-- Service Definitions -->

      <mx:HTTPService id="getTypesNTopics" url="../../data.xml" method="POST" useProxy="false"
      result="showData(event)" fault="handleFault(event);" />
      <mx:HTTPService id="getTypesNTopics2" url="../../topicsNcategories5.php" method="POST" useProxy="false" result="showData2(event)" fault="handleFault2(event);" />

      <!-- End Service Definitions-->

      <mx:Script>
      <![CDATA[
      import mx.collections.XMLListCollection;
      import mx.effects.*;
      import mx.effects.easing.*;
      import mx.containers.*;
      import mx.core.*;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;
      import mx.events.FlexEvent;
      import mx.controls.Alert;
      import flash.events.*;
      import mx.managers.DragManager;
      import mx.events.DragEvent;
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.events.FaultEvent;

      [Bindable] private var topicsAC:ArrayCollection = new ArrayCollection();
      [Bindable] private var categoryAC:ArrayCollection = new ArrayCollection();
      [Bindable] private var topics2AC:XMLListCollection = new XMLListCollection();

      public function getData():void
      {
      getTypesNTopics.send();
      getTypesNTopics2.send();
      }

      private function showData2(event:ResultEvent):void
      {
      //topics2AC = event.result.data.topics.topic;

      lastResultValue2.text = "Event, Categories:" +event.result.data.categories.category
      + "Event, Topics:" + event.result.data.topics.topic;
      scrollMenu.dataProvider = event.result.data.topics.topic;
      lastResultValue2.text = "Result: " + String(event.result.data.topics.topic);
      }

      private function handleFault2(event:FaultEvent):void
      {
      lastResultValue2.text = "Error: " + event.fault;
      }


      (Keep in mind that the function & service tag contents are just the latest version as of 20 mins ago.)

      Any help would be greatly appreciated. There are still dozens of other things I want to get working on but there's no point until I can get my app. to talk with mySQL (databases). I'm begging PLEASE after weeks of wasting time trying to get my <topic> & <category> nodes to populate two ComboBoxes I just wrote a static XML file last night--just so I could feel like I had accomplished something (that's what the first Service tag does).

      Thanks 2^65536 times!
      Todd
        • 1. Re: SQL -&gt; PHP -&gt; XML -&gt; Flex 2
          Level 1
          Try getting rid of the xml decleration

          What errors do you recieve?

          I am using xml formatted in PHP with no problem

          Let me know the errors and I'll see if I can help
          • 2. Re: SQL -&gt; PHP -&gt; XML -&gt; Flex 2
            Chaos7703 Level 1
            As it is posted right now (resultFormat (rF) = default object with the result assigned directly), I don't receive an error but lastResultValue.text & scrollMenu (a comboBox) both read "$topic->topic."

            With rF = default (object) & topics2AC:ArrayCollection (assigned to the text & CB) I get: "Type Coercion failed: cannot convert "$topic->topic" to mx.collections.ArrayCollection. ..."

            With rF = e4x, data type (dT) = arrayCollection I get: "Error: [RPC Fault faultString="Error #1088: The markup in the document following the root element must be well-formed." faultCode="Client.CouldNotDecode" faultDetail="null"] "
            (e4x is the format that I WANT to use, but anything working will suffice for now)

            rF = xml, dT = AC: "Property data not found on flash.xml.XMLDocument and there is no default value."

            With the "xml version" tag removed rF = e4x, dT = XMLListCollection I get " Error #1034: Type Coercion failed: cannot convert "$topic->topic" to mx.collections.XMLListCollection."

            Likewise with dT=arrayCollection: "Error #1034: Type Coercion failed: cannot convert "$topic->topic" to mx.collections.ArrayCollection." (Same with dT=XMLList)

            The underlying code(source) of the PHP page's output is: "<data><categories><category>Poem</category> ...</categories><topics><topic>Teaching</topic>...<topic>Kids</topic></topics></data>" There's an empty line above the source. The <?xml version="1.0" encoding="utf-8"?> tag is/isn't there depending on whether I leave it in or not. I don't know how/why the empty line is there.

            The general result seems to be that I get either "cannot decode" using e4x, "cannot convert $topic->topic" using anything else. Perhaps the problem is still in the PHP, but Firefox recognizes & treats it as valid XML giving me the "
            This XML file does not appear to have any style information associated with it. The document tree is shown below.


            <data>

            <categories>
            <category>Poem</category>
            <category>Song</category>
            <category>Prayer</category>
            <category>Idea</category>
            <category>Whim</category>
            <category>Enlightenment</category>
            <category>Quote</category>
            <category>Answers</category> ..." when I call the page. So I'm at a loss as to what to even try anymore.

            Thanks again anyone, everyone
            Merry Christmas, Happy Hannukah, Happy Holidays, etc.
            Todd
            • 3. Re: SQL -&gt; PHP -&gt; XML -&gt; Flex 2
              VarioPegged Level 2
              Here's what I know works if you're just looking to use your XML as dataProviders for two combo boxes:

              The XML that's generated by your PHP script works fine for me with a quick mock-up of a MySQL database something similar to your database (at least what I could gather from your post).

              Set the resultFormat="e4x" in your HTTPService like you mentioned you wanted to do.

              Variables to XMLList types...

              [Bindable] private var topic:XMLList;
              [Bindable] private var category:XMLList;

              then, assign the result event values to the variables...

              private function showData(event:ResultEvent):void
              {
              topic = event.result.topics.topic;
              category = event.result.categories.category;
              //Note that "data" is omitted, because we're already at data with event.result.
              //To see what your XML data coming in looks like:

              lastResultValue.text = "Event, Categories: \n";
              lastResultValue.text += category + "\n \n";
              lastResultValue.text += "Event, Topics: \n";
              lastResultValue.text += topic + "\n \n";
              lastResultValue.text += "Result: \n";
              lastResultValue.text += String(event.result.topics.topic);

              }
              ...
              ...

              <mx:ComboBox id="topicCB" dataProvider="{topic}" width="150"/>
              <mx:ComboBox id="categoryCB" dataProvider="{category}" width="150"/>
              <mx:TextArea id="lastResultValue" height="350" width="300" />
              ...

              Like I said, I created a full mock-up of your setup as best I could, and it works just fine for me. Hope it does the same for you.

              TS
              • 4. Re: SQL -&gt; PHP -&gt; XML -&gt; Flex 2
                Chaos7703 Level 1
                All the help is greatly appreciated! Unfortunately I found that the problem was not in any of the expected places...

                THE PROBLEM:

                I am developing locally & I found out that when you're developing on your own computer you have to add an additional argument to the command line of the compiler. In FB under the properties of the project then Flex Compilers -> Additional compiler arguments I added " -use-network=false " & it stopped giving me all the errors. HOWEVER, I learned this after I had already bundled everything up and put it all on a remote server and got it functioning and the two scenarios were NOT equivalent. On the remote platform I was able to use a data type of ArrayCollection for my variable with a resultFormat of the default 'object' (i.e.-no rF specified), but not locally. When I tried to use the same file locally something (I don't remember now) didn't work right & I had to change the resultFormat around and ended up using an XMLList for my variable's data type.

                Thanks again for everyone's help. I did have to convert to XMMList & everything's working fine now (except the million other things I haven't gotten to yet ;)