Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.96 KB | None | 0 0
  1. function updateInfo() { // Save new input info to XML file
  2.     $catTitle = $_POST['catTitle']; // Category title
  3.     $cid = $_POST['catNr']; // Category ID
  4.     $pid = $_POST['prodid']; // Array with product IDs
  5.     $pName = $_POST['prodname']; // Array with product Names
  6.     $pPrice = $_POST['prodprice']; // Array with product Prices
  7.     $deleteProd = $_POST['deleteProduct']; // Array with product IDs for product removal
  8.     $deleteCat = $_POST['deleteCategory']; // If checked - delete category
  9.    
  10.   /** Manage new info and add it to the XML file **/
  11.     global $xmldoc;
  12.     $category = $xmldoc->getElementsByTagName("products")->item($cid-1); // Get the current category
  13.    
  14.     if(isset($deleteCat)) { // If user chose to delete the category, delete it
  15.         $category->parentNode->removeChild($category);
  16.         return;
  17.     }
  18.    
  19.     // Create new elements for the new content
  20.     $newCat = $xmldoc->createElement("products");
  21.     $newTitle = $xmldoc->createElement("title", $catTitle);
  22.         $newCat->appendChild($newTitle);
  23.     print_r($deleteProd);
  24.     $delCount = count($deleteProd) - 1; // Felar inte!
  25.     echo "<br />".count($deleteProd)."<br />".$delCount;
  26.     $k=0; // Increases whenever a product is found and "deleted"
  27.     for($i=0;$i<count($pid);$i++) { // Add all the new product data to the new products element
  28.         if(($deleteProd[$k] == $pid[$i]) || $deleteProd[$delCount] == false) $k++; // If the current product was chosen to be deleted, skip re-creating it
  29.         else {
  30.             $newProd = $xmldoc->createElement("product");
  31.                 $newCat->appendChild($newProd);
  32.             $newElem = $xmldoc->createElement("id",$pid[$i]);
  33.                 $newProd->appendChild($newElem);
  34.             $newElem = $xmldoc->createElement("name",$pName[$i]);
  35.                 $newProd->appendChild($newElem);
  36.             $newElem = $xmldoc->createElement("price",$pPrice[$i]);
  37.                 $newProd->appendChild($newElem);
  38.         }
  39.     }
  40.     $category->parentNode->replaceChild($newCat,$category); // Replace the old category info with the new info
  41.     $xmldoc->save('prod.xml'); // Save the file with the new content
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement