Jump to content

[SOLVED] Arrays? Strings? Loops?


majhate

Recommended Posts

Hello,

 

I am new to the forum and php. I have a question many of you might find easy. I am trying to create a system where users can create their owns polls. I have it so they can type the number of answers they want with a for loop. I figured out the best to put the answers into a database is by using one field instead of many. So I would have a field called answers, and all the answers they wanted would go in there seperated by a space or something. Now my problem is, when then are going to fill in the answers (remember they typed in the number of answers and that exact number of textboxes showed up) I need to put all of their answers in a string, so: $string = $answer1 . "," . $answer2 . "," . $answer3. then this will go into the one answer field into the database. Now their number of answers would always vary so I'm guessing I would need to set it up in some kind of array that counts the number of varibles? I am stuck at this point. Any Ideas? I would greatly appreciate it!

 

Thanks,

Jason

Link to comment
Share on other sites

hi majhate,

 

I would NOT do this:

 

"So I would have a field called answers, and all the answers they wanted would go in there seperated by a space or something."

 

I would instead insert a row for each answer. trust me, it makes things much easier.

To determine number of answers just count the rows in the table

 

good luck-

Link to comment
Share on other sites

Chantown is technically right.

 

That said...

 

Lets say I have a string. The string is like: "test,test2,test3,test4"

 

To get it apart, all you need to do is:

 

$answers = explode(',', $string);

 

And now all the answers are in an array, and all the commas are gone.

 

And to find out how many you have, you can just go count($answers)

 

And to put them back together as a string, it:

 

$string = implode(',', $answers)

 

The biggest problem you are going to have is escaping the separator (IE:The answer is: Earth, Wind and Fire). The best way around this would probably be to just str_replace all ',' with ','.

Link to comment
Share on other sites

Now for a real answer

 

MySQL will be your friend here

need a few tables lets assume a few things

1) You already store your users in a table

2) The table's primary key is UserID

 

Now that that is assumed lets move on

1) Build a table called Polls

PollID CreatorID Question

2) Second table called Poll Answers

AnswerID PollID Answer

3)Third table called Poll Results

PollID AnswerID UserID

 

 

Making a poll is 3 queries

1) Insert the poll with its creator and question

2) Get the mysql_insert_id() from the above query use it in the next query to make the answers

3) Insert all the answers into the Answer table under that PollID

 

Now to view a poll

2 queries

1) Query for a poll with that PollID for its Question

2) Query for all answers where PollID = This Pool

 

To add results

1 query*

1) Insert into `Answers` AnswerID, UserID Values($answer,$_SESSION['UserID']);

You probably want to verify they haven't answered this poll already fisrt

 

View Results

4 Queries

1) Get pool questoin

2) Get pool answer

3) Get the answer results.

 

Link to comment
Share on other sites

You guys are not answering my question! I need to know how to insert multiple varibles under one field in MySQL. Since the number of answers are going to vary, (could be 5 or 50) I need to use some kind of loop that puts them in 1 string (answers = $answer1 . "," . $answer2 . "," . $answer3) If it were a set number of answers I could do it, but its not. I have it so you can type the number of answers you want. Here: http://fileamp.com/test    Now I need to be able to take the value of those text boxes and put it into 1 simple string. Any ideas?

 

Thanks!

Jason

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.