0

Understanding about Question:

  1. When any user logged into website , there is one page shows records coming from mysql database table. What I want is to show records randomly when user first time shows that page. Then after the records must have same sequence of display as user shows first time when logged into account.

  2. The sequence of records remain persist until logout.

  3. But the sequence will again change randomly when user logged in again.

Anand Solanki
  • 3,419
  • 4
  • 16
  • 27

4 Answers4

2

Create cache with results when user first login and then just read from cache, not from DB.

For cache you can use function serialize and save data to file (unique name for each user). Then just unserialize form file.

Example:

Lanch login() to simulate user login to site, and then replace it with notLogin() to simulate user already login on site. Notice that users points change when user is login, but not when he is already on site.

//when user is login
function login(){
    //1. here you log in user;
    $userID = '345353';
    //2. And you get some data drom DB
    $randomRowsFromDb = getRandomDataFromDB();
    //3. Save it to cache
    saveToCache($userID, $randomRowsFromDb);
    //4. Display it (optional)
    display($randomRowsFromDb);
}

//when user is already on site
function notLogin(){
    $userID = '345353';
    $data = loadFromCache($userID);//load from cache
    display($data);//display cached data instead of taking it from DB
}
//function geting random data form DB
function getRandomDataFromDB(){
    return 
    array(
    array('id'=>'43534','login' => 'John', 'points' => rand(0,100)),
    array('id'=>'27725','login' => 'Anna', 'points' => rand(0,100)),
    array('id'=>'23664','login' => 'Jerremy', 'points' => rand(0,100)),
    array('id'=>'87855','login' => 'Kate', 'points' => rand(0,100)));
} 

function display($dataToDisplay){
    var_dump($dataToDisplay);
}

function saveToCache($userID, $data){
    file_put_contents($userID.'.cache', serialize($data));
}

function loadFromCache($userID){
    if (file_exists($userID.'.cache')){
        $file = file_get_contents($userID.'.cache');
        return unserialize($file);
    }else{
        //in case cache is missing
        $data = getRandomDataFromDB();
        saveToCache($userID, $data);
        return $data;
    }
}
Volvox
  • 1,639
  • 1
  • 12
  • 10
1

I get one good solution:

>> After login use rand function.
>> When you fetch the records, stores them in SESSION var for future use.
>> Next time when page loads, use session vars to show data.

OR

You can store data in Session when user lo-gin && remove it when log out.

Like:

<?php

session_start();

mysql_connect('localhost', 'root', '');
mysql_select_db('test');

$query = "SELECT children, name FROM tree ORDER BY RAND()";
$result = mysql_query($query);


while ($row = mysql_fetch_assoc($result)) {
    $_SESSION['tree'][] = $row['children'];
}

header("Location: blank.php");
?>
  • Thanks
Anand Solanki
  • 3,419
  • 4
  • 16
  • 27
0

Get records into array and use shuffle command http://php.net/manual/en/function.shuffle.php

0

RAND() function in SQL returns Random records from table.

Select * from table_name order by RAND() Limit 10 

If you want to persist record in the same order that it first showed, and show it in multiple pages, save the array on fetching in Session variable

Deepika Janiyani
  • 1,487
  • 9
  • 17
  • 1
    It is not recommended to use RAND() for ordering purposes, see http://stackoverflow.com/questions/14330798/mysql-order-by-rand-performance-issue-and-solution – Juraj Blahunka Oct 21 '13 at 10:02