How do I export parsed VCard data into a MySQL database?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

How do I export parsed VCard data into a MySQL database?

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do I export parsed VCard data into a MySQL database?

Post 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.
WithHisStripes
Forum Contributor
Posts: 131
Joined: Tue Sep 13, 2005 7:48 pm

Re: How do I export parsed VCard data into a MySQL database?

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: How do I export parsed VCard data into a MySQL database?

Post 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:

Code: Select all

VERSION - 3.0
N - DONLEY
etc ..
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.
Post Reply