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
https://forums.phpfreaks.com/topic/227768-multianswer-poll-counting-votes/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.