majhate Posted December 30, 2007 Share Posted December 30, 2007 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 Quote Link to comment Share on other sites More sharing options...
chantown Posted December 30, 2007 Share Posted December 30, 2007 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- Quote Link to comment Share on other sites More sharing options...
tapos Posted December 30, 2007 Share Posted December 30, 2007 You can do it using html arrays. -- Thanks Tapos Pal Quote Link to comment Share on other sites More sharing options...
tibberous Posted December 30, 2007 Share Posted December 30, 2007 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 ','. Quote Link to comment Share on other sites More sharing options...
majhate Posted December 30, 2007 Author Share Posted December 30, 2007 I need to know how to make it automatically put all of the varibles in a string. Like a for loop, because the amount of varibles is going to be undefined. The person may what 5, or 50 answers. Any ideas? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 You can do it using html arrays. -- Thanks Tapos Pal Since when does html have arrays? Quote Link to comment Share on other sites More sharing options...
anon Posted December 30, 2007 Share Posted December 30, 2007 You can do it using html arrays. -- Thanks Tapos Pal Since when does html have arrays? Yes. WTF Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 30, 2007 Share Posted December 30, 2007 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. Quote Link to comment Share on other sites More sharing options...
majhate Posted December 31, 2007 Author Share Posted December 31, 2007 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 Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 31, 2007 Share Posted December 31, 2007 [code]don't number the inputs box1 box2, box3 do box[] then on your processor you can say <?php foreach($_POST['box'] as $value){ //Do stuff } ?> [/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.