Ryanz Posted February 6, 2008 Share Posted February 6, 2008 Hey I'm kind of new to PHP and I'm working on my new website. I need to pick a random question page (don't ask why) over and over again. I have about 100 question pages. Once the question has been completed there is a link which should lead onto the next question page, but I want all the question pages to appear randomly, but only once each. So basically once the page has been visited through a random link, it can't be visited again. I have developed a kinda of system for this where I just need a random number from 1-100 to be displayed, although once one number has been displayed it does not show again so that the user can go through each page individually. For example the user starts on page one, then is sent to page 8, then page 64 ect. but once it has visited a page it must not go back there, so it cannot visit page 8 again until the user visits the home page and starts again. So basically it would be cool if somone could show me a way to pick a random number from a sequence, then delete it from the sequence. The number must be displayed as an integer. I thought of using arrays but I couldn't find the correct functions to use. Any help would be much appreciated! Cheers Ryan Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/ Share on other sites More sharing options...
only one Posted February 6, 2008 Share Posted February 6, 2008 I would do it using sessions. Something like. <?php session_start(); $num = rand(1,100); $questionsAskedArray = explode("|", $_SESSION['questionsAsked'], -1); $question = array(); //array of all your questions.. if( !in_array($questionsAskedArray) ) { echo $question[$num]; $_SESSION['questionsAsked'] .= "|". $num; } ?> You might want to use a loop in it to keep trying until it actually works. Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/#findComment-460215 Share on other sites More sharing options...
Stooney Posted February 6, 2008 Share Posted February 6, 2008 You could make an array with 1-100 (one number per element), then shuffle($array). As you view a 'question page', change the value of that array element to 'used'. Then each time you go to show a new page, it'll grab the next element of the array as long as it's value is not 'used'. Just a thought. It would probably be best to keep the array in a session, and visiting the home page, will reset the session variable. Hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/#findComment-460219 Share on other sites More sharing options...
mem0ri Posted February 6, 2008 Share Posted February 6, 2008 I would keep your 100 pages in a session-variable array... $arr = array('pagename', 'pagename', pagename'....)...giving you key-values 0-99 for all 100 pages... ...then...when someone initiates a new page-load to one of these random pages... $y = count($arr) -1; $val = rand(0, $y); //load $arr[$val]; unset($arr[$val]); That should work. Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/#findComment-460220 Share on other sites More sharing options...
Psycho Posted February 6, 2008 Share Posted February 6, 2008 Here you go. change the first variable to however many pages you want. I set it at 10 for testing purposes: <?php $page_count = 10; $page = $_GET['page']; if ($page!='Next') { $pages = range(1, $page_count); shuffle($pages); setcookie ('pages', serialize($pages)); echo "You are on the home page. there are $page_count random pages to visit.<br><br>"; echo " Click <a href=\"$_SERVER[php_SELF]?page=Next\">here</a> to start."; } else { $pages = unserialize($_COOKIE['pages']); $this_page = array_shift ($pages); $last_page = (count($pages)>0)?false:true; setcookie ('pages', serialize($pages)); echo "You are on page $this_page.<br><br>"; echo "Pages <span style=\"color:red;\">used</span>/remaining:<br>"; for ($i=1; $i<=$page_count ; $i++) { if (in_array($i, $pages)) { echo "<b>$i</b>, "; } else { echo "<span style=\"color:red;\">$i</span>, "; } } if ($last_page) { echo "<br><br>This is the last page."; } else { echo "<br><br>Go to <a href=\"$_SERVER[php_SELF]?page=Next\">next</a> page."; } echo "<br><br>Go <a href=\"$_SERVER[php_SELF]\">home</a> and start over."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/#findComment-460236 Share on other sites More sharing options...
Ryanz Posted February 8, 2008 Author Share Posted February 8, 2008 Thanks guys! The problem is pretty much solved! Amazing help thre, I hope to become a part of this forum! Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/#findComment-461690 Share on other sites More sharing options...
Psycho Posted February 8, 2008 Share Posted February 8, 2008 Please click the link to mark the topic solved. Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/#findComment-461752 Share on other sites More sharing options...
GameYin Posted February 8, 2008 Share Posted February 8, 2008 Just wondering, could I use this code if...for a "random game of the hour" or whatever? How would I achieve that. Would I just set an interval and randomize the game output? Quote Link to comment https://forums.phpfreaks.com/topic/89808-php-random-number-sequence/#findComment-461758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.