Understanding about Question:
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.
The sequence of records remain persist until logout.
But the sequence will again change randomly when user logged in again.
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;
}
}
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");
?>
将记录获取到数组中并使用shuffle命令http://php.net/manual/en/function.shuffle.php
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.