Importing a tag formatted bibliographic data to MySQL

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
simppa77
Forum Newbie
Posts: 2
Joined: Thu Nov 21, 2002 5:09 am

Importing a tag formatted bibliographic data to MySQL

Post by simppa77 »

Hi guys! I recently got help from another forum on this subject,
but now I turn to you to make it complete.

I must import a tag formatted text file containing bibliographic data. I've enclosed a sample of this data at the bottom of this post. There's also the full tag listing, in case someone decides to actually help me with this.

I'm used to importing standard CSV text files using LOAD DATA INFILE, but this is a another thing. I've been told I must write a PHP script to import this data.

Here is what I've been told:


For each line of that input, let's call the first two characters the "2-code" and the rest of the line after the space the "data".

Create a table which can contain every attribute you might be sent, even if a lot of them will be empty.

In PHP, create an array which matches 2-codes to column names of your table. Something like: $translation = array ("IB" => "ISBN", "BI" => "binding", "AU" => "author");

Read each line of the file, and split it at the first space.

If the 2-code that you get this time is the same as the one you got on the last line, add data top the data you got last line.

If the 2-code is different, add the array value of $translation to a $columns string, and the value to a $values string.

For the first three lines of the first record you posted, $columns will read "ISBN, binding, author", and $values will read "'1859060196', 'PAPERBACK', 'SHINE, NORMAN'".

When you get to "**", combine everything into an SQL query, execute it, then clear out $columns and $values.

$query = "INSERT INTO books(" . $columns . ") VALUES(" . $values . ")";


Well... now I only need help on doing just this... I'm a total newbie to coding... I know how to connect to my database, and display the records... but working with arrays, reading one line at a time, splitting the line at the first space... these are all new to me.

Here's the sample data

Code: Select all

** 
IB 1859060196 
BI PAPERBACK 
AU SHINE, NORMAN 
BC VXFN 
CO UK 
PD 19990923 
NP 128 
RP 9.99 
RI 9.99 
RE 9.99 
PU CONNECTIONS BOOK PUBLISHING 
YP 1999 
TI NUMEROLOGY 
TI YOUR CHARACTER AND FUTURE REVEALED IN NUMBERS 
EA 9781859060193 
RF R 
SG 2 
GC M01 
DE A unique step-by-step visual approach to numerology, how to assess 
DE characters and compatibility from names and birth dates. 
** 
IB 1898295395 
BI SPIRAL BOUND 
AU PRICE, ROBIN 
BC ELX 
CO UK 
PD 19961231 
RP 15.75 
RI 15.75 
RE 15.75 
PU ENGLISH EXPERIENCE 
YP 1996 
SR BRAIN FRIENDLY RESOURCES 
TI CONVERSATION 2: STUDENT TALK 
EA 9781898295396 
RF R 
GC C01 


Here are all the possible fields 

** START OF RECORD 
IB ISBN 
AV UK AVAILABILITY 
BI BINDING 
AU AUTHOR 
BC BIC CLASSIIFCATION 
CO COUNTRY OF ORIGIN 
ED EDITOR 
IL ILLUSTRATOR 
EI EDITION 
IU ILLUSTRATIONS 
CP CO-PUBLISHER 
LA LANGUAGE OF BOOK 
MP MIXED PACK COMPONENTS 
NC NATIONAL CURRICULUM KEY STAGE 
PD PUBLICATION DATE 
PA PAGINATION 
NP NUMBER OF PAGES 
RP UK RRP SELLING PRICE 
RI UK PRICE INCL VAT 
RE UK PRICE EXCLUDING VAT 
DI PAGE SIZE/DIMENSIONS 
PU PUBLISHER NAME 
YP YEAR OF PUBLICATION 
RC BOOKDATA READERSHIP CODE 
RS BOOKDATA READERSHIP TEXT 
SR SERIES 
SE SERIES EDITOR 
TI TITLE 
ST SUB-TITLE 
PT PART-TITLE 
TR TRANSLATOR 
PN PART NUMBER 
DE SHORT DESCRIPTION 
EA EAN NUMBER 
RF RETURNS FLAG 
RD RETURNS DATE 
SI NEW ISBN 
WE WEIGHT (Grams) 
SG STAR RATING 
PI GARDNERS PUBLICITY DESCRIPTION 
GC GARDNERS CLASSIFICATION CODE
Please help,

Simo Savonen
sipesa@utu.fi
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

It sounds like sink or swim time :!:

What you need to learn how to do (besides program in general) is parse files for bits of data,
create and process arrays, and dynamically create (in this case, query) strings. Once you have those things down, this should be a piece of (tedious) cake.

But if you don't know the basics, how can you do the above. I'm planning some building on a hunk of land down here and the most important part too me at this time is the foundation. And until it's done, you won't see anything resembling a wall or roof. Get it?

Now, since the maxim here seems to be that nobody writes code for people (and I agree with this), I'll suggest. Below is some psuedo-code. Something like code to help you see the flow of the program.

Code: Select all

# Get data somehow
/* 
insert code here
*/

# Create array or arrays to help filter data
/*
insert code here
*/

# Parse data and build query string
/*
insert code here
*/

# Connect to db and insert data
/*
insert code here
*/
Now there are probably tutorials for each of the commented ("#") lines above. Go one piece at a time and I'm sure you'll get it figure out. Google is your freind.

This is how you learn.

Cheers,
BDKR
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

As an addendum to the dark cloud of a post before, check out this link.

http://www.google.com/search?hl=en&lr=& ... gle+Search

Cheers,
BDKR
simppa77
Forum Newbie
Posts: 2
Joined: Thu Nov 21, 2002 5:09 am

You have a point

Post by simppa77 »

You're right... I was out of line asking people to do the work for me.

In fact, the only reason I haven't found any help on the 'net is the
fact that I was not familiar with the term "parsing". 8O

Thanks for the link, though.

Of the four phaces you described, I've managed to complete all but
the actual parsing, which IS quite tricky... and yes, tedious.

Simo
Post Reply