简体   繁体   English

首次登录时如何随机获取记录?

[英]How to get records randomly when first time login?

Understanding about Question: 关于问题的理解:

  1. When any user logged into website , there is one page shows records coming from mysql database table. 当任何用户登录网站时,会有一页显示来自mysql数据库表的记录。 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. 但是,当用户再次登录时,顺序将再次随机更改。

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). 对于缓存,您可以使用功能serialize并将数据保存到文件(每个用户的唯一名称)。 Then just unserialize form file. 然后只需反unserialize表单文件即可。

Example: 例:

Lanch login() to simulate user login to site, and then replace it with notLogin() to simulate user already login on site. 启动login()以模拟用户登录到站点,然后将其替换为notLogin()以模拟已经在站点登录的用户。 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. 您可以在用户lo-gin &&注销后将其删除时将数据存储在Session中。

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 谢谢

将记录获取到数组中并使用shuffle命令http://php.net/manual/en/function.shuffle.php

RAND() function in SQL returns Random records from table. SQL中的RAND()函数从表中返回随机记录。

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 如果您要以最初显示的顺序持久保存记录,并在多页中显示,请在获取Session变量时保存数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM