add, create, delete data in XML file

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

captainbarred
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Sep 26, 2012 7:54 pm

add, create, delete data in XML file

Postby captainbarred » Wed Sep 26, 2012 8:05 pm

Hey all, I have an XML file with data listed as

<toollist>
<tool>
<toolid>7348</toolid>
<toolname>Jackhammer</toolname>
<desc>Ryobi Brand - Blue - Electric Jackhammer</desc>
<reserve>None</reserve>
<lastmaint>6/17/2012</lastmaint>
</tool>
<tool>
**CLIP**
</tool>
**ETC**
</toollist>


I have a simple PHP form to create new entries.
I also have all the data displayed in a table.

What i need is a way to edit the data. When a tool gets serviced, for example, I need to update the lastmaint field. Or if it gets reserved I need to update the reserve field.

How is the best way to do this. Or rather, the easiest way. Just needs to use PHP to edit the data.

I'm thinking I need to set the array, then have a form to have the user input an array number to select which gets updated.

Any help would be greatly appreciated! Am I on the right track?

Thanks in advance!

-Tim

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: add, create, delete data in XML file

Postby johnj » Thu Sep 27, 2012 8:57 am


rwhite35
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Sep 27, 2012 10:55 am

Re: add, create, delete data in XML file

Postby rwhite35 » Thu Sep 27, 2012 11:13 am

Personally, I wouldn't use a flat file for data storage if you intent to change the data often. Flat file is great for content management system where the information changes once in a while. A database back end would be faster and less line of code to do the same thing.

That said, here's a function I use to write page content to a text file. XML will have additional nuances, but this will get you in the right direction, unless I'm totally misunderstanding your question.

Code: Select all

public function writer($fpath, $string){
   $date = date('jS F Y');
   $handle = fopen("$fpath", 'a'); //file path to write, a - append on to end
   flock($handle,LOCK_EX); //lock the file so only one user can write to it at one time.
   $input = $date.",".$string."\n"; //concatinate variables in to one string
   if ($input > 0)
   {
       fwrite($handle, $input);
       return true;
       flock($handle, LOCK_UN);// un lock the file
       }
      else die("No lines to write for $fp");
     } //closes function writer


So this opens your file and appends the string of data on to the end of the opened file. You would use this to add items.

Other functions to look at fseek(), fgets(), fgetc(), fclose().
Hope that helps

captainbarred
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Sep 26, 2012 7:54 pm

Re: add, create, delete data in XML file

Postby captainbarred » Thu Sep 27, 2012 4:43 pm

Thanks for the replies fellas!

After running through the wordpress site linked, I have a few more ideas on direction.

This isn't quite right but is it close?
I think I screwed up the section telling what to update to the element.....

<?php

$toolat = $_GET["toolid"];
$tooled = $_GET["tooled"];
$toolup = $_GET["update"];

$xmlDoc = new DOMDocument();
$xmlDoc->loadXml(toollist.xml);

$updateme = $xmlDoc->getElementsByTagName("toolid");

if ( $updateme == $toolat){
$new = $doc->createElement($tooled);
$new = $toolup
$xmlDoc->appendChild($new);
}

$doc->save("toollist.xml");

$url = 'www.sample.com';

header("Location: $url");

?>

Any input would be GREAT!!!!

captainbarred
New php-forum User
New php-forum User
Posts: 3
Joined: Wed Sep 26, 2012 7:54 pm

Re: add, create, delete data in XML file

Postby captainbarred » Sat Sep 29, 2012 12:04 pm

updated version. Made it a loop so it will run through the whole toollist.xml file.

Just returns blank white page which tells me that the for loop is not working correctly as it won't get past there.... Hmmm.....

<?php

$toolid = $_POST["toolid"];
$tooled = $_POST["tooled"];
$toolup = $_POST["update"];

$xmlDoc = new DOMDocument();
$xmlDoc->loadXml(toollist.xml);

$updateme = $xmlDoc->getElementById("toolid");

for ( $updateme == $toolid) {
$xmlDoc->getElementById($tooled);
$tooled->appendChild( $doc->createTextNode( $tool["toolup"] ));
}

$doc->save("toollist.xml");

$url = 'http://www2.esc.edu/tbarrera/tooldbadmin.php';

header("Location: $url");

?>


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests

cron