dongtan9518 2015-07-10 02:58
浏览 50
已采纳

如何使用PHP在MySQL之前显示序列随机数据?

Hello i have tables like this :

Employee

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

i used this source code :

<?php

mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("Employee") 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);
    }
}
?>

to display these data :

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

The problem is i want to random that data like this (from the result before):

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

so i add codes like this :

 <?php

mysql_connect("localhost", "root", "1234") or die(mysql_error());
mysql_select_db("Employee") 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();

?>

but does not work, may you know where is the problem? Thank you

  • 写回答

1条回答 默认 最新

  • dongxin991209 2015-07-10 03:33
    关注

    You could use Shuffle to randomize the array you get from mysql_fetch_array.

    It would look something like this:

    $letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
    $array = array();
    
    while($row = mysql_fetch_assoc($employees)){
        $array[] = $row
    }
    
    $i = 0;
    
    // Assign a letter to each employee in the correct order
    foreach($array as $Key => $row){
        $array[$Key]["letter"] = substr($letters, $i, 1);
        $i++;
        if($i > 25){
            $i = 0;
        }
    }
    
    // Shuffle the array
    shuffle($array);
    
    // Print each entry with correctly assigned letter and name.
    foreach($array as $row) {
        echo "<DIV>" . $row["letter"] . " = " . $row['EmployeeName'] . " </div>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置