Jump to content

Texan78

Members
  • Posts

    272
  • Joined

  • Last visited

Posts posted by Texan78

  1. I know what the topic says and I know some people will say "Did you google it"? I know XML is the language in which RSS feeds are created but what I am trying to do is unique. I have a HTML form that submits data in which that data is used to create an XML file that I use to display data in tables. I chose this route because it's a lot easier and lightweight for what I was needing versus using a database. Now what I am trying to do is create a RSS from that XML file that I parse to display data. Is this even possible and what would be the best approach to do this? When I Google it all I can find is information regarding creating and RSS feed from a Database. Which I am trying to avoid that route.

     

    So is it possible to create a script that will read that XML file then format it as an RSS feed? If so can someone point me to some examples or tutorials?

     

    -Thanks

  2. I have a XML file that I am trying to create a RSS feed from but I am having a hard time adding the headers.

     

    It should look like this....

    <?xml version="1.0"?>
    <rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
      <channel>
        <title>Mesquite Area Storm Reports</title>
        <link>http://www.mesquiteweather.net/wxmesqLSR.php</link>
        <description>Courtesy of Mesquite Weather</description>
        <language>en-us</language>
        <copyright>Mesquite Weather</copyright>
        <ttl>15</ttl>
        <image>
           <url>http://www.mesquiteweather.net/images/App_75_x_75.png</url>
           <width>75</width>
           <height>75</height>
        </image>
    <entrys>
      <reports>
         <timestamp>Wed Jun 5th 10:13 pm</timestamp>
         <name>Unknown</name>
         <location>Mesquite</location>
         <report>WIND DAMAGE</report>
         <description>Enter report description</description>
      </reports>
    </entrys>
    </channel>
    </rss>
    
    

    But instead it comes out like this.

     

    http://www.mesquiteweather.net/xml/mesquite.xml

     

    But if you run the script it is RSS but no data.

     

    http://www.mesquiteweather.net/xml/process.php

     

    Here is the php script....

    <?php
    
    if (isset($_POST['lsr-submit']))
        {
            header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
        }
    
    $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
    $xml = false;if(file_exists('mesquite.xml'))
    $xml = simplexml_load_file('mesquite.xml'); if(!$xml)
    $xml = simplexml_load_string('<entrys/>');
    
    $rssfeed .= '<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">';
    $rssfeed .= '<channel>';
    $rssfeed .= '<title>Mesquite Area Storm Reports</title>';
    $rssfeed .= '<link>http://www.mesquiteweather.net/wxmesqLSR.php</link>';
    $rssfeed .= '<description>Courtesy of Mesquite Weather</description>';
    $rssfeed .= '<language>en-us</language>';
    $rssfeed .= '<copyright>Mesquite Weather</copyright>';
    $rssfeed .= '<image>';
    $rssfeed .= '<url>http://www.mesquiteweather.net/images/App_75_x_75.png</url>';
    $rssfeed .= '<width>75</width>';
    $rssfeed .= '<height>75</height>';
    
    $rssEnd .= '</channel>';
    $rssEnd .= '</rss>';
    
    $name = $_POST['name'];
    $location = $_POST['location'];
    $report = $_POST['report'];
    $description = $_POST['desc'];
    
    $fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
    $lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
    $location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
    $report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
    $description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);
    
    echo $rssfeed;
    
    $reports = $xml->addChild('reports');
    $reports->addChild('timestamp', date("D M jS g:i a",time()));
    $reports->addChild('name', $name);
    $reports->addChild('location', $location);
    $reports->addChild('report', $report);
    $reports->addChild('description', $description);
    
    echo $rssEnd;
    
    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $doc->preserveWhiteSpace = true;
    $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
    $doc->save('mesquite.xml');
    
    ?>
    

    Is there an easier way to do this?

     

     

  3. You're still not hearing me or thinking logically about this.

     

    Yes I do hear you as I clearly stated in my last post, first sentence, "I hear what you're saying and that makes sense". I was just trying to explain what I was trying to do so you would have better information and we were on the same page.

     

    It appears to be working as when I run the script it removed all the entries. I am running a test so I will know more in an about 30 mins.

  4. I hear what you're saying and that makes sense. How it's suppose to work is it is suppose to check the timestamp. If it is X amount from the time now it is suppose to remove that report. This code is code I have found after doing extensive searching. I really haven't been able to find anything solid associated with what I need. So I have just been trying to piece together code from searches.

     

    That is what the line is code is suppose to do. It is just suppose to check the time as a qualifier.

    $timestamp = strtotime($domElement->getAttribute('timestamp'));
    if ($timestamp < strtotime('now - 1 hour')) {    $reports = $domElement;
    

    When I echo the script it shows all the entries it should remove. It is just not removing them.

     

    http://www.mesquiteweather.net/xml/update.php

    <?php
    
    $doc = new DOMDocument;
    $doc->load('mesquite.xml');
    
    $thedocument = $doc->documentElement;
    
    //this gives you a list of the reports
    $list = $thedocument->getElementsByTagName('reports');
    
    //figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
    $reports = null;
    foreach ($list as $domElement){
     $timestamp = strtotime($domElement->getAttribute('timestamp'));
    if ($timestamp < strtotime('now - 1 hour')) {    $reports = $domElement;
    
    //Now remove it.
    if ($reports != null)
    $thedocument->removeChild($reports);
    
      }
    }
    
    echo $doc->saveXML();
    ?>
  5. When I run it this is what it's echo'ing

     

    http://www.mesquiteweather.net/xml/update.php

     

     

    Timestamp is an element in your XML document, not an attribute

     

    True, but if you look at that line carefully it is "$domElement->getAttribute" and "$reports = $domElement;" which means it should be looking for the element "timestamp" then get the attribute of timestamp being the date/time.

     

    This is what's in the script that creates the data for the entries.

    $reports->addChild('timestamp', date("D M jS g:i a",time()));
    
  6. How often is the XML updated? Can you modify whatever reads it? Because you might be able to make the reader skip over old entries when displaying the contents, then make whatever updates it also remove old entries. No cron required.

     

    The XML is updated manually by persons who are submitting a storm report from a form on one of the pages. So there is really only data when there is a storm. Which is why I was wanting to delete it after certain amount of time so there isn't old reports just sitting there for weeks if there is no storms.

     

    I like that idea of no cron. It's not a big deal having to set up one though. I just figured having a separate file to remove entries then call it with a cron would be easier to keep the code separated. I am not sure how easy that would be to implement into the code that displays the data.

     

    This is what I have come up with so far but it's not removing anything. It should remove it all of it because it's all over an hour but it doesn't.

    <?php
    
    $doc = new DOMDocument;
    $doc->load('mesquite.xml');
    
    $thedocument = $doc->documentElement;
    
    //this gives you a list of the reports
    $list = $thedocument->getElementsByTagName('reports');
    
    //figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
    $reports = null;
    foreach ($list as $domElement){
     $timestamp = strtotime($domElement->getAttribute('timestamp'));
    if ($timestamp < strtotime('now - 1 hour')) {    $reports = $domElement;
    
    //Now remove it.
    if ($reports != null)
    $thedocument->removeChild($reports);
    
      }
    }
    
    echo $doc->saveXML();
    ?>
    

    When I run it this is what it's echo'ing

     

    http://www.mesquiteweather.net/xml/update.php

     

    Which seems to be adding entries.

     

    Here is the raw XML

     

    http://www.mesquiteweather.net/xml/mesquite.xml

  7. I can't edit my original post so I will just add it here. This is the code I have come up with but a little stuck. I was thinking of a php script that is run by a CRON that will load the XML and look for any entries older than 12 hours and remove them then resave the XML. Where I am stuck is I am not sure how set it to check for entries older than 12 hours. Am I on the right track? Any suggestions?

    <?php
    
    $doc = new DOMDocument; 
    $doc->load('mesquite.xml');
    
    $thedocument = $doc->documentElement;
    
    //this gives you a list of the reports
    $list = $thedocument->getElementsByTagName('reports');
    
    //figure out which ones you want -- assign it to a variable (ie: $nodeToRemove )
    $nodeToRemove = null;
    foreach ($list as $domElement){
      $attrValue = $domElement->getAttribute('timestamp');
      if ($attrValue == 'VALUEYOUCAREABOUT') {
        $nodeToRemove = $domElement; //will only remember last one- but this is just an example 
      }
    }
    
    //Now remove it.
    if ($nodeToRemove != null)
    $thedocument->removeChild($nodeToRemove);
    
    echo $doc->saveXML(); 
    ?>
    

    Here is an example of the XML

    <entrys>
    −
    <reports>
    <timestamp>Mon Jun 3rd 7:34 am</timestamp>
    <fname>Unknown</fname>
    <lname/>
    <location/>
    <report>WIND DAMAGE</report>
    <description>Enter report description</description>
    </reports>
    −
    <reports>
    <timestamp>Tue Jun 4th 12:02 pm</timestamp>
    <name/>
    <location>Mesquite</location>
    <report>GENERAL</report>
    <description>Test of the form input. </description>
    </reports>
    </entrys>
  8. I have done a search but I can't really find anything related to what I am needing. I am not sure if I am searching for it correctly or not. I have an XML file that is created via PHP and populated with data from a form. What I am trying to do is delete certain entries in that XML file after X amount of time. Is there an easy way to do this or can it even be done at all. I was thinking of some php script that was run by a CRON to check the XML file and delete certain entries by the timestamp after X amount of time. Can someone provide some suggestions or get me pointed in the right direction?

     

    -Thanks!

     

  9. I got it working correctly with my code without having to completely rewrite it with two simple edits and without having to go back to tweek it to work with my html form and display script.

     

    All I had to do was replace this....

    $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
    $xml = simplexml_load_string($str);
    
    

    With this and works great and is simple and not complex without a complete code overhaul.

    $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
    $xml = false;if(file_exists('mesquite.xml'))
    $xml = simplexml_load_file('mesquite.xml'); if(!$xml) $xml = simplexml_load_string('<entrys/>');
    

    Like I said, had you read my OP and then update you would have noticed it didn't need a complete overhaul. Thanks though!

  10. So figure out why.

     

    Uh, I think that is what I am trying to do....

     

    So add some logic to capture that event, and create the needed node.

     

    I guess you didn't read all of this or see my code or you have no idea ether or you will see I had tried that in my OP.

     

    But thanks for stating the obvious.

  11. Are you not reading anything I am posting. It is clear as day, I don't know how much clearer I can make it. Have you read everything and visited the links I posted to demo to see how it's setup?

     

    Are you testing this code because it doesn't work. Just to amuse you I tried your code. It does the EXACT same thing as the code I have now does. So I guess I will waste some more time explaining this.

     

    I used YOUR code which works great for submit TO THE XML file FROM the form IF THERE IS AN EXISTING entry like my original code worked so this is what the XML file looked like with an existing entry.....

    <entry>
    −
    <reports>
    <timestamp>Fri May 31st 11:50 pm</timestamp>
    <fname>Mesquite</fname>
    <lname>Weather</lname>
    <location>Vanston Park</location>
    <report>POWER OUTAGE</report>
    <description>Oops I did it again!</description>
    </reports>
    </entry>
    

    Then it would append the XML file...

     

    BUT... Once you completely delete the contents of the XML file NOT the actual file like so....

     

    http://www.mesquiteweather.net/xml/test3.xml

     

    Then you are unable to submit TO the XML file FROM the form.

     

    Go ahead and try it.

     

    http://www.mesquiteweather.net/wxmesqLSR-report.php

     

    Once you submit it, it will take you to a page where it should be displayed below the map and it isn't working which works great IF there is an existing entry. Then check the XML file after you have submitted and you will see nothing was wrote to it.

     

    It will only submit OR append if there is an existing entry in the XML file. As you can see the XML file DOES exist. Your code did exactly what my previous code was doing which WORKS GREAT if there is an existing entry but, once the XML file is empty then no.

     

    I appreciate your help but please read my posts carefully because had you, you would have known this already and both yours and my time wouldn't have been wasted.

     

    -Thanks!

  12. You never said you needed to populate the form using data from the XML file. The problem you specified was that you had an issue appending to an XML file.

     

    Does this mean you want to populate the form with the first entry or the last entry? 

    Do you want to overwrite what's already in the XML file or append to it as they are two completely different tasks?

     

     

    Please go read the first paragraph in my OP carefully.

     

    Then read the UPDATE to my OP carefully from this post.

     

    http://forums.phpfreaks.com/topic/278650-append-xml-file-with-php/?do=findComment&comment=1433506

     

    And you're right. I never said "I needed to populate the form using data from the XML file". I am using a form which I posted a link to from my OP to populate date TO the XML file as I stated in my first sentence in my OP. Had you read my posts carefully and visited the links I posted to the example you would have seen this and how it's suppose to work. The problem I WAS having is I couldn't append data FROM the form TO the XML file. Every time I would make a new submission it would overwrite the existing entry instead of appending the new submission TO the XML file. I have since fixed that issue.

     

    Problem now is that there has to be at least one entry in the XML file or the form won't submit TO the XML file. I need to be able to submit FROM the form TO the XML when it is empty but I am unable to do that as it won't submit TO the XML file unless there is at least one entry. Which in turn if there is no submissions in weeks it will show at least one outdated report in the table on this page. So if there is no recent reports in X amount of time the XML file needs to be empty but, that is different issue I will sort out later but in order to do that I have to sort this out first.

     

    http://www.mesquiteweather.net/wxmesqLSR.php

     

    So I need to be able to submit from the form TO the XML file when it's empty and when it's empty it needs to show nothing in the file. As it stands now if it is empty it just shows empty elements. Which causes two other issues, one being not able to submit FROM the form TO the XML file.

  13.  

    unless I've completely misinterpreted your OP.

     

     

    I think you may have. That's why I said read this post which is an update to my OP.

     

    http://forums.phpfreaks.com/topic/278650-append-xml-file-with-php/?do=findComment&comment=1433506

     

     

    Like I said this script WORKS exactly like I need it to BUT, there has to be at least ONE entry in the XML or the HTML form won't submit. I need to be able to submit to the XML when it's empty.

    <?php
    
    if (isset($_POST['lsr-submit']))
        {
            header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
        }
    
    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $location = $_POST['location'];
    $report = $_POST['report'];
    $description = $_POST['desc'];
    
    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->load('mesquite.xml');
    
    $element = $xml->getElementsByTagName('reports')->item(0);
    
    $timestamp = $element->getElementsByTagName('timestamp')->item(0);
    $fname = $element->getElementsByTagName('fname')->item(0);
    $lname = $element->getElementsByTagName('lname')->item(0);
    $location = $element->getElementsByTagName('location')->item(0);
    $report = $element->getElementsByTagName('report')->item(0);
    $description = $element->getElementsByTagName('description')->item(0);
    
    $newItem = $xml->createElement('reports');
    
    $newItem->appendChild($xml->createElement('timestamp', date("D M jS g:i a",time())));;
    
    $newItem->appendChild($xml->createElement('fname', $_POST['firstname']));
    $newItem->appendChild($xml->createElement('lname', $_POST['lastname']));
    $newItem->appendChild($xml->createElement('location', $_POST['location']));
    $newItem->appendChild($xml->createElement('report', $_POST['report']));
    $newItem->appendChild($xml->createElement('description', $_POST['desc']));
    
    $xml->getElementsByTagName('entry')->item(0)->appendChild($newItem);
    
    $xml->save('mesquite.xml');
    
    ?>
    

    ALSO when it's empty it will still show the elements but they are empty. When that happens it shows an empty table instead of my "No reports at this time" table from my script that I use to display the data from the XML file which you can see from the links in my OP. Which works because I have tested it BUT, it won't show unless the XML is empty. It can't be empty or the form won't submit to the XML. That's the issue I am having right now.

     

    Thanks for the help but, please all the updates carefully as my OP has been solved with the exception of the small issue I have listed. I don't want to have to rewrite my entire working code if I don't have to.

     

    http://forums.phpfreaks.com/topic/278650-append-xml-file-with-php/?do=findComment&comment=1433506

  14.  

    Sorry... what?

     

     

    Great answer, care to share why?

     

    If you NEED to use an XML file I suggest you do the following:

    1. Break your problem down into small problems, i.e. I need to create a file if it doesn't exist and append to one if it does.
    2. Tackle each problem individually, one-at-a-time.
    3. Comment your code so you, and others, know whats going on.
    4. Change "reports" to "report" as it contains report information not many reports.
    5. Define a Document Type Definition (DTD) to create a fully qualified XML document.

    Once you've broken the larger problem down into smaller ones you will, hopefully, find its actually a really easy task.

     

    Let me get you started

    // Does the file exist?
    if(file_exists($file)) {
        // Yes, lets get its contents
        $xml = file_get_contents($file);
    } else {
        // No, lets set up a default
        $xml = "<entries></entries>"; //Should also contain the DTD and the XML definition. Perhaps have a default XML file.
    }
    
    
    // Lets load the XML into a DOMDocument object...
    $dom = DOMDocument::loadXML($xml);
    
    
    // Now lets append entries. This will be the same regardless of whether or not the file already exists.

     

     

    Please read this again carefully

     

    http://forums.phpfreaks.com/topic/278650-append-xml-file-with-php/?do=findComment&comment=1433506

  15. Ok well I have kept plugging away and I have a working script that allows me to add entries from the submission form without it overwriting the current one. Only issue I am having is in order for it to work there already has to be an existing entry.

     

    So at all times there has to be something in there in order to submit to it. That won't work for me because if there isn't a report I have a table that displays that there is no reports. In order for that message to display the XML file needs to be empty but it can't be empty otherwise the form won't submit to it. So that is the pickle I am in now.

     

    I need it to where I don't always have to have a entry in there like this.

    <entry>
    −
    <reports>
    <timestamp>Fri May 31st 11:50 pm</timestamp>
    <fname>Mesquite</fname>
    <lname>Weather</lname>
    <location>Vanston Park</location>
    <report>POWER OUTAGE</report>
    <description>Oops I did it again!</description>
    </reports>
    </entry>
    

    I need this instead if there is no entry and have the ability to submit a new entry without there being a current entry in the XML file.

    <entry>

    This is the current working script that I have right now.

    <?php
    
    if (isset($_POST['lsr-submit']))
        {
            header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
        }
    
    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $location = $_POST['location'];
    $report = $_POST['report'];
    $description = $_POST['desc'];
    
    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->load('mesquite.xml');
    
    $element = $xml->getElementsByTagName('reports')->item(0);
    
    $timestamp = $element->getElementsByTagName('timestamp')->item(0);
    $fname = $element->getElementsByTagName('fname')->item(0);
    $lname = $element->getElementsByTagName('lname')->item(0);
    $location = $element->getElementsByTagName('location')->item(0);
    $report = $element->getElementsByTagName('report')->item(0);
    $description = $element->getElementsByTagName('description')->item(0);
    
    $newItem = $xml->createElement('reports');
    
    $newItem->appendChild($xml->createElement('timestamp', date("D M jS g:i a",time())));;
    
    $newItem->appendChild($xml->createElement('fname', $_POST['firstname']));
    $newItem->appendChild($xml->createElement('lname', $_POST['lastname']));
    $newItem->appendChild($xml->createElement('location', $_POST['location']));
    $newItem->appendChild($xml->createElement('report', $_POST['report']));
    $newItem->appendChild($xml->createElement('description', $_POST['desc']));
    
    $xml->getElementsByTagName('entry')->item(0)->appendChild($newItem);
    
    $xml->save('mesquite.xml');
    
    ?>
    

    Any suggestions or ideas?

  16. I am not sure I follow you. The script works great and does what I need it to do except I can only add one entry at a time. If a new entry is submitted from the form it just overwrites the current entry. It doesn't add the entry to the existing entries.

     

    Without this....

    $xml->reports = "";
    

    It won't add any entries to the XML from the form.

     

    I have since change it to this which works too.

    $reports = $xml->addChild('reports');
    $reports->addChild('timestamp', date("D M jS g:i a",time()));
    $reports->addChild('fname', $fname);
    $reports->addChild('lname', $lname);
    $reports->addChild('location', $location);
    $reports->addChild('report', $report);
    $reports->addChild('description', $description);
    
  17. I have a php script that creates an XML file with data from a HTML form. It works great and the XML file is created and the form writes data to it. I am having a hard time appending data to the XML file though. Every time I submit new data from the form to the XML file it just overwrites the last one instead of just adding that data to it.

     

    Here is the php script I am using to create and write the XML file from the form.

    <?php
    
    if (isset($_POST['lsr-submit']))
        {
            header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
        }
    
    $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
    $xml = simplexml_load_string($str);
    
    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $location = $_POST['location'];
    $report = $_POST['report'];
    $description = $_POST['desc'];
    
    $fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
    $lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
    $location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
    $report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
    $description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);
    
    $xml->reports = "";
    $xml->reports->addChild('timestamp', date("D M jS g:i a",time()));
    $xml->reports->addChild('fname', $fname);
    $xml->reports->addChild('lname', $lname);
    $xml->reports->addChild('location', $location);
    $xml->reports->addChild('report', $report);
    $xml->reports->addChild('description', $description);
    
    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $doc->preserveWhiteSpace = true;
    $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
    $doc->save('test2.xml');
    
    ?>
    

    Here is the XML output

     

    http://www.mesquiteweather.net/xml/test2.xml

     

    HTML Form in case anyways wants to see the behavior after new data is submitted.

     

    http://www.mesquiteweather.net/wxmesqLSR-report.php

     

    -Thanks!

  18. Ok, I got it working with one small exception. Now the only problem is when a new submission is added it overwrites the previous entry. I need it to add the newest submission to the XML file and not over ride it and store it for X amount of time.

    Here is the working php script that creates the xml file and takes the data from the HTML form and puts it in the XML file.

    <?php
    
    if (isset($_POST['lsr-submit']))
        {
            header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
        }
    
    $str = '<?xml version="1.0" encoding="UTF-8"?><entrys></entrys>';
    $xml = simplexml_load_string($str);
    
    $fname = $_POST['firstname'];
    $lname = $_POST['lastname'];
    $location = $_POST['location'];
    $report = $_POST['report'];
    $description = $_POST['desc'];
    
    $fname = htmlentities($fname, ENT_COMPAT, 'UTF-8', false);
    $lname = htmlentities($lname, ENT_COMPAT, 'UTF-8', false);
    $location = htmlentities($location, ENT_COMPAT, 'UTF-8', false);
    $report = htmlentities($report, ENT_COMPAT, 'UTF-8', false);
    $description = htmlentities($description, ENT_COMPAT, 'UTF-8', false);
    
    $xml->reports = "";
    $xml->reports->addChild('fname', $fname);
    $xml->reports->addChild('lname', $lname);
    $xml->reports->addChild('location', $location);
    $xml->reports->addChild('report', $report);
    $xml->reports->addChild('description', $description);
    
    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    $doc->preserveWhiteSpace = true;
    $doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
    $doc->save('test2.xml');
    
    ?>
    

    Here is the xml file it creates.

     

    http://www.mesquiteweather.net/xml/test2.xml

     

    Here is the form to submit to the XML file. Submit a test submission and it takes you to the page to display but you'll notice it will overwrite the last one instead of adding to the XML file.

     

    http://www.mesquiteweather.net/wxmesqLSR-report.php

     

    So how do I add to the XML without it overwriting the last one and keep them for X amount of time.

  19. Ok I have been doing some more research and this is what I am testing now which creates a XML file like I need and it's formatted properly. Issue now is how do I get the data from the submission of the form into this XML?

    <?php
    
    if (isset($_POST['lsr-submit']))
        {
            header('Location: http://www.mesquiteweather.net/wxmesqLSR.php');
        }
    
    function createFile($xml_file)
    {
        // create new dom document
        $xml = new DOMDocument();
    
        // these lines would create a nicely indented XML file
        $xml->preserveWhiteSpace = false;
        $xml->formatOutput = true;
    
        // create a root element, and add it to DOM
        addRoot($xml);
    
        // add more elements to xml file
        $reports = $xml->createElement("reports");
    
        // add this element to the root
        $xml->documentElement->appendChild($reports);
    
        // create elements
        $fname = $xml->createElement("fname");
        $lname = $xml->createElement("lname");
    
        // append these elements to friend
        $reports->appendChild($fname);
        $reports->appendChild($lname);
    
        // save dom document to an xml file
        $xml->save($xml_file);
    }
    
    function addRoot(&$xml)
    {
        $xml->appendChild($xml->createElement("entry"));
    }
    
    // call xml function
    createFile('out.xml');
    
    ?>
    

    Here is the form...

    <form name="lsrReports" action="xml/test.php" method="post">
    <table width="50%" align="center" cellpadding="2" cellspacing="0">
      <tr>
        <td> First name:</td><td> <input type="text" name="firstname"></td>
        <td> Last name:</td><td>  <input type="text" name="lastname"></td>
      </tr>
      <tr>
        <td> Location:</td><td> <input type="text" name="location"></td>
        <td> Report:</td><td> <select name="report">
                                 <option value="Wind Damage" selected>Wind Damage</option>
                                 <option value="Hail">Hail</option>
                                 <option value="Flooding">Flooding</option>
                                 <option value="Power Outage">Power Outage</option>
                                 <option value="General">General</option>
                              </select>
        </td>
      </tr>
      <tr>
         <td> Description: </td><td colspan="4"> <textarea rows="5" cols="65" name="desc" onfocus="this.value=''">Enter report description</textarea></td>
      </tr>
      <tr>
         <td colspan="4" style="text-align:center;"><input type="submit" name="lsr-submit" value="Submit"></td>
      </tr>
    </table>
    </form>
    
    

    Now the HTML form is suppose to submit to the XML file then display the output to this page via the XML file. I have the page to display the XML set up, that's not the problem. I just need help with getting the info from the Form to the XML file.

     

    http://www.mesquiteweather.net/wxmesqLSR.php

     

    Here is the test XML file that is created from the script that the form is submitting too.

     

    http://www.mesquiteweather.net/xml/out.xml

     

     

    -Thanks!

  20. This might be confusing so I am going to do my best at describing this.

     

    I have a simple HTML form that I am trying to create an XML file from the data. Here is the link to the form.

     

    http://www.mesquiteweather.net/wxmesqLSR-report.php

     

    This is the php script I am using to create the XML which works if you don't redirect after the form has been submitted but, I need it to redirect so you can view the data. It populates with the data if you don't redirect. Problem is it doesn't show on the page it needs to be viewed on. Also once  data has been entered it deletes the previous data.

    <?php
    if (isset($_POST['lsr-submit']))
        {
            header('Location: wxmesqLSR.php');
        }
    header('Content-type: text/xml');//Tell the browser it's an XML document
    ob_start();
    echo '<?xml version="1.0"?'.'>'."\n";//We have to break up the XML closing tag because [?]> is the closing tag for PHP
    echo "<entry>
     <reports>
       <fname>{$_POST['firstname']}</fname>
       <lname>{$_POST['lastname']}</lname>
       <location>{$_POST['location']}</location>
       <report>{$_POST['report']}</report>
       <description>{$_POST['desc']}</description>
     </reports>
    </entry>";
    ?>
    

    What would be a good way to achieve what I am trying to do? I just need people to be able to enter info in the form and store it in a XML file and retain it for X amount of time. If I manually create a XML file the data displays fine so I know it is reading the XML file.

     

    -Thanks

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.