Jump to content

[SOLVED] My function doesn't work :(


dink87522

Recommended Posts

The point of this function is to not give the user a question they have had in the previous 20 questions. However it simply does not work (i.e. you do get a question you've had in the most 20) and I can't see why. I am trying to check if $rnd has been in the previous 20 (stored from a cookie and which is retrieving that info successfully) and if so, select a new $rnd number and check it etc... Any help would be much appreciated.

 

<?php $rnd = rand(1, 203);
//recentQuestionsPop($rnd);
$checked = "";
$recentlyAnswered = $_COOKIE["Recently_answered"];
$recentQuestions = explode("|", $recentlyAnswered); 
while ($checked != "1"){ 
$rnd = rand(1, 203);
//$recentQuestions = explode("|", $recentlyAnswered);
    $checked = recentQuestionsPop($rnd); 
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function recentQuestionsPop($rnd){
$checked = "1"; 
switch ($rnd){
case "$recentQuestions[0]"; 
if ($rnd == $recentQuestions[0]){
	$checked = "0";
} 
break;
case "$recentQuestions[0]"; 
if ($rnd == $recentQuestions[0]){
	$checked = "0"; 
} 
break;
case "$recentQuestions[1]"; 
if ($rnd == $recentQuestions[1]){
	$checked = "0"; 
} 
break;
case "$recentQuestions[2]"; 
if ($rnd == $recentQuestions[2]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[3]"; 
if ($rnd == $recentQuestions[3]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[4]"; 
if ($rnd == $recentQuestions[4]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[5]"; 
if ($rnd == $recentQuestions[5]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[6]"; 
if ($rnd == $recentQuestions[6]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[7]"; 
if ($rnd == $recentQuestions[7]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[8]"; 
if ($rnd == $recentQuestions[8]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[9]"; 
if ($rnd == $recentQuestions[9]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[10]"; 
if ($rnd == $recentQuestions[10]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[11]"; 
if ($rnd == $recentQuestions[11]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[12]"; 
if ($rnd == $recentQuestions[12]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[13]"; 
if ($rnd == $recentQuestions[13]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[14]"; 
if ($rnd == $recentQuestions[14]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[15]"; 
if ($rnd == $recentQuestions[15]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[16]"; 
if ($rnd == $recentQuestions[16]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[17]"; 
if ($rnd == $recentQuestions[17]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[18]"; 
if ($rnd == $recentQuestions[18]){
	$checked = "0"; 
} 
break; 
case "$recentQuestions[19]"; 
if ($rnd == $recentQuestions[19]){
	$checked = "0"; 
} 
break; 
}
return $checked;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/169948-solved-my-function-doesnt-work/
Share on other sites

get rid of the recentQuestionsPop() function

 

 

$checked = "";
$recentlyAnswered = $_COOKIE["Recently_answered"];
$recentQuestions = explode("|", $recentlyAnswered); 
while ($checked != "1"){ 
   $rnd = rand(1, 203);
   if (!in_array($rnd,$recentQuestions)) $checked = "1";
}

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.