gpt4 book ai didi

php - 查询中的 ORDER BY 不适用于循环

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

如果您在这张图片中看到...

enter image description here

用户名 pfar54 出现两次。我有一个具有此排序方法的查询..

ORDER BY up.ordering

如果用户的 ordering 为 2,那么我希望玩家姓名出现在第二个输入中,而不是创建另一列。

这就是我的playerByUser 数据库中的全部内容...

id userid playername    ordering
1 1 Tom Brady 1
2 85 Aaron Rodgers 1
3 1 Eddie Lacey 2

出于某种原因,它循环遍历所有内容,而不是按排序列对其进行排序。该页面应该看起来更像这样..

pfar54      Big Daddy
Tom Brady Aaron Rodgers
Eddie Lacey

我的 SELECT 查询或循环做错了什么,不允许用户在一行中拥有多个玩家,而不是为新玩家创建一个新列?

try {
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$stmt = $con->prepare("SELECT up.ordering, u.id, u.username, up.playername
FROM users AS u
INNER JOIN playersByUser AS up ON u.id = up.userid
WHERE u.group IN (3,4,5)
ORDER BY up.ordering");
if ( !$stmt || $con->error ) {
// Check Errors for prepare
die('User/Player SELECT prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$stmt->execute()) {
die('User/Player SELECT execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
} catch (Exception $e ) {
die("User/Player SELECT execute() failed: " . $e->getMessage());
}
$stmt->bind_result($ordering, $userid, $username, $playername);
<form action="" method="POST">
<?php

while ($stmt->fetch()) {
$playersArray[['userid']]=['playername'];
?>
<div class="draftResultsWrap">
<div class="inline">
<?php echo "<div>" . $username . "</div>"; ?>
</div>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='1 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='2 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='3 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='4 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='5 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='6 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='7 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='8 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='9 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='10 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='11 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='12 <?php echo $playername;?>'/>
<input type="text" name="playerarray<?php echo $userid; ?>[]" class="draftBorder" value='13 <?php echo $playername;?>'/>
</div>
<?php
}
?>

最佳答案

<table class="draftResultsWrap">
<tr><th>Username</th><th>Playername</th></tr>
<?php while ($stmt->fetch()){?>
<?php echo "
<tr>
<td><input name='username[$userid]' value='$username'/></td>
<td><input name='playername[$userid]' value='$playername'/></td>
</tr>
"; ?>

<?php }?>
</table>

关于php - 查询中的 ORDER BY 不适用于循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32384893/

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