Jump to content

PHP Random Number Sequence?


Ryanz

Recommended Posts

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.";
}

?>

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.