I need to run a query and get vars matching my params into an array. Then I need to display one var at a time in arandom order. Starbucks has not kicked in yet -- need help to jump start...

I was thinking something like this but need help connecting the dots:


// create array somehow... I use a class to query
$myArray = array();
$i = 0;

$myVarsArray = $db->get_results ("SELECT myVar FROM myTable WHERE a = 1 ") 
foreach ($myVarsArray as $vars) 
{
// create array ?
$myArray[$i++] = $vars->myVar;
}

// get random number from that array

$myRandomNo = ' ';

// display results 
WHERE dataID =  $myRandomNo

    Well after reading the results to the array you could use shuffle() to randomize the whole array or use array_rand() to pick one randomly from the array.

    You can also use RAND straight in the sql query if you like. shuffle would be my personal choice.

      OK, arrays is my weakness. Say I have an array:

      Array ( [0] => 30 [1] => 76 [2] => 46 [3] => 54 [4] => 63 [5] => 73 [6] => 32 [7] => 68 )

      And to complicate the matters, I'd like to write the array into a session, so that Idon't have to query db more while the user is on the site. Would it be something like:

      $_SESSION['myVars'] = $myArray;
      

      How do I get the random number in this case? My guess is that I need to randomize the KEY as oppose to VALUE of the array... am I getting warmer?

      If I have a random number I can get my results like this:

      $randomNum = 2;
      echo $myArray[$randomNum]; // prints 46
      

        You could also store an array index in the session, starting from position 0. Every time you use a value from the array, you increment the index, and wraparound (modulo the array size) as needed.

          that may work, but at this point my array has only 8 values, what happens if the user refreshes the page 9 times?

            what happens if the user refreshes the page 9 times?

            That's where the wraparound comes in.

              dah!

              I think I like it but I don't want to keep it in a perticular loop. The number of values is too low to figure out what's coming next. I'd like to keep it totally random.

              I think I'm figuring out the rest. So I rewrote the query to set limit to 8. So I guess all I need is to pull a random number from 0 to 7.

              srand ((double) microtime()* 1000000);
              $value = rand(1,8);
              

              Would this work of will it skip the first array value [0]?

                OK, I got it to work but once in a while I get a blank instead of the value.

                Here's what I've got:

                
                srand ((double) microtime()* 1000000);
                $random_N = rand(1,8);
                
                // I've got an array with 8 values
                // Array ( [0] => 30 [1] => 76 [2] => 46 [3] => 54 [4] => 63 [5] => 73 [6] => 32 [7] => 68 )
                
                $_SESSION['randomNum'] = $myArray;
                
                echo $_SESSION['randomNum'][$random_N];
                

                Why do I get blanks and what did I miss?

                  The caffeine is reaching my brain!

                  I fixed it. I set $i = 1 instead of 0 and so my array starts with 1 and goes to 8.

                    Using cahva's idea, what you will do is this:
                    1. Shuffle the array ($array), and save it in the session.
                    2. Save an index ($index) with value 0 in the session.
                    3. Retrieve $array[$index].
                    4. $index++
                    5. Goto 3.

                    [man]array_rand/man is probably simpler here, since it automatically works for an array of any size. You use of rand(1, 8) would result in an array out of bounds error if 8 is returned.

                      Write a Reply...