SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    xml generate problem

    I have this script that I am playing with to create an xml file from a mysql query

    PHP Code:
    <?php

     $file
    fopen("results.xml""w");

     
    $xml_output  "<?xml version=\"1.0\"?>\n";
    $xml_output .= "<gallery>\n";

    for(
    $x $x mysql_num_rows($Recordset1) ; $x++){
        
    $totalRows_Recordset1 mysql_fetch_assoc($Recordset1);
        
    $xml_output .= "\t<listings>\n";
        
    $xml_output .= "\t\t<houseid>" $totalRows_Recordset1['houseid'] . "</houseid>\n";
        
    $xml_output .= "\t\t<username>" $totalRows_Recordset1['username'] . "</username>\n";
        
    $xml_output .= "\t\t<address>" $totalRows_Recordset1['address'] . "</address>\n";
        
    $xml_output .= "\t</listings>\n";
    }

    $xml_output .= "</gallery>";

     
    fwrite($file$xml_output);

     
    fclose($file);

     echo 
    "XML has been written.  <a href=\"results.xml\">View the XML.</a>";

    ?>
    here is the result of a query with one record
    <gallery>
    <listings>
    <houseid/>
    <username/>
    <address/>
    </listings>
    </gallery>

    the result is not listed.
    Here is the result of a query with 2 records

    <gallery>
    <listings>
    <houseid>26</houseid>
    <username>jerry</username>
    <address>55 fake ave</address>
    </listings>
    <listings>
    <houseid/>
    <username/>
    <address/>
    </listings>
    </gallery>

    there is 2 records in that query. For some reason it is only outputing anything over 1 record. yet it is displaying blank tags

    if you see my problem please let me know. I am just trying to write from a simple query now. MY real task is to write the xml from a more advanced query. So I need to figure out the simple problem first
    thanks

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Are you calling mysql_fetch_assoc() anywhere else before the loop? It seams that it's already moved one row forward when you start the loop. Anyway you're better off with:
    PHP Code:
    while($totalRows_Recordset1 mysql_fetch_assoc($Recordset1)){
        
    $xml_output .= "\t<listings>\n";
        
    $xml_output .= "\t\t<houseid>" $totalRows_Recordset1['houseid'] . "</houseid>\n";
        
    $xml_output .= "\t\t<username>" $totalRows_Recordset1['username'] . "</username>\n";
        
    $xml_output .= "\t\t<address>" $totalRows_Recordset1['address'] . "</address>\n";
        
    $xml_output .= "\t</listings>\n"

    Saul

  3. #3
    SitePoint Enthusiast dalar's Avatar
    Join Date
    Jul 2005
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Are you calling mysql_fetch_assoc() anywhere else before the loop? It seams that it's already moved one row forward when you start the loop. Anyway you're better off with:
    PHP Code:
    while($totalRows_Recordset1 mysql_fetch_assoc($Recordset1)){
        
    $xml_output .= "\t<listings>\n";
        
    $xml_output .= "\t\t<houseid>" $totalRows_Recordset1['houseid'] . "</houseid>\n";
        
    $xml_output .= "\t\t<username>" $totalRows_Recordset1['username'] . "</username>\n";
        
    $xml_output .= "\t\t<address>" $totalRows_Recordset1['address'] . "</address>\n";
        
    $xml_output .= "\t</listings>\n"

    Yes I agree, this is much neater than using a for loop and makes more logical sense.


Bookmarks

Posting Permissions

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