Jump to content

Count options specified in poll system


ecopetition

Recommended Posts

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.

Link to comment
Share on other sites

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/>

Link to comment
Share on other sites

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');
		}

Link to comment
Share on other sites

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';
}
?>

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.