0

I can't provide the HTML due to my site not being live. Though I know from theory coding a professional will hopefully be able to follow my logic.

  • Please point out with my code how I can get this working, as I see the following link is similar but not exactly the same scenario. I'm confused how to apply it to my following code:

Generating random numbers without repeats

My Goal I want to use PHP to create the following:

  1. Place 00 to 99 into an array.
  2. Retrieve a random number from the array.
  3. Store/Use the retrieved random from the array to place in a mysql_query as seen with number = $question Note here (Do I really require ORDER BY RAND() here?)
  4. Remove the selected random number from the array list of numbers
  5. Fetch and display the random number
  6. Once all numbers have been used then an error displays saying, refresh page to reset numbers.

The next time I want another random number it cannot duplicate the same random number it selected from the array before. So if I used the code above say 109 times then only 1 number is left in the array.

Code (Edited):

   <?php

  //--------------------------------------------------------------------------
  // Example php script for fetching data from mysql database
  //--------------------------------------------------------------------------
  $host = "localhost";
  $user = "root";
  $pass = "";

  $databaseName = "monkeyscanfly";
  $tableName = "num_image";

  //--------------------------------------------------------------------------
  // 1) Connect to mysql database
  //--------------------------------------------------------------------------
  include 'DB.php';
  $con = mysql_connect($host,$user,$pass);
  $dbs = mysql_select_db($databaseName, $con);

    if(!isset($_SESSION)) {
        session_start();

        $_SESSSION['used'] = [];
    }

    $array = [0,1,2,3,4,5,6,7,8,9,"00","01","02","03","04","05","06","07","08","09",10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99]; 

    while(!empty($array)) {
        $array = array_diff($array, $_SESSSION['used']);

        //pick a random point from the array
        $random = array_rand($array);

        // Save the used element in session
        $_SESSION['used'][] = $array;


        //store the random question number
        $question = $array[$random];

        // Select information from database and use array to assign to the selected row.

        $query = mysql_query("SELECT number, association, image_file, skeleton, sound, colour, comments FROM num_image WHERE number = $question ORDER BY RAND() LIMIT 1");

        // Remove random number that was chosen from the array, so the next time the random number is ran, it won't be found in the array.

        unset($array[$random]);

        //fetch result to print on page

        $arrayss = mysql_fetch_row($query);

        //Echo result as json
        echo json_encode($arrayss);
    }

    if(count($array) == count($_SESSION['used'])) {
        $_SESSION['used'] = [];
    }
?>

I hope this makes sense, I'm having a difficult time find out how to do it, I have searched for hours and can't get my head around it. :)

I forgot to mention that this PHP script will be reloaded by ajax code every time I need a new random number. So it has to store/remember the number with that in mind. If that makes sense?

Community
  • 1
  • 1
aussiedan
  • 341
  • 1
  • 10
  • 3
    You should not be using mysql_* functions anymore, they've been deprecated for a long time in PHP 5.x and COMPLETELY REMOVED in PHP 7.x. If you want your code to be future-proof you MUST use a different SQL API such as mysqli or PDO – GordonM Apr 25 '17 at 08:14
  • @GordonM I know, but unfortunately this is what I have to work with currently. When I redevelop the entire rest of my project I'll do it with PHP 7.x in mind. – aussiedan Apr 25 '17 at 08:17
  • @aussiedan double work, not very time efficient, why not just start by using PHP 7? – Script47 Apr 25 '17 at 08:19
  • 1
    Possible duplicate of [Generating random numbers without repeats](http://stackoverflow.com/questions/17778723/generating-random-numbers-without-repeats) – Script47 Apr 25 '17 at 08:20
  • @Script47 because I have already spent months building the site spread over the last few years haha. With my next project I'll be using PHP 7. – aussiedan Apr 25 '17 at 08:49
  • @aussiedan Why would you pick up a website that is several years old, considering web standards, languages, etc are changing so frequently. That's like developing a site in 2007 and thinking it's OK to pick it up now and continue working off of the same framework. – Harry Kitchener Apr 25 '17 at 12:38

1 Answers1

1

if I understood your correctly I think this code is what you're looking for. In this case since you're using AJAX we'll be saving used questions like array in session.

<?php
    if(!isset($_SESSION)) {
        session_start();

        $_SESSION['used'] = ((!isset($_SESSION['used'])) ? ([]) : ($_SESSION['used']));
    }

    //--------------------------------------------------------------------------
    // 1) Connect to mysql database
    //--------------------------------------------------------------------------
    include 'DB.php';

    $con = mysql_connect($host, $user, $pass);
    $dbs = mysql_select_db($databaseName, $con);

    $array = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99];
    $array_unique = [];

    while(!empty($array_unique = array_diff($array, $_SESSION['used']))) {
        //pick a random point from the array
        $random = array_rand($array_unique, 1);

        // Save the used element in session
        $_SESSION['used'][] = $array_unique[$random];

        //store the random question number
        $question = $array_unique[$random];

        // Select information from database and use array to assign to the selected row.
        $query = mysql_query("SELECT `number`,`association`,`image_file`,`skeleton`,`sound`,`colour`,`comments` FROM ".$tableName." WHERE `number` = ".$question." LIMIT 0,1");

        //fetch result to print on page

        $row = mysql_fetch_row($query);

        //Echo result as json
        echo json_encode($row);
    }
    if(count($array_unique) == 0) {
        $_SESSION['used'] = [];
    }
?>

Just use while and array_rand

b0ne
  • 653
  • 3
  • 10
  • Thanks b0ne, I tried that but I'm still getting repeats. I assume it's because I'm using ajax to call a page with the php? So would that kill the array and reload it every time, therefore resetting the array continuously on load? – aussiedan Apr 25 '17 at 09:09
  • You're very welcome, try it now with the updated code. – b0ne Apr 25 '17 at 09:19
  • I've updated the code above and it's not showing anything now at all. Maybe calling the database is killing it? But I need to call it to get the information form the database. – aussiedan Apr 25 '17 at 09:41
  • Try it out now. – b0ne Apr 25 '17 at 09:46
  • seems ` while(!empty($array)) { ` to be causing issue, as it shows nothing and when I remove it from the code including the trailing ` } ` then it runs, but still seems to show duplicates but not as often. But since it's showing duplicates I assume it means it's skipping the code you created? – aussiedan Apr 25 '17 at 10:01
  • Actually that's because we have ORDER BY RAND() so removed that. Doing another test. – aussiedan Apr 25 '17 at 10:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142591/discussion-between-aussiedan-and-b0ne). – aussiedan Apr 25 '17 at 10:11