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

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Do while loop not adjusted for output

Thread began 5/05/2010 5:38 pm by tom92909 | Last modified 5/06/2010 12:40 pm by Justin Nemeth | 948 views | 3 replies |

tom92909Beta Tester

Do while loop not adjusted for output

I've got a do while loop that I'm using to read a SQL query and then write a XML file to my server...

php:
<?php
$fp 
fopen('output.xml''w');
fwrite($fp'<?xml version="1.0" encoding="utf-8"?>');
?>
<?php 
do { 
fwrite($fp'<gallery Name="'.$row_Recordset1['name'].'">');
fwrite($fp'<image file="'.$row_Recordset1['directory'].'/'.$row_Recordset1['photo'].'">');
fwrite($fp'</image>');
fwrite($fp'</gallery>');
} while (
$row_Recordset1 mysql_fetch_assoc($Recordset1)); 
?>
<?php
fclose
($fp);
?>



The above creates a properly formatted XML file that works with my application, however, if I have multiple records in the same <gallery> I need the loop to include all of the <image></image> records before closing the </gallery> and moving on to the next <gallery>


MySQL Query on the Gallery_contents Joining the Gallery listing ON gallery IDs.

php:
mysql_select_db($database_ba, $dbconnect);
$query_Recordset1 = "SELECT * FROM gallery_content INNER JOIN gallery ON gallery.rec_id = gallery_content.gallery_id";
$Recordset1 = mysql_query($query_Recordset1, $dbconnect) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);




The desired XML output example below would have (2) Gallery Groups with (2) photos in each group.

html:
<?xml version="1.0" encoding="utf-8"?>
<gallery Name="Special Events">
<image file="Gallery2/sample1.jpg">
</image>
<image file="Gallery2/sample2.jpg">
</image>
</gallery>
<gallery Name="Just for Fun">
<image file="Gallery1/landscape.png">
</image>
<image file="Gallery1/portrait.png">
</image>
</gallery>
</content>




Do I create two loops here? I'm not sure how to accomplish this.

Thanks in advance.

Sign in to reply to this post

Jason ByrnesWebAssist

change this part of the code:

php:
<?php do { 

fwrite($fp'<gallery Name="'.$row_Recordset1['name'].'">');
fwrite($fp'<image file="'.$row_Recordset1['directory'].'/'.$row_Recordset1['photo'].'">');
fwrite($fp'</image>');
fwrite($fp'</gallery>');
} while (
$row_Recordset1 mysql_fetch_assoc($Recordset1)); 
?>




to:

php:
<?php $name ""?>

<?php 
do { 
if(
$row_Recordset1['name'] != $name) {
  if(
$name != ""fwrite($fp'</gallery>');
  
fwrite($fp'<gallery Name="'.$row_Recordset1['name'].'">');
  
$name $row_Recordset1['name'];
}
fwrite($fp'<image file="'.$row_Recordset1['directory'].'/'.$row_Recordset1['photo'].'">');
fwrite($fp'</image>');

} while (
$row_Recordset1 mysql_fetch_assoc($Recordset1)); 
fwrite($fp'</gallery>');
?>
Sign in to reply to this post

tom92909Beta Tester

Outstanding, it worked marvelously!
I owe you a beer!

Sign in to reply to this post

Justin Nemeth

Not really related to original issue, but I would suggest just building up a string, then doing one fwrite call at the end. That will be more more efficient and will reduce the number of file writes quite a bit.

-justin

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...