简体   繁体   中英

Append to XML file with php SimpleXML

I'm working on a help ticket project and I have a good portion of it already working except this one aspect. I would like to display all ticket information (from a XML file) to the screen but then be able to append the comments with new comments to the ticket. I print all the ticket information without a problem it's appending it that is the problem. One, because my $_GET array is gone after submit I lose the ticket information, two I can't figure out how to just add new comments to the comments section for the particular ticket in my XML. Below is the code.

<!DOCTYPE html>
<html lang="en">
<head>
<title>
    Kapsiak's Ticket Closer
</title>
<meta charset="utf-8">
<link rel="stylesheet" href="site.css">
</head>
<body>
<?php include 'header.php'; ?>
<?php include 'nav.php'; ?>
<main>
    <div class="row">
        <div class="sidel">
        </div>
 <main>
    <div class="row">
        <div class="sidel">
        </div>
        <div class="main">
        <?php
            // read all the ticket information to screen
            $xml=simplexml_load_file("tickets.xml") or die ("Error: Cannot Display Tickets");
            foreach($xml->children() as $ticket) {
            if ($ticket['id'] == $_GET['id']) {
                    echo "<div class='helpLeft'>";
                    echo "<h3>Requester</h3>";
                    echo "<p>First Name: " . $ticket->fname . "</p>";
                    echo "<p>Last Name: " . $ticket->lname . "</p>";
                    echo "<p>Phone: " . $ticket->phone . "</p>";
                    echo "<p>School: " . $ticket->loc . "</p>";
                    echo "<p>Room: " . $ticket->room . "</p>";
                    echo "</div>";
                    echo "<div class='helpLeft'>";
                    echo "<h3>Device Information</h3>";
                    echo "<p>Device Type: " . $ticket->device . "</p>";
                    echo "<p>Manufacturer: " . $ticket->manu . "</p>";
                    echo "<p>Model: " . $ticket->model . "</p>";                            
                    echo "<p>Serial: " . $ticket->serial . "</p>";
                    echo "<p>Status: " . $ticket->status . "</p>";
                    echo "</div>";
                    echo "<div class='comments'>";
                    echo "<h3>Comments</h3>";
                    // check for new comments
                    if (!empty($_POST)){
                        $ticket->appendChild('comments', $_POST(comments));
                        }
                    echo "<p>" . $ticket->comments . "</p>";
                    echo "</div>";
                }
            }
            echo "<br>";                    
        ?>
        <div class="comments">              
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
    <textarea name="comments" id="comments" cols="60" rows="10" required> </textarea><br>                
    <select size="1" name="status" id="status" required>
    <option value="open">Select New Status </option>
    <option value="In Progess">In Progress </option>
    <option value="Awaiting Reply">Awaiting Reply    </option>
    <option value="Closed">Closed </option>
    </select>
            <input type="submit" value="Update"/>
            </form>
            </div>
        </div>
        <div class="sider">
        </div>
     </div>
 </main>
 <?php include 'footer.php'; ?>
 </body>
 </html>

With the help of Blauharley I was able to figure out the rest of this issue. appendchild() and addchild() are not needed for SimpleXML. I had to just write the new variable to the XML using

$ticket->comments = $oldCom .  " " . $_POST['comments'];

I then just had to save the XML using

file_put_contents("tickets.xml", $xml->asXML());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM