ecopetition Posted November 20, 2008 Share Posted November 20, 2008 Hello there, I'm making a polling system and have written the following code so far: PHP: if(isset($sumbit)) { $poll_title = (isset($_POST['poll_title'])) ? htmlspecialchars($_POST['poll_title']) : ''; $poll_options_post = (isset($_POST['poll_options'])) ? $_POST['poll_options'] : array(); $poll_options = array(); $num_poll_choices = 0; $poll_questions = array(); $num_poll_questions = 0; $poll_questions_post = (isset($_POST['poll_questions'])) ? $_POST['poll_questions'] : array(); for($i = 0; $i < count($poll_questions_post); $i++) { $poll_questions[] = htmlspecialchars($poll_questions_post[$i]); if(! empty($poll_questions[$i])) { $num_poll_questions++; } for($j = 0; $j < count($poll_options_post); $j++) { if(! empty($poll_options_post[$j])) { $num_poll_choices++; } } if(! empty($poll_title) && $num_poll_choices < 2) { trigger_error('num_poll_choices'); } } } HTML: <form> tags etc here {QUESTION} 1: <input type="text" name="poll_questions[]"/><br/> Option 1: <input type="text" name="poll_options[]"/><br/> Option 2: <input type="text" name="poll_options[]"/><br/> Option 3: <input type="text" name="poll_options[]"/><br/> Option 4: <input type="text" name="poll_options[]"/><br/> Option 5: <input type="text" name="poll_options[]"/><br/> Option 6: <input type="text" name="poll_options[]"/><br/> Option 7: <input type="text" name="poll_options[]"/><br/> Option 8: <input type="text" name="poll_options[]"/><br/> Option 9: <input type="text" name="poll_options[]"/><br/> Option 10: <input type="text" name="poll_options[]"/><br/> <br/> {QUESTION} 2: <input type="text" name="poll_questions[]"/><br/> Option 1: <input type="text" name="poll_options[]"/><br/> Option 2: <input type="text" name="poll_options[]"/><br/> Option 3: <input type="text" name="poll_options[]"/><br/> Option 4: <input type="text" name="poll_options[]"/><br/> Option 5: <input type="text" name="poll_options[]"/><br/> Option 6: <input type="text" name="poll_options[]"/><br/> Option 7: <input type="text" name="poll_options[]"/><br/> Option 8: <input type="text" name="poll_options[]"/><br/> Option 9: <input type="text" name="poll_options[]"/><br/> Option 10: <input type="text" name="poll_options[]"/><br/> <br/> {QUESTION} 3: <input type="text" name="poll_questions[]"/><br/> Option 1: <input type="text" name="poll_options[]"/><br/> Option 2: <input type="text" name="poll_options[]"/><br/> Option 3: <input type="text" name="poll_options[]"/><br/> Option 4: <input type="text" name="poll_options[]"/><br/> Option 5: <input type="text" name="poll_options[]"/><br/> Option 6: <input type="text" name="poll_options[]"/><br/> Option 7: <input type="text" name="poll_options[]"/><br/> Option 8: <input type="text" name="poll_options[]"/><br/> Option 9: <input type="text" name="poll_options[]"/><br/> Option 10: <input type="text" name="poll_options[]"/><br/> Basically, when the user presses submit, it runs through the above process and either submits the poll to the database (the database functions aren't shown), or returns an error. If the poll poster wishes to post a question (note that if they want they can post only one question but have a maximum of 3 available, and each question can have a maximum of 10 options) but only specifies say, one answer instead of the minimum 2, then it returns an error ('num_poll_choices'). However, the bit that's confusing me is that if the user specifies one option in the first question and one answer in the second question the PHP thinks "two options have been specified so I can continue". How do I make it know where the options were specified and which questions have only been given one option, which ones have two options, and which one has three options (in which case, continue). I hope this isn't confusing, but if you can help I'd be really grateful. Thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/ Share on other sites More sharing options...
sasa Posted November 20, 2008 Share Posted November 20, 2008 change form to <form> tags etc here {QUESTION} 1: <input type="text" name="poll_questions[1]"/><br/> Option 1: <input type="text" name="poll_options[1][]"/><br/> Option 2: <input type="text" name="poll_options[1][]"/><br/> Option 3: <input type="text" name="poll_options[1][]"/><br/> Option 4: <input type="text" name="poll_options[1][]"/><br/> Option 5: <input type="text" name="poll_options[1][]"/><br/> Option 6: <input type="text" name="poll_options[1][]"/><br/> Option 7: <input type="text" name="poll_options[1][]"/><br/> Option 8: <input type="text" name="poll_options[1][]"/><br/> Option 9: <input type="text" name="poll_options[1][]"/><br/> Option 10: <input type="text" name="poll_options[1][]"/><br/> <br/> {QUESTION} 2: <input type="text" name="poll_questions[2]"/><br/> Option 1: <input type="text" name="poll_options[2][]"/><br/> Option 2: <input type="text" name="poll_options[2][]"/><br/> Option 3: <input type="text" name="poll_options[2][]"/><br/> Option 4: <input type="text" name="poll_options[2][]"/><br/> Option 5: <input type="text" name="poll_options[2][]"/><br/> Option 6: <input type="text" name="poll_options[2][]"/><br/> Option 7: <input type="text" name="poll_options[2][]"/><br/> Option 8: <input type="text" name="poll_options[2][]"/><br/> Option 9: <input type="text" name="poll_options[2][]"/><br/> Option 10: <input type="text" name="poll_options[2][]"/><br/> <br/> {QUESTION} 3: <input type="text" name="poll_questions[3]"/><br/> Option 1: <input type="text" name="poll_options[3][]"/><br/> Option 2: <input type="text" name="poll_options[3][]"/><br/> Option 3: <input type="text" name="poll_options[3][]"/><br/> Option 4: <input type="text" name="poll_options[3][]"/><br/> Option 5: <input type="text" name="poll_options[3][]"/><br/> Option 6: <input type="text" name="poll_options[3][]"/><br/> Option 7: <input type="text" name="poll_options[3][]"/><br/> Option 8: <input type="text" name="poll_options[3][]"/><br/> Option 9: <input type="text" name="poll_options[3][]"/><br/> Option 10: <input type="text" name="poll_options[3][]"/><br/> Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/#findComment-694488 Share on other sites More sharing options...
ecopetition Posted November 20, 2008 Author Share Posted November 20, 2008 Thanks for your input, but it still isn't working. The poll is still being added to the database and not triggering an error. Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/#findComment-694506 Share on other sites More sharing options...
sasa Posted November 20, 2008 Share Posted November 20, 2008 where you add to db Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/#findComment-694509 Share on other sites More sharing options...
ecopetition Posted November 20, 2008 Author Share Posted November 20, 2008 The following code adds each bit to a different db table, it's mostly self-explanatory: if(! empty($poll_title)) { for($i = 0; $i < count($poll_questions); $i++) { if(empty($poll_questions[$i])) { continue; } $sql = "INSERT INTO poll_questions (question_text) VALUES('". $db->escape($poll_questions[$i]) ."')"; $db->query($sql); } $poll_id = $db->last_id(); for($i = 0; $i < count($poll_options); $i++) { if(empty($poll_options[$i])) { continue; } $sql = "INSERT INTO poll_options (question_id, option_text) VALUES('". $poll_questions[$i] ."', '". $poll_options[$i] ."')"; $db->query($sql); } $sql = "INSERT INTO polls (poll_title, topic_id) VALUES('$poll_title', $topic_id)"; $db->query($sql); $sql = "UPDATE topics SET has_poll = 1 WHERE topic_id = $topic_id"; $db->query($sql); define('META_REFRESH', "viewtopic.php?t=$topic_id&f=$forum_id"); trigger_error('poll_added'); } Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/#findComment-694521 Share on other sites More sharing options...
sasa Posted November 20, 2008 Share Posted November 20, 2008 try <?php if(! empty($poll_title)) { $test = false; foreach($_POST['poll_questions'] as $key => $questions) { $poll_options = $_POST['poll_options'][$key]; foreach ($poll_options as $k => $v) if (empty($v)) unset($poll_options[$k]); if(empty($questions) or count($poll_options) < 3) { continue; } $test = true; $sql = "INSERT INTO poll_questions (question_text) VALUES('". $db->escape($questions) ."')"; $db->query($sql); $poll_id = $db->last_id(); foreach ($poll_options as $option){ $sql = "INSERT INTO poll_options (question_id, option_text) VALUES('". $poll_id ."', '". $option ."')"; $db->query($sql); } } if ($test){ $sql = "INSERT INTO polls (poll_title, topic_id) VALUES('$poll_title', $topic_id)"; $db->query($sql); $sql = "UPDATE topics SET has_poll = 1 WHERE topic_id = $topic_id"; $db->query($sql); define('META_REFRESH', "viewtopic.php?t=$topic_id&f=$forum_id"); trigger_error('poll_added'); } else echo 'poll not added'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/#findComment-694558 Share on other sites More sharing options...
ecopetition Posted November 20, 2008 Author Share Posted November 20, 2008 Sorry, it's still not working Personally I'm sure it has something to do with the first chunk of code. Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/#findComment-694575 Share on other sites More sharing options...
ecopetition Posted November 20, 2008 Author Share Posted November 20, 2008 Can anyone else spare their expertise to assist me please Quote Link to comment https://forums.phpfreaks.com/topic/133515-count-options-specified-in-poll-system/#findComment-694637 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.