Results 1 to 3 of 3

Thread: PHP creating an XML file

  1. #1
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP creating an XML file

    Hi all. My first time here.

    I’m very new to PHP, but am having trouble with this project. My task (given by myself) is to pull product information from a product database (OpenCart) and plonk it into an XML file.

    What I still need to do with the file that I’m struggling with:
    It needs to generate the .xml file in the root directory where the .php file is
    It needs to generate the file at 9pm every night

    This is my .php file so far
    Code:
    <?php
    
    header('Content-type: text/xml');
    
    // It will be called downloaded.pdf
    header('Content-Disposition: attachment; filename="products.xml"');
    
    // connect to database
    $db_name = "database";
    $connection = mysql_connect("localhost", "user", "password") or die("Could not connect.");
    
    // querying mysql
    $db = mysql_select_db($db_name, $connection) or die("Could not connect.");;
    
    $result = mysql_query("SELECT * FROM product_description NATURAL JOIN product");
    
    // XML File Creation
    $buf ='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <ROOT>
    <Products>
    ';
    
    while($row=mysql_fetch_array($result)) {
    
          $buf .=  "<Product>
       <ProductCode><![CDATA[ ".htmlentities($row[7]) ." ]]></ProductCode>
       <Title><![CDATA[ ". htmlentities($row[2]) ." ]]></Title>
       <Category><![CDATA[ categories ]]></Category>
       <Price>". htmlentities($row[14]) ."</Price>
       <Quantity>". htmlentities($row[9]) ."</Quantity>
       <Condition>New</Condition>
       <Location>Cape Town</Location>
       <ShippingOption>Speed Services</ShippingOption>
       <ImageURL><![ CDATA[ http://www.digitalsushi.co.za/image/". htmlentities($row[11]) ." ]]></ImageURL>
       <Description><![CDATA[ ".html_entity_decode($row[5]) ." ]]></Description>
     </Product>
     ";
    }
    
    $buf .= "</Products>
    </ROOT>";
    
    echo $buf;
    
    mysql_close($connection);;
    $buf="";
    
    ?>

    Can anyone help me with this?

  2. #2
    Join Date
    Jan 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    $file=fopen("xmlfile.xml","w");
    fwrite($file,$buf);
    fclose($file)

    this is to create a xml file with your products.

    if you need it to run at 9am you need to create an crontab job for you .php script

  3. #3
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you for the reply. Where would I plonk that into the sheet?

    I managed to figure out the cronjob with this code, but I still need to test it.
    Code:
    lynx -source http://digitalsushi.co.za/products.php > /dev/null

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
  •