Jump to content

multianswer poll, counting Votes


mickyvelly

Recommended Posts

Hey all,

 

i have a problem;

 

i have 2 arrays:

$haystack //contains all posible answers in the poll, it contains dates for possible meetings

Array

(

    [0] => 12-02-2011

    [1] => 13-02-2011

    [2] => 14-02-2011

    [3] => 15-02-2011

)

 

$needles //contains the answers the user clicked

Array

(

    [0] => 12-02-2011

    [1] => 13-02-2011

)

 

i have about 10 entries simulated and i want to count the corresponding dates into another array so:

 

Array

(

    [0] => 5

    [1] => 3

    [2] => 1

    [3] => 1

 

)

 

keep in mind that the users can give multiple answers

 

 

Link to comment
Share on other sites

solved it!!!

 

function countVotes($haystack, $countAnswers, $needles, $countFunction)
{
//$haystack is the array where we store all the posible answers (array keys must begin from 0)
//$countAnswers is the variable were we store the total amount of votes
//$needles is the array with only 1 vote (but it can contain multiple answers (beware that the answers must correspond with the $haystack))
//$countFunction is the amount of times the function has been called, so the if statement knows when to unset the session variable

$p = 0; // count for while loop
while($p < count($needles))
{//loop while there are new needles
	if(is_array($needles))
	{//if the needle is an array
		if(in_array($needles[$p], $haystack))
		{//check if the needle is in the array
			$_SESSION['count'][$needles[$p]]++;
		}
	}
	else
	{//if the needle is not an array
		if(in_array($needles, $haystack))
		{//check if the needle is in the array
			$_SESSION['count'][$needles]++;
		}
	}
	$p++;
}
if($countFunction == ($countAnswers-1))
{//if there is no new needle anymore, copy the session data to a safe variable which we will return and delete the session data
	$count = $_SESSION['count'];
	unset($_SESSION['count']);
}
return $count;//return the array with the counted votes
}function countVotes($haystack, $countAnswers, $needles, $countFunction)
{
$p = 0; // count for while loop
while($p < count($needles))
{//loop while there are new needles
	if(is_array($needles))
	{//if the needle is an array
		if(in_array($needles[$p], $haystack))
		{//check if the needle is in the array
			$_SESSION['count'][$needles[$p]]++;
		}
	}
	else
	{//if the needle is not an array
		if(in_array($needles, $haystack))
		{//check if the needle is in the array
			$_SESSION['count'][$needles]++;
		}
	}
	$p++;
}
if($countFunction == ($countAnswers-1))
{//if there is no new needle anymore, copy the session data to a safe variable which we will return and delete the session data
	$count = $_SESSION['count'];
	unset($_SESSION['count']);
}
return $count;//return the array with the counted votes
}

 

sorry guys for solving the problem myself... ;)

 

thanks anyways

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.