1

so my problem is, that I'm trying to make an application, that will mostly retrieve data from a MySQL database, divided into tables with particular date the data was created. Right now, I have a script for PHP, that allows me to extract the data from the database and put it into an XML file. The problem with that is, that I don't have it date-ordered, which is very important in the app, but I don't have any idea, how would I make a script, that would extract the data from MySQL db and put the data into this format:

<allitems>
    <items date="29-11-2011">
        <item>
            <title>Something</title>
            <title2>Something else</title2>
        </item>
        <item>
            <title>Something2</title>
            <title2>Something else2</title2>
        </item>
        <item>
            <title>Something3</title>
            <title2>Something else3</title2>
        </item>
    </items>
</allitems>

Whereas there would be more groups, each with different date attribute, and each created from a different table, but all this data in one XML file.

By the way, this is my PHP script for what I use right now:

$query = mysql_query("SELECT * FROM test_codes");
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<items>";

$fp = fopen($filename,"w");

for($x = 0 ; $x < mysql_num_rows($query) ; $x++){
    $row = mysql_fetch_assoc($query);

    $xml_output .= "\t<item>\n"; 
    $xml_output .= "\t\t<title>" . $row['title'] . "</title>\n";
    $xml_output .= "\t\t<code>" . $row['code'] . "</code>\n";
    $xml_output .= "\t\t<quantity>" . $row['quantity'] . "</quantity>\n"; 
    $xml_output .= "\t\t<price>" . intval($row['price'])*intval($row['quantity']) . "</price>\n"; 
    $xml_output .= "\t</item>\n";
}

$xml_output .= "</items>";

fwrite($fp,$xml_output);
fclose($fp);

And this is a picture, of how I mean the data usage in my flex application (in a list): http://img855.imageshack.us/img855/9291/sofxml.png

5
  • Don't really get the point to your question. You have a MySQL database and a php server. You're going to use php anyway to extract data from the database. Why do you want to send this to Flex and then create a xml? You can create an xml in php and send THAT to the flex app. For this, I will answer your question soon Commented Nov 30, 2011 at 17:02
  • I want the server to create an XML file and then send it to the flex app, the thing is I don't really know, how to create the xml if I have more than 1 of the <items> nodes. I basically need to know, how to output the xml, so I have more then 1 <items> node and more then 1 <item> nodes in it, and then how to read the attributes (<items date="something">) in the flex app. That's all. Commented Nov 30, 2011 at 17:24
  • I checked it, but I don't think it quite answers my question. I don't know the php script for what I mentioned, and I need to use the data retrieved from XML best in bindable form (label="{data.title}"). Your answer explains how I would retrieve the attribute, but I mostly need to know the PHP script for creating the XML. Commented Nov 30, 2011 at 17:30
  • Then this is totally not related to flex? Commented Nov 30, 2011 at 17:46
  • It is related to flex, but mostly to PHP. I put flex as one of the tags, because the question was also related to flex, but I need rather an answer on how to insert the data into list, as I showed on my picture: img855.imageshack.us/img855/9291/sofxml.png Commented Nov 30, 2011 at 18:04

2 Answers 2

1
// query ordered by date
$query = mysql_query("SELECT * FROM test_codes ORDER BY 'date' DESC");
$orderedResult = array();

// group the items by date
$queryResult = mysql_fetch_asoc($query);
foreach($queryResult as $row) {
  $orderedResult[$row['date']][] = $row;
}

// render output by date
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<allitems>";

$fp = fopen($filename,"w");

foreach($orderedResult as $key => $dateItems) {
  $xml_output .= '<items date="' . $key . '">';
  foreach($dateItems as $row) {
    $xml_output .= '<item>';
        $xml_output .= '<title>' . $row['title'] . '</title>';
        $xml_output .= '<code>' . $row['code'] . '</code>';
        $xml_output .= '<quantity>' . $row['quantity'] . '</quantity>';
    $xml_output .= '</item>'
  }
  $xml_output .= '</items>';
}

$xml_output .= "</allitems>";

fwrite($fp,$xml_output);
fclose($fp);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's what I needed! Right now, I just have to find out how to use the 'date' attribute in flex.
1

Suppose you have a php script which pulls the data from mySQL and responds with a xml (like you have given in your question) at http://myserver.com/myphp.php?arg1=val1&arg2=val2

You'll use a HTTPService to get that

private var service:HTTPService;

//Run this on application initialize
private function init() {
    service=new HTTPService();
    service.addEventListener(ResultEvent.RESULT, sResult);
    service.resultFormat="xml";
    service.url="http://myserver.com/myphp.php?arg1=val1&arg2=val2";
}

private function sResult(e:ResultEvent):void {
    var o:Object = xmlStringToObject((e.result as XMLNode).toString());
    //Now you have your xml as an object
    trace(o.allitems.items.getItemAt(0).date); //gives 29-11-2011
    trace(o.allitems.items.getItemAt(0).item.getItemAt(0)); //{title: Something, title2: Something else}
    //You get the pattern, if not, debug and watch o
}

public function xmlStringToObject(xmlStr:String):Object{
    var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
    xmlDoc.ignoreWhite=true;
    var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
    var resultObj:Object = decoder.decodeXML(xmlDoc);
     return resultObj;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.