I am attempting to make an addressbook using a php form/validation which then generates an xml file. I have every part of it working, validation/confirmation/xml generation but I am running into some problems with data not saving (it is erasing the previous xml instead of appending it). I am really new at this so I will apologize if my code is messy. Once I get this solved I am also hoping to use xsl to transform the xml into a nice table but I have problems with that not reading as well... Here is my code around the forms:

Is there a way to get this to not create new xmls and just add to the existing one? Once I get some input on that I'll go into my xsl problem but I think part of it has to do with a new xml being created likely wipes out my link to the style sheet....

12-06-2012, 06:10 AM

taishar77

I take it this is something that cannot be done?

12-06-2012, 06:31 AM

traq

Quote:

Originally Posted by taishar77

it is erasing the previous xml instead of appending it...

Well, your code is doing that explicitly. You're not loading and adding to the existing xml; you're creating a new document and overwriting the existing one.

PHP Code:

<?php

$doc = new DOMDocument();$doc->load( "/path/to/your/existing.xml" );

// continue (but don't recreate the root element)// you can use DOMXpath [http://php.net/domxpath] to find the element(s) you want to append your new elements to

If you don't absolutely need to use xml for this, I'd recommend taking a different direction...

for example, if the addresses you're collecting ever need to be searched (e.g., to find a particular person's address, or addresses in a given city), you'd be much better off using a database.

If you're going to send it to another application (e.g., to a webpage for use by javascript), you might consider using JSON - it's much simpler, lighter weight, equally portable, less error-prone, and it's trivial for most languages to convert it to native data types.

XML is complex, slow to process, and memory-intensive (you have to load the entire document before you can do anything).

12-06-2012, 06:43 AM

taishar77

Thank you traq, I figured it was because I am writing a new one with DOM, I'm just really new so trying to find the variables I need to swap out is where it becomes sort of a head banging against the wall experience :)

I have the database elements created with MySQL but I am even less experienced doing that then I am generating the xml file, which I've managed to do but it obviously has been a huge headache for someone who is very new and learning on the fly. I will research how to write it to a MySQL database and generate a table for it - is there a resource you can point me to for that specifically? I've gotten sort of rummy working on this four straight days into hours in the morning I'd care to forget.

I managed to get a table setup on my server for MySQL but that is something totally foreign to me on how to access/write to it.

12-06-2012, 03:07 PM

traq

MySQL's dev website is a great resource, but it's the most confusing thing in the world to try to read.

For this project, you probably wouldn't need more than SELECT, INSERT, and maybe UPDATE for your queries. The big part is designing your tables and indexes. Can you describe each piece of data that you're collecting, and how they relate to each other? e.g., which pieces are optional, which are sub-parts of the same info, which info might have more than one entry for the same person, which info must be unique? Which pieces of data will you be querying (searching) by?

sequence and it worked wonderfully. You were exactly right in that creating a database and connecting to it to create the output was much simpler than messing around with creating xml via DOMDocument functions. I later on managed to find an example of the DOM function I needed but I was already done (usually works that way doesn't it?). Thanks for your advice!

Quote:

Originally Posted by traq

MySQL's dev website is a great resource, but it's the most confusing thing in the world to try to read.

For this project, you probably wouldn't need more than SELECT, INSERT, and maybe UPDATE for your queries. The big part is designing your tables and indexes. Can you describe each piece of data that you're collecting, and how they relate to each other? e.g., which pieces are optional, which are sub-parts of the same info, which info might have more than one entry for the same person, which info must be unique? Which pieces of data will you be querying (searching) by?

// if you need to validate the user input (e.g., make a field required),// you might do it like this:if( empty( $_POST['lastname'] ) ){ $errors[] = "Lastname is required!"; // set error message}else{$lastname = $DB->real_escape_string( $_POST['lastname'] ); // sanitize input}