dink87522 Posted August 12, 2009 Share Posted August 12, 2009 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 More sharing options...
Mark Baker Posted August 12, 2009 Share Posted August 12, 2009 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"; } Link to comment https://forums.phpfreaks.com/topic/169948-solved-my-function-doesnt-work/#findComment-896524 Share on other sites More sharing options...
dink87522 Posted August 12, 2009 Author Share Posted August 12, 2009 Thanks, I'll try that. I was not aware of the in_array function. Link to comment https://forums.phpfreaks.com/topic/169948-solved-my-function-doesnt-work/#findComment-896539 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.