text file parsing to mysql

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

hi everyone,

i am new at coding php and need some guidance on how to do the

i have a text file with lots of multiple choice questions, eg

1. what is blah blah blahdsd fds
fdsf dfds dfdsf dsf s

    a. fi fd df dsfsd
    b. fie
        fds fsdf
    c. foe
    d. fum

2. what is blah blah blah

    a. fi
    b. fie
    c. foe
    d. fum

I like to store the question part into a mysql field for question and
each choice in choiceA, choiceB, choiceC, and choiceD fields of the
same table. the question and each choice could span several lines, not
limited to one line.

How can I accomplish this?


Re: text file parsing to mysql

herman gold wrote:
Quoted text here. Click to load it

This is going to be a bit awkward because you don't have good
delimiters.  For instance, it would be much easier of you had the
question on one line, immediately followed by each answer (also on one
line each), then a blank line, i.e.

1. question
   a. answer
   b. answer
   c. answer
   d. answer

2. question
   a. answer
   b. answer
   c. answer
   d. answer

(Of course, other formats would work, also - just an example).

As it is, you could read in a line and check the first characters (after
you trim() white space, of course).  If they are numeric, it is probably
a question.  If it is "a.", "b.", ..., it is probably an answer.

It's a quick way, but will fail with lines such as:

1. "What is the secret to life, the universe and everything?".
   a. According to Douglas Adams, it is

2. Which maid called her employer Mr.

More complicated would be to keep track of whether you are parsing a
question or an answer, and if the latter, which answer.  You could also
look for blank lines to indicate the separator between a question and
it's answers, and between the previous answers and the new question.

BTW - you should read up on database normalization.  Your design is
probably not optimum for several reasons.  But you can follow up more on
that in comp.databases.mysql.

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: text file parsing to mysql

Quoted text here. Click to load it

Well, for starters, I would store the quiz/survey in a format that
will be much easier to parse, like XML:

<question number=3D"1">
  <choice name=3D"a">Why not?</choice>
  <choice name=3D"b">Because</choice>


Then, there are a variety of tools available from the simple to the
complex in PHP for parsing this data.  Another script can just as
easily parse the XML and draw the questionnaire in a standardized
format, such as you listed above.

Beyond that, you're not really asking a specific question.  Are you
more concerned with the parsing, or the database functionality?  I
think using a standardized mark-up language will solve your parsing
problem.  You should heed Jerry's suggestion of looking into a
relational db design--not in this group.  If you have specific
questions about actually using PHP to communicate with the db, then
post them back here.

Re: text file parsing to mysql

Quoted text here. Click to load it
I get the feeling that he doesn't have the choice. It looks to me like
an initial load of a mass of questions that are currently held in text

Assuming that they all have exactly the same format as the lines he
showed us, it should be possible to work out some logic to load them.
I would tend to use PIPELINEs (as in VM/CMS PIPELINEs) for this or
maybe Excel to pre-process them.

If it was to be an ongoing thing then why go via XML at all.

Re: text file parsing to mysql

Quoted text here. Click to load it

Well, that's really where the OP needs to come in and shed some light
on the situation.  We don't know if these are being generated by hand,
read in from a whole pile of pre-existing text files, or being
generated dynamically through some other interface...

Site Timeline