Back-end for Front-Enders: XML to MySQL – insert and update

Freelancing can take a developer in many directions that they wouldn’t get to go in a specific company role. The three years that I spent in front-end development taught me a lot about jQuery, CSS and conditional PHP but it really even touch on handling data. The terms ‘sanitize’ and ‘data object’ are still new-ish to me, gleaned from the lexicon of colleagues and peers. Nevertheless, it is the job of a freelancer to understand that anything is possible and then find out how to do it.

The problem:
I have an ftp folder with about 10 .xml files that get updated by a 3rd party every day at midnight. i need to a) initially insert them into MySQL and b) cron check daily for new new records and for records that need updated.

In my mind, it is a matter of breaking apart arrays and analysing them… but, I wanted a pro opinion on how it should be done. So I asked my friend Lloyd Leung what he recommended and he laid it out for me:

First, you will have to create a .php page

1.) validate the xml, make sure it’s correct. look into “tidy” in the php manual.
2.) load object if exists, or start a new object.
3.) validate the data object’s data.
4.) save the data object.
5.) done.

– Run the cron on your .xml at 2am, so it gaves them time to upload, and finalize the data.
– If at anypoint, something F’s up… send an email out to report the error as to what happened.

So, of course, this is perfectly clear but I still wanted to double check that I had my wording right. I asked exactly what he meant by ‘data object’ and was told that it’s a class, that would represent a single row in a database. Or ‘collection’ of data that is represented in the database.

For example:
– “human” would be an object.
– Human has “color of eyes”, “hair color”, “height”… and those are all properties of “human”.
– human->setHairColor(‘brown);
– human[‘hairColor’] or human->getHairColor(), would return “brown”

When you “setHairColor();” you can check if it’s a string, in a list of acceptable “colors”, if it’s “capitalized”, whatever business rules that need to happen.
So, now you’re dealing with a single class, that houses logicallly, all functions that are tied to “human”.

Thanks Lloyd! This was helpful for me.