Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP and XML help

    Hello all,

    Posted in this forum previously and got some good advice, stumbled across this problem. I am outputting XML which I got PHP to write to a different document called photos.xml however when I review the results it only displays the last entry in my database. I would like it to display all entries, and I'm not sure how to achieve this ..here is my code;

    Code:
    <?php 
    
    $link = mysql_connect("****","****",""); 
    mysql_select_db("****"); 
    
    $query = "SELECT ilink,image_name,description FROM gallery"; 
    
    $results = mysql_query($query); 
    
    
    while($line = mysql_fetch_array($results)) { 
    
    $myFile = "photos.xml";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = "<?xml version=\"1.0\"?>\n"; 
    fwrite($fh, $stringData); 
    $stringData = "<photos path=\"images/\">\n";
    fwrite($fh, $stringData);  
    $stringData = "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
    fwrite($fh, $stringData); 
    $stringData = "</photos>\n"; 
    fwrite($fh, $stringData);   
    fclose($fh);
    } 
    mysql_close($link); 
    ?>
    Thanks in advance

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    try this;

    PHP Code:
    <?php 

    $link 
    mysql_connect("****","****",""); 
    mysql_select_db("****"); 

    $query "SELECT ilink,image_name,description FROM gallery"
    $results mysql_query($query); 

    $stringData "<?xml version=\"1.0\"?>\n"

    while(
    $line mysql_fetch_array($results)) { 

        
    $stringData .= "<photos path=\"images/\">\n";
        
    $stringData .= "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
        
    $stringData .= "</photos>\n"
    }
    mysql_close($link);

    $filename "photos.xml";
    $handle fopen($filename'a');
    fwrite($handle$stringData)
    ?>

  • #3
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Your loop is writing to the file each iteration. You want the xml data and file opening to be outside the loop to begin with, and only the repeated section of the <photo> tags to be inside the loop. likewise the closing of the file needs to be outside the loop, after the } Something like this should work
    PHP Code:
    <?php

    $link 
    mysql_connect("****""****""");
    mysql_select_db("****");

    $query "SELECT ilink,image_name,description FROM gallery";

    $results mysql_query($query);

    $myFile "photos.xml";
    $fh fopen($myFile'w') or die("can't open file");
    $stringData "<?xml version=\"1.0\"?>\n";
    fwrite($fh$stringData);
    $stringData "<photos path=\"images/\">\n";
    fwrite($fh$stringData);


    while (
    $line mysql_fetch_array($results)) {
        
    $stringData "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
        
    fwrite($fh$stringData);
    }
    $stringData "</photos>\n";
    fwrite($fh$stringData);
    fclose($fh);
    mysql_close($link);

    ?>
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #4
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hello there thank you for the response,

    It kind of worked however know it is doubling results and after one entry i get
    <photos>
    <photos>
    <photos>

    when there is only meant to be one

    this is the error coming up in the browser;
    Only one top level element is allowed in an XML document. Error processing resource 'http://localhost/xampp/work/

    <photos path="images/">
    -^

    Thank you if you can help any further

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, try this;
    PHP Code:
    <?php 

    $link 
    mysql_connect("****","****",""); 
    mysql_select_db("****"); 

    $query "SELECT ilink,image_name,description FROM gallery"
    $results mysql_query($query); 

    $stringData "<?xml version=\"1.0\"?>\n"
    $stringData .= "<photos path=\"images/\">\n";

    while(
    $line mysql_fetch_array($results)) { 
        
    $stringData .= "<photo name=\"".$line['image_name']."\" url=\"".$line['ilink']."\">".$line['description']."</photo>\n";
    }

    $stringData .= "</photos>\n"

    mysql_close($link);

    $filename "photos.xml";
    $handle fopen($filename'a');
    fwrite($handle$stringData)
    ?>

  • #6
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you for your help,

    I think I have realised what the problem is my XML outputs like this

    Code:
    <?xml version="1.0"?>
    <photos path="images/">
    <photo name="valentino" url="gold.jpg">valentino in gold.</photo>
    </photos>
    <photos path="images/">
    <photo name="dior4" url="self.jpg">dior new piece</photo>
    </photos>
    and im trying to get it to output in this way

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <photos path="images/">
    	<photo name="Photo 1" url="1.jpg">photo description</photo>	
    	<photo name="Photo 2" url="2.jpg">photo description</photo>	
    	<photo name="Photo 3" url="3.jpg">photo description</photo>	
    	<photo name="Photo 4" url="4.jpg">photo description</photo>	
    	<photo name="Photo 5" url="5.jpg">photo description</photo>	
    	<photo name="Photo 6" url="6.jpg">photo description</photo>	
    	<photo name="Photo 7" url="7.jpg">photo description</photo>
    <photos>
    I think this is the reason it isn't working, thank you for your help so far not sure how to reform my code so it outputs like the above

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Try this method
    PHP Code:
    <?php

    // Create document with header version and encoding type
    $xml = new DOMDocument('1.0''UTF-8');

    // Create root node "photos" with path="images/" attribute
    $photos $xml->createElement('photos');
    $photos->setAttribute('path''images/');

    // Append the root element to the xml document header
    $xml->appendChild($photos);

    // MySQL connection and querying
    mysql_connect('localhost''username''password');
    myqsl_select_db('database');
    $query "SELECT ilink,image_name,description FROM gallery"
    $results mysql_query($query);
     
     
    // Loop through each result
    while($data mysql_fetch_object($results)) {
        
        
    // Create a new "photo" element with the description inside it
        
    $photo $xml->createElement('photo'$data->description);
        
        
    // Append name and url attributes
        
    $photo->setAttribute('name'$data->image_name);
        
    $photo->setAttribute('url'$data->ilink);
        
    //Append the data to the <photos> tag
        
    $photos->appendChild($photo);
    }

    // Format the output nicely (indented)
    $xml->formatOutput true;

    // Write the contents to photos.xml
    file_put_contents('photos.xml'$xml->saveXML());
    It uses the DOM to create the document
    Last edited by JAY6390; 01-20-2010 at 04:57 PM. Reason: Added formatOutput
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #8
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much your amazing!!!!

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    No problem. I think you missed my first post, that would do the same thing, but in the format you used
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •