Page 1 of 1
How do I export parsed VCard data into a MySQL database?
Posted: Wed May 06, 2009 6:33 pm
by WithHisStripes
Heya,
So I am using an existing script to parse a VCard, and all is well when it prints it as an array, but I don't know how I take that information and pull it's data so I can insert it into a database. Can someone give me some pointers on how to accomplish this? Thanks!
http://www.theportlandco.com/nodes/parser.php
Re: How do I export parsed VCard data into a MySQL database?
Posted: Wed May 06, 2009 11:46 pm
by John Cartwright
something like..
Code: Select all
foreach ($vcards as $vcard) {
foreach ($vcard as $attribute => $rows) {
echo $attribute .' - '. $rows[0]['value'][0][0] .'<br>';
}
}
this will iterate the first value of each of the keys, which should give you a starting point to iterating the dataset. Depending on whether you have a normalized database or not would be very different on how you would approach importing this to a database. Fundamentally, you are just building an SQL string (or many) as you progressively iterate the dataset.
Re: How do I export parsed VCard data into a MySQL database?
Posted: Thu May 07, 2009 3:42 pm
by WithHisStripes
You'll have to forgive me, I'm still a bit of an intermediate, so I don't really understand what you said or what the code is doing exactly?
Can you elaborate and dumb it down a bit?

Thanks!
Re: How do I export parsed VCard data into a MySQL database?
Posted: Thu May 07, 2009 11:34 pm
by John Cartwright
Did you try running the code I posted? Basically, all it is doing is looping the first level and second level keys in your array. Given the data you posted, this would output something like:
It's not exactly what you want, since each attribute, i.e. VERSION, N, FN, can have multiple values. Now, once you've figured out how to properly iterate the dataset, you have to decide how you are going to store the data. Again since each attribute can have multiple values, do you want to store each value in it's own row (normalized), or do you want to just combine the text into a single field (non-normalized). Normalization is most certainly the better practice, although it can be a tiny bit more complicated unless you've crasped the concept. If not, I would still recommend you look into it.
Give it a shot, when you get stuck let us know exactly what your having trouble with.