你好我有这样的表:
员工
EmployeeID EmployeeName
1234 Minjun
1235 Nichkhun
1236 Taecyeon
1237 Wooyoung
1238 Junho
1239 Chansung
我使用了这个源代码:
<?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';
$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>";
}
?>
显示如下的随机数据:
E = Junho
B = Nichkhun
C = Taecyeon
F = Chansung
D = Wooyoung
A = Minjun
或者像这样:
B = Nichkhun
D = Wooyoung
F = Chansung
E = Junho
A = Minjun
C = Taecyeon
问题是我想像这样显示10种varian随机:
E = Junho
B = Nichkhun
C = Taecyeon
F = Chansung
D = Wooyoung
A = Minjun
F = Chansung
D = Wooyoung
C = Taecyeon
E = Junho
B = Nichkhun
A = Minjun
A = Minjun
D = Wooyoung
E = Junho
B = Nichkhun
F = Chansung
C = Taecyeon
D = Wooyoung
B = Nichkhun
C = Taecyeon
A = Minjun
E = Junho
F = Chansung
C = Taecyeon
D = Wooyoung
F = Chansung
A = Minjun
E = Junho
B = Nichkhun
A = Minjun
F = Chansung
C = Taecyeon
E = Junho
D = Wooyoung
B = Nichkhun
C = Taecyeon
D = Wooyoung
F = Chansung
B = Nichkhun
A = Minjun
E = Junho
E = Junho
C = Taecyeon
B = Nichkhun
F = Chansung
D = Wooyoung
A = Minjun
E = Junho
B = Nichkhun
A = Minjun
D = Wooyoung
F = Chansung
C = Taecyeon
D = Wooyoung
A = Minjun
C = Taecyeon
B = Nichkhun
F = Chansung
E = Junho
所以我添加这样的代码:
<?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';
$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.
for ($i=1; $i<=10; $i++){
foreach($array as $row) {
echo "<DIV>" . $row["letter"] . " = " . $row['EmployeeName'] . " </div>";
echo "";
}
}
?>
但结果是这样的(将一个结果循环10次):
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
C = Taecyeon
D = Wooyoung
B = Nichkhun
A = Minjun
F = Chansung
E = Junho
你可能知道问题出在哪里?谢谢
答案 0 :(得分:3)
更新您的'for循环',如下所示,然后尝试
//shuffle($array); //Comment this line
for ($i=1; $i<=10; $i++){
shuffle($array);
foreach($array as $row) {
echo "<DIV>" . $row["letter"] . " = " . $row['EmployeeName'] . " </div>";
echo "";
}
}