Php 如何给玩家提供唯一的号码
我正在制作一个脚本,当玩家数量为4时,我将给出玩家数量。现在我有了一个玩家表,我将用玩家编号更新它,但我似乎无法同时生成唯一编号和更新表循环。。。在这种情况下的脚本,我会给所有的球员3号 到目前为止,我得到的是:Php 如何给玩家提供唯一的号码,php,while-loop,Php,While Loop,我正在制作一个脚本,当玩家数量为4时,我将给出玩家数量。现在我有了一个玩家表,我将用玩家编号更新它,但我似乎无法同时生成唯一编号和更新表循环。。。在这种情况下的脚本,我会给所有的球员3号 到目前为止,我得到的是: <?PHP include '../config.php'; $con = mysql_connect($hostip, $mysqluser, $mysqlpass); if (!$con) { die('Could not connect: ' . mysql_er
<?PHP
include '../config.php';
$con = mysql_connect($hostip, $mysqluser, $mysqlpass);
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($mysqldatabase, $con);
$gameid = '08e893ac1c4446758ee44423c173c5aa';
/////////////////////////// mysq connect end
$sql=mysql_query("SELECT DISTINCT username FROM table");
$pn = array();
while ( count($pn) < 4 ) {
$random = mt_rand(1,4);
if ( !in_array($random,$pn) ) {
$pn[] = $random;
$rr = $random;
while ($rowx = mysql_fetch_array($sql)) {
echo $rr.' '.$rowx['username'].' ';
}
}
}
print_r($pn); ?>
我猜它只得到第一个数字。。。我也尝试过其他方法,但这似乎是一个更好的解决方案
//编辑解决方案
$randoms = range(1, 4);
shuffle($randoms);
while ($rowx = mysql_fetch_array($sql)){
$random = array_shift($randoms);
echo $random.' '.$rowx['username'].' ';
}
不要在循环中调用mt_rand,提前生成一个随机列表,并在需要时选择号码:
$randoms = range(1, 4);
shuffle($randoms);
while(...) {
$random = array_shift($randoms);
不要在循环中调用mt_rand,提前生成一个随机列表,并在需要时选择号码:
$randoms = range(1, 4);
shuffle($randoms);
while(...) {
$random = array_shift($randoms);
$randoms=范围(1,4);洗牌($randoms);while($rowx=mysql\u fetch\u array($sql)){$random=array\u shift($randoms);echo$random.'.'.$rowx['username'.'.';}$randoms=range(1,4);洗牌($randoms);while($rowx=mysql_-fetch_-array($sql)){$random=array_-shift($randoms);echo$random.''.$rowx['username'].''.}