查询中的ORDER BY不使用循环

时间:2015-09-03 20:29:49

标签: php mysql loops while-loop

如果您在此图片中看到...

enter image description here

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

ORDER BY up.ordering

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

这就是我在我的玩家中使用的所有数据库......

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 
}
?> 

1 个答案:

答案 0 :(得分:0)

<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> 
相关问题