gpt4 book ai didi

php - 如何在 PHP 和 MySQL 上使用循环?

转载 作者:行者123 更新时间:2023-11-29 05:17:04 25 4
gpt4 key购买 nike

你好,我有这样的表:

员工

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com