/    Sign up×
Community /Pin to ProfileBookmark

Using RAND() and Pagenation

Hello,

Im creating a script that would do the following.

There are total 250 ($max) questions, i want the script to randomly pick 50 ($amount) different / unique questions. – This part works!

What i want it to do then is to display 1 ($show) question on each page, then the user would click next, for the next question, or they could go back and retry it.

So what i am having problems with:

  • 1. Only showing the 1 question from the array.

  • 2. Only showing the 1 question on the page.
  • Heres my code:

    [code=php]
    <?php

    $max = 250; $min = 1;
    $show = 1; $amount = 50;
    $x = 0; $numbers[0] = 0;

    $start = isset($_POST[‘s’])?$_POST[‘s’]:0;
    $total_rows = isset($_POST[‘t’])?$_POST[‘t’]:”;
    $_SERVER[‘PHP_SELF’] = isset($_SERVER[‘PHP_SELF’])?$_SERVER[‘PHP_SELF’]:”;

    while (count($numbers) < $amount) {
    $ranvar = rand($min, $max);
    while(in_array($ranvar, $numbers)) {
    $ranvar = rand($min, $max);
    }

    $numbers[$x] = $ranvar;
    $x++;
    }

    if(!$total_rows)
    list($total_rows)=$x;

    $startP = $start-$show;
    $startN = $start+$show;

    $num_pages = intval($total_rows/$show);
    if($total_rows%$show)
    {
    $num_pages++;
    }

    echo ‘x: ‘.$x.'<br />’;

    for($numbers as $value) {
    echo ‘Question: ‘.$value.'<br />’;
    }

    //print_r($numbers);

    ?>
    <table width=”100%”>
    <tr>
    <td>
    <form action=”<?=$_SERVER[‘PHP_SELF’];?>” method=”post”>
    <?php
    echo $startP>=0? ‘<input type=”submit” value=”PREV”>’.
    ‘<input type=”hidden” name=”t” value=”‘.$total_rows.'”>’.
    ‘<input type=”hidden” name=”s” value=”‘.$startP.'”>’:'<input type=”submit” value=”PREV” disabled>’;
    ?>
    </form>
    </td>
    <td>
    <form action=”<?=$_SERVER[‘PHP_SELF’];?>” method=”post”>

    <?php
    echo $startN<$total_rows? ‘<input type=”submit” value=”NEXT”>’.
    ‘<input type=”hidden” name=”t” value=”‘.$total_rows.'”>’.
    ‘<input type=”hidden” name=”s” value=”‘.$startN.'”>’:'<input type=”submit” value=”NEXT” disabled>’;
    ?>
    </form>
    </td>
    </tr>
    </table>
    [/code]

    Thanks for any help.

    to post a comment
    PHP

    3 Comments(s)

    Copy linkTweet thisAlerts:
    @CaptainSessaMar 19.2011 — 2 solutions I can think of:

    1.) The not very nice one: Store your questions and already given answers in your $_SESSION object and use as a POST/GET parameter only the array key of the answer (for next it would be $key+1, for previous $key-1).

    2.)The nice one: If you're into some jquery / javascript, you could call via ajax a page that creates your random array and returns it via JSON.. you can then parse it in your JS into an array and dynamically update your page.. this does not require a pageload, which is much more beautiful for the user ?
    Copy linkTweet thisAlerts:
    @lordspaceMar 19.2011 — I was thinking of a similar thing.

    when the user starts the test create an array of question IDs, shuffle the array and then save it in the session.

    then when the user clicks prev/next you'll always know the current position (starting from 0).
    Copy linkTweet thisAlerts:
    @NogDogMar 19.2011 — You could split the difference with AJAX, storing the selected set of questions/answers in a PHP session array, then using JavaScript AJAX calls to submit answers and get the next question. This would mean more HTTP requests/responses than a pure JavaScript solution, but would mean less data dumped to the browser at any one time while allowing you to record each answer on the server side as it is submitted (in case the user's browser crashes or (more likely) the user crashes. ?
    ×

    Success!

    Help @LiL_aaron spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.18,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...