Solved

Editting specific data in XML using PHP

Posted on 2011-09-29
2
285 Views
Last Modified: 2012-05-12
Hello EE,

I am working on a small project that is making a small shopping website.

I am trying to implement the following code:

// Read the saved session variable
$myCart = $_SESSION["Cart"];

// Load the XML Document
$doc = new DomDocument('1.0');
$doc->load( "data.xml" );
$getItems = $doc->documentElement;

// Iterate through the array passed through containing the items.
foreach( $myCart as $Item )
{
	// Create a new XPath Variable.
	$xpath = new DOMXPath( $doc );

	// Get the correct element needed to be changed.
	$getSold = $xpath->query('/Items/Item[ItemID="' + $Item + '"]/Sold');
	$getHold = $xpath->query('/Items/Item[ItemID="' + $Item + '"]/Hold');

	// Change it to the new values.
	$getSold->item(0)->nodeValue += $getHold;
	$getHold = "0";
}

// Save the XML Document
$doc->save( $filename );
echo $doc->saveXML();

Open in new window


In this code, I am storing a session variable of "Item IDs" that will be copied into a variable and the code will iterate through all the elements in the array.

For each Item ID, I need to increment the corresponding "Sold" value by the "Hold" value and reset the value of "Hold" back to "0" in an XML document that is loaded.

At the end, the XML document needs to be saved.

The XML File looks like the following:

<?xml version="1.0" encoding="utf-8"?>
<Items>
	<Item>
		<ItemID>ABC001</ItemID>
		<Name>Book1</Name>
		<Price>4.99</Price>
		<Qty>5</Qty>
		<Hold>5</Hold>
		<Sold>0</Sold>
	</Item>
	<Item>
		<ItemID>ABC002</ItemID>
		<Name>Book2</Name>
		<Price>14.99</Price>
		<Qty>5</Qty>
		<Hold>5</Hold>
		<Sold>1</Sold>
	</Item>
	<Item>
		<ItemID>ABC003</ItemID>
		<Name>Book3</Name>
		<Price>24.99</Price>
		<Qty>5</Qty>
		<Hold>10</Hold>
		<Sold>2</Sold>
	</Item>
</Items>

Open in new window


Please note I'm quite new to working with XML and XPath and I hope this makes sense and someone can help me.

Thank you very much in advance.
0
Comment
Question by:mr_stevie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
mr_stevie earned 0 total points
ID: 36814108
After hours of trial and error, managed to whip up the following code which seems to work the way I want it:

 
// Load the XML Document
$doc = new DomDocument('1.0');
$doc->load( "data.xml" );
$getItems = $doc->documentElement;

// Iterate through the array passed through containing the items.
foreach( $myCart as $Item => $ItemValue )
{
	// Create a new XPath Variable.
	$xpath = new DOMXPath( $doc );
	
	// Make Query String
	$SoldQuery = "/Items/Item[ItemID=\"" . $Item . "\"]/Sold";
	$HoldQuery = "/Items/Item[ItemID=\"" . $Item . "\"]/Hold";

	// Get the correct element needed to be changed.
	$getSold = $xpath->query( $SoldQuery );
	$getHold = $xpath->query( $HoldQuery );

	// Change it to the new values.
	$getQty->item(0)->nodeValue += $getHold->item(0)->nodeValue;
	$getHold->item(0)->nodeValue = "0";
}

// Save the XML Document
$doc->save( $filename );
echo $doc->saveXML();

Open in new window

0
 
LVL 1

Author Closing Comment

by:mr_stevie
ID: 36814116
Solution was developed by the original poster whilst waiting for an answer.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
bulk create of subfolders in multiple parent folders 9 65
compare date to getdate() 8 29
Powershell PGP and SFTP 1 43
Notepad++ Question 11 21
When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question