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
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    coincidentally i've got my xml generator files open as i'm just setting some new ones up. I've been using xml for ages and this seems to work really well I'm sorry for not customising this answer to your question, but this code works.
    PHP Code:

    $i = 0;

    // open xml file
    $file = fopen("data.xml", "w");

    $xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?>\r\n";
    $xml .= "<rss version=\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\">";

    $xml .= "<channel>";

    $xml .= "<title>Buy It Now Items</title>";
    $xml .= "<link>browse_buy_it_now.php</link>";
    $xml .= "<description>A complete list of items available for Buy It Now. These items are all available for immediate purchase</description>";
    //////////////////////////
    //    DO ITEMS          //
    //////////////////////////

    // GET BUY IT NOW ITEMS
    $sql mysql_query(
    "SELECT *.....");

    if (!
    $sql) {
        exit(
    '<p>Error Retrieving Data<br>' 'Error: ' mysql_error() . '</p>');
    }

    while (
    $row mysql_fetch_array($sql)) {
    $i++;
    // build the url
    $id $row['id'];
    $url "http://www.mysite.com/item-$id";
    $pic "http://www.mysite.com/pics/".$row['pict_url'];

    $title $row['title'];

    $desc trim($row['description']);


    $price1 $row['buy_now'];

    $xml .= "<item>\r\n";
    $xml .= "<g:label>a label</g:label>\r\n";
    $xml .= "<title>$title</title>\r\n";
    $xml .= "<link>$url</link>\r\n";
    $xml .= "<g:price>$price</g:price>\r\n";
    $xml .= "<description>$desc</description>\r\n";
    $xml .= "<g:id>$id</g:id>\r\n";
    $xml .= "<g:image_link>$pic</g:image_link>\r\n";
    $xml .= "</item>\r\n";

    // close url loop
    }

    // CLOSE URL SET //
    $xml .= "</channel>";

    $xml .= "</rss>";

    if (!
    fwrite($file$xml)) {
        echo 
    "writing failed";
        } else {
        echo 
    "RIPPA! $i buy it now items added";
        }

    fclose($file);
    Incidentally this is a froogle feed.
    Last edited by wheeler; Jan 29, 2007 at 23:04.
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

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

    thanks

    ill try it out. I appreciate the help


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
  •