作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好,我有这样的表:
员工
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 种随机变量:
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
请问问题出在哪里?谢谢
最佳答案
如下所示更新您的“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 "";
}
}
关于php - 如何在 PHP 和 MySQL 上使用循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31333057/
我是一名优秀的程序员,十分优秀!