close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 or 2018 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Using DataAssist Query to export to XML

Thread began 5/20/2010 8:30 pm by tom92909 | Last modified 5/21/2010 5:34 pm by tom92909 | 728 views | 3 replies |

tom92909Beta Tester

Using DataAssist Query to export to XML

I have a DataAssist query that is working.
I have a fwrite to XML export process using same DataAssist query working, but not as desired.

php:
<?php
mysql_select_db
($database_dbconnect$dbconnect);
$query_Recordset1 "SELECT * FROM mytable GROUP BY name";
$Recordset1 mysql_query($query_Recordset1$dbconnect) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?>
<?php
$fp 
fopen('example.xml''w+');
fwrite($fp'<?xml version="1.0" encoding="utf-8" ?>');
fwrite($fp'<content>');
?>
<?php 
do {
fwrite($fp'<item Name="'.$row_Recordset1['name'].'">');
fwrite($fp'<image="'.$row_Recordset1['image'].'">');
fwrite($fp'<text>');
fwrite($fp'<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>');
fwrite($fp'</text>');
fwrite($fp'</image>');
fwrite($fp'</item>');
} while (
$row_Recordset1 mysql_fetch_assoc($Recordset1));
?>
<?php
fwrite
($fp'</content>');
fclose($fp);
?>




What I expect as a result is something like this...

html:
<?xml version="1.0" encoding="utf-8" ?>
<content>
<item Name="FRUIT">
<image="apple.jpg">
<text>
<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>
</text>
</image>
<image="orange.jpg">
<text>
<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>
</text>
</image>
<image="pear.jpg">
<text>
<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>
</text>
</image>
</item>
<item Name="Animals">
<image="dog.jpg">
<text>
<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>
</text>
</image>
<image="cat.jpg">
<text>
<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>
</text>
</image>
</item>
<item Name="Flowers">
<image="rose.jpg">
<text>
<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>
</text>
</image>
<image="daisy.jpg">
<text>
<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>
</text>
</image>
</item>
</content>



If I use ORDER BY in stead of GROUP BY I get something closer to what I'm looking for, but it needs only to send the <item> only once per/match.

I think my query is the problem... What I'm getting instead of the above desired result is only the first record of each ("GROUP BY") <item> name. Which is exactly what the query is asking for...I need to write this XML file to match up all of the records that have the same names, write those records close that </item> and then move to the next set of <item> name matches and so on until the query is done.

What am I missing here?

Sign in to reply to this post

tom92909Beta Tester

I've slept on it. Good morning btw. :)

Thinking out loud...

What if I created a 2nd recordset that only looked at the <item> names, and then I included another loop before the detailed loop occurs....?

php:
<?php do {{

fwrite($fp'<item Name="'.$row_Recordset2['name'].'">');
} while (
$row_Recordset2 mysql_fetch_assoc($Recordset2));
fwrite($fp'<image="'.$row_Recordset1['image'].'">');
fwrite($fp'<text>');
fwrite($fp'<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>');
fwrite($fp'</text>');
fwrite($fp'</image>');
fwrite($fp'</item>');
} while (
$row_Recordset1 mysql_fetch_assoc($Recordset1));
?>



Not sure if my syntax is even close, nor do I have time to test this out, but hopefully when I get back later today, someone will have commented on this and then I'll test it as well...

Have a great day folks!

Sign in to reply to this post

Jason ByrnesWebAssist

only the one recordset is needed.


Hers what you need to do:
Before the loop create a variable for the name, and set it to blank
<?php $nameStr = ""; ?>

inside the loop add an if statement to determine if the name tag should be written. and set the value of nameStr to the value ion the recordset.

the first time, you wont need to close the name tab, but subsequent times through, you will

php:
if($nameStr != $row_Recordset1['name']) {

  if($nameStr != "") fwrite($fp, '</item>');
  fwrite($fp, '<item Name="'.$row_Recordset1['name'].'">');
  $nameStr = $row_Recordset1['name'];
}



full code:

php:
<?php

mysql_select_db
($database_dbconnect$dbconnect);
$query_Recordset1 "SELECT * FROM mytable GROUP BY name";
$Recordset1 mysql_query($query_Recordset1$dbconnect) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);
?>
<?php
$fp 
fopen('example.xml''w+');
fwrite($fp'<?xml version="1.0" encoding="utf-8" ?>');
fwrite($fp'<content>');
?>
<?php $nameStr 
""?>
<?php 
do {
if(
$nameStr != $row_Recordset1['name']) {
  if(
$nameStr != ""fwrite($fp'</item>');
  
fwrite($fp'<item Name="'.$row_Recordset1['name'].'">');
  
$nameStr $row_Recordset1['name'];
}
fwrite($fp'<image="'.$row_Recordset1['image'].'">');
fwrite($fp'<text>');
fwrite($fp'<![CDATA[Lorem ipsum dolor sit amet, consectetuer.]]>');
fwrite($fp'</text>');
fwrite($fp'</image>');
} while (
$row_Recordset1 mysql_fetch_assoc($Recordset1));
fwrite($fp'</item>');
?>
<?php
fwrite
($fp'</content>');
fclose($fp);
?>
Sign in to reply to this post

tom92909Beta Tester

Outstanding recommendation! It worked perfectly!

:D

Thank you very much!

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...