makeshift_theory Posted January 25, 2007 Share Posted January 25, 2007 Now I would post my wish list app on here but I'm using it :P. This is my second little XML gizmo so I hope it runs okay =).Just upload this file to your server and create a "xml" directory in the same location. Then just change the define tags to what you want.So anywho here is the code, I am going to explain it just in case:Adding Process:> Takes information from form and assigns to array.> Validates information, if empty throw an exception.> Creates a temp file to place new data in.> Attempts to open XML file and recreate it, appending our latest addition.> Write xml to temp file.> Unlink original xml file.> Copy temp to original xml file name.Show Process:> Attempt to open xml file.> Process file and display.[code]<?//////////////////////////// XML Wishlist // By: Techno|ust // Date: 1/25/07// Copyright: GNU// OpenSource <3 ///////////////////////// try { define("EOL","\n\n"); // Define our breaks define("BOOKXML","xml/booklist.xml"); // Define our page define("TEMP","xmltemp.xml"); // Define our temp file function validate($title,$price,$link) { // Validates entry if(empty($title) || empty($price) || empty($link)) return false; else return true; } if(isset($_POST['submit'])) { // Handle a new Entry $list = array( "title" => $_POST['title'], "price" => $_POST['price'], "link" => "<a href=".$_POST['link']." target=_blank>Click here for Link</a>", ); if(validate($list['title'],$list['price'],$list['link']) == false) throw new Exception("One or more entries is invalid, please try again."); if(!$temp = fopen(TEMP, "a+")) throw new Exception("Error opening(creating) xml document"); if(!chmod(TEMP."", 0777)) throw new Exception("Could not chmod xml/".BOOKXML." successfully, please try again."); $xdoc = simplexml_load_file(BOOKXML); // Load our XML FILE $xml = "<?xml version=\"1.0\" ?><root>"; foreach($xdoc->book as $book) // Get all of our existing structure $xml .= "<book><title>".$book->title."</title><price>".$book->price."</price><link>".htmlentities($book->link)."</link></book>"; $xml .= "<book><title>".$list['title']."</title><price>".$list['price']."</price><link>".htmlentities($list['link'])."</link></book></root>"; if(file_exists(BOOKXML)) if(!unlink(BOOKXML)) // If file exists let's wipe it and recreate throw new Exception("Could not remove xml/".BOOKXML." please remove the file manually or try again."); if(!fwrite($temp,$xml)) throw new Exception("Error Writing to temp file."); if(!fclose($temp)) throw new Exception("Cannot close file"); if(!copy(TEMP, BOOKXML)) throw new Exception("Could not rename temp file."); else echo nl2br("Added book titled ".$list['title']." to the wishlist".EOL); } elseif(isset($_POST['show'])) { // Show Existing Entries if(file_exists(BOOKXML)) { $xml = simplexml_load_file(BOOKXML); echo nl2br("<u><b>My Current Wishlist:</b></u>".EOL); foreach($xml->book as $book) { echo nl2br("<B>TITLE:</B>".EOL.$book->title.EOL); echo nl2br("<B>PRICE:</B>".EOL.$book->price.EOL); echo nl2br(html_entity_decode("<B>LINK:</B>".EOL.$book->link.EOL)); } echo nl2br("<hr><hr>".EOL); } else throw new Exception("Booklist.xml does not exist, you must create a list before you can view"); } } catch(Exception $e) { echo nl2br("<font color=\"#FF0000\"><b>".$e->getMessage()."</b></font>".EOL); }?><form action="<?=$_SERVER['PHP_SELF']?>" method="POST">Name of Book:<input style="width:200px;" type="text" name="title" id="title"><br><br>Price:<input style="width:200px;" type="text" name="price" id="price"><br><br>Link to View:<input style="width:200px;" type="text" name="link" id="link"><br><br><input type="submit" name="submit" id="submit" value="Add Book"> <input type="submit" name="show" id="show" value="Show Booklist"></form>[/code] Link to comment https://forums.phpfreaks.com/topic/35662-xml-wishlist/ Share on other sites More sharing options...
makeshift_theory Posted January 25, 2007 Author Share Posted January 25, 2007 I have created [url=http://www.startrekrpg.com/booklistdemo/book.php]www.startrekrpg.com/booklistdemo/book.php[/url] So you guys can check it out without installing.PS> Okay, environment ready for testing. Link to comment https://forums.phpfreaks.com/topic/35662-xml-wishlist/#findComment-168957 Share on other sites More sharing options...
sanguinious Posted February 15, 2007 Share Posted February 15, 2007 a few notes:you can add a price which is not numericyou can leave the link blank but it still shows a link on the listif you dont put in http:// then it tries to open it locallyi added as book title: <a href="what">link</a> - it said it had added to the list, but when trying to view it, errors Link to comment https://forums.phpfreaks.com/topic/35662-xml-wishlist/#findComment-185324 Share on other sites More sharing options...
Recommended Posts