如何使用PHP在MySQL上显示随机数据?

时间:2015-07-08 02:17:56

标签: php mysql random xampp random-sample

你好我有这样的表:

员工

EmployeeID  EmployeeName 
1234        Nayeon     
1235        Jihyo     
1236        Jungyeon     
1237        Dahyun     
1238        Sana       
1239        Mina
1240        Tzuyu
1241        Chaeyeong
1241        Chaeyeong
1242        Momo

我使用了这个源代码:

<?php

mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("databasetransport") or die(mysql_error());

$employees = mysql_query("SELECT * FROM Employee ORDER BY EmployeeID") 
or die(mysql_error());  

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$position = 0;
$position2 = 0;
$toomany = '';

while($row = mysql_fetch_array( $employees )) {
    echo "<DIV>" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeName'] . " </div>";
      $position ++;
    if($position > 25) {
        $position = 0;
        $position2 ++;
        if($position2 > 25) { echo "We need to rethink this idea."; break; }
        $toomany = substr($letters, $position2, 1);
    }
}
?>

显示这些数据:

 A  = Nayeon     
 B  = Jihyo     
 C  = Jungyeon     
 D  = Dahyun     
 E  = Sana       
 F  = Mina
 G  = Tzuyu
 F  = Chaeyeong
 H  = Chaeyeong
 I  = Momo

问题是我想像这样随机抽取数据(来自之前的结果):

C  = Jungyeon 
A  = Nayeon 
H  = Chaeyeong    
B  = Jihyo 
I  = Momo        
F  = Mina
G  = Tzuyu
E  = Sana  
F  = Chaeyeong
D  = Dahyun     

所以我添加这样的代码:

 <?php

mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("databasetransport") or die(mysql_error());

$employees = mysql_query("SELECT * FROM Employee ORDER BY EmployeeID") 
or die(mysql_error());  

$letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$position = 0;
$position2 = 0;
$toomany = '';

while($row = mysql_fetch_array( $employees )) {
    echo "<DIV>" . $toomany.substr($letters, $position, 1) . " = " . $row['EmployeeID'] . " </div>";
      $position ++;
    if($position > 25) {
        $position = 0;
        $position2 ++;
        if($position2 > 25) { echo "We need to rethink this idea."; break; }
        $toomany = substr($letters, $position2, 1);
    }
}


function generateRandomString($length = 10) {
    $characters = $positions;
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}


echo generateRandomString();

?>

但什么都没发生(LOL)你可能知道问题在哪里吗?谢谢

3 个答案:

答案 0 :(得分:1)

只需构建您的数组,然后使用此

http://php.net/manual/en/function.shuffle.php

欢呼声。

$a = array(
    'A = Nayeon',
    'B = Jihyo',
    'C = Jungyeon',
    'D = Dahyun',
    'E = Sana',
    'F = Mina',
    'G = Tzuyu',
    'F = Chaeyeong',
    'H = Chaeyeong',
    'I = Momo',
 );
 shuffle( $a );
 var_export( $a );

输出:

array (
  0 => 'I = Momo',
  1 => 'E = Sana',
  2 => 'F = Chaeyeong',
  3 => 'F = Mina',
  4 => 'B = Jihyo',
  5 => 'A = Nayeon',
  6 => 'C = Jungyeon',
  7 => 'G = Tzuyu',
  8 => 'D = Dahyun',
  9 => 'H = Chaeyeong',
)
  • 作为这里的一方,shuffle不维护数组键,并且它修改数组意味着shuffle的实际返回值是布尔值(true | false)。
  • 但它会保留带有分配字母的名称(如果可取的话)
  • 简单快捷
  • 可读
  • 确保您不要将同一行拉出两次。

答案 1 :(得分:0)

我认为你可以在SQL中完成所有这些:

select (@c := CHAR(ASCII(@c) + 1)) as c, EmployeeName
from pegawai cross join
     (select @c := 'A') params
order by rand();

这会以随机顺序分配字符。如果您想要EmployeeId订单:

select p.*
from (select (@c := CHAR(ASCII(@c) + 1)) as c, EmployeeName
      from pegawai cross join
           (select @c := 'A') params
      order by EmployeeId
     ) p
order by rand();

答案 2 :(得分:0)

您可以将查询更改为:

$employees = mysql_query("SELECT * FROM pegawai ORDER BY RAND()") 

然后:

$letters = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ'); //Use is as array
shuffle($letters); //Mix the array
$position = 0;
$position2 = 0;
$toomany = '';
    while($row = mysql_fetch_array( $employees )) {
        echo "<DIV>" . $toomany[$position] . " = " . $row['EmployeeID'] . " </div>";
          $position ++;
        if($position > 25) {
            $position = 0;
            $position2 ++;
            if($position2 > 25) { echo "We need to rethink this idea."; break; }
            $toomany = substr($letters, $position2, 1);
        }
    }