随机播放的结果不会以随机顺序发送到数据库

时间:2015-09-04 01:58:31

标签: php mysql shuffle

我有一个页面我正在洗牌用户订单。我想将该订单的结果发送到我的数据库中,以便我可以在以后使用该订单。我认为结果是按照他们在洗牌时出现的顺序发送的,但事实并非如此,我不确定他们发送的是什么顺序。

$query = mysqli_query($con, "SELECT * FROM users WHERE `group` IN (3, 4 ,5)");

echo 'Users to be given draft order: <br>';
$array = array();
while ($row = mysqli_fetch_assoc($query)) {
    $array[] = $row;
    echo $row['firstname'] . ' ' . $row['lastname'] . '<br>';
}
?>
<form method="

POST" name="form">
    <input type="submit" value="Create Draft Order" name="shuffle">
     <input type="hidden" name="shuffleButton" value="1">
</form>

Shuffled results: <br>
<div class="main-bag">     
<div class="shuffle_results" id="results"></div> 
 <form method="post">
    <input type="submit" value="Finalize Draft Order" name="insert">
<?php
$count = 0;
foreach ($array as $result) :
$count++;
$shuffle_count = $count;
    $shuffle_firstname = htmlentities($result['firstname']);
    $shuffle_lastname = htmlentities($result['lastname']);
    $shuffle_id = htmlentities($result['id']);
    $shuffle_username = htmlentities($result['username']);
    $shuffle_email = htmlentities($result['email']);
?>
    <input type="hidden" name="count[]" value="<?php echo $shuffle_count; ?>">
        <input type="hidden" name="firstname[]" value="<?php echo $shuffle_firstname; ?>">
        <input type="hidden" name="lastname[]" value="<?php echo $shuffle_lastname; ?>">
        <input type="hidden" name="id[]" value="<?php echo $shuffle_id; ?>">
        <input type="hidden" name="username[]" value="<?php echo $shuffle_username; ?>">
        <input type="hidden" name="email[]" value="<?php echo $shuffle_email; ?>">

<?php 

    endforeach;
     if ( isset($_POST['shuffleButton'] ) ) :
        echo '<input type="submit" value="Finalize Draft Order" name="insert">';
    endif;

?>
</form>
<?php        
if (isset($_POST['insert'])) {
    $ordering_number = "1";

    $con = mysqli_connect("localhost", "", "", "");
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $draft_stmt1 = $con->prepare("INSERT INTO user_players (user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?)");
  if ( false===$draft_stmt1|| false===$draft_stmt2 ) {
         // Check Errors for prepare
        die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
    }
    $draft_stmt1->bind_param('issss', $shuffle_id, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);

    foreach ($_POST['id'] as $i => $shuffle_id) {
        $shuffle_firstname = $_POST['firstname'][$i];
        $shuffle_lastname = $_POST['lastname'][$i];
        $shuffle_username = $_POST['username'][$i];
        $shuffle_email = $_POST['email'][$i];

        $draft_stmt1->execute() or
            die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));

有没有人知道为什么这不会按照我正在洗牌的顺序发送?我的数据库表中有一个基本的id字段,这样每个插入的用户都会按照它们被起草的顺序给出id,但是它没有那样工作。

对不起所有的代码,我想确保不要忘记可能有用的东西谢谢!

更新

新代码

  $draft_stmt1 = $con->prepare("INSERT INTO user_players (id, user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?, ?)");

 if ( false===$draft_stmt1|| false===$draft_stmt2 || false===$draft_stmt3) {
         // Check Errors for prepare
        die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
    }
    $draft_stmt1->bind_param('iissss', $i, $shuffle_id, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);

    foreach ($_POST['id'] as $i => $shuffle_id) {
        $shuffle_firstname = $_POST['firstname'][$i];
        $shuffle_lastname = $_POST['lastname'][$i];
        $shuffle_username = $_POST['username'][$i];
        $shuffle_email = $_POST['email'][$i];

        $draft_stmt1->execute() or
            die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));

这是用户随机播放的地方..

$query = mysqli_query($con, "SELECT * FROM users WHERE group IN (3,4,5) ORDER BY id");
$array = array();

while ($row = mysqli_fetch_assoc($query)) { 
$array[] = array( 
'id' => $row['id'],
'firstname' => $row['firstname'], 
'lastname' => $row['lastname'], 
'username' => $row['username'], 
'email' => $row['email'] 
); 

if (isset($_POST['shuffle'])) {

   }
}
shuffle($array);
    echo json_encode($array);

1 个答案:

答案 0 :(得分:2)

在表格中添加一列shuffle_order,并在此列中存储从表单收到玩家的顺序。

$draft_stmt1 = $con->prepare("INSERT INTO user_players (shuffle_order, user_id, firstname, lastname, username, email) VALUES (?, ?, ?, ?, ?, ?)");
if ( false===$draft_stmt1|| false===$draft_stmt2 ) {
    // Check Errors for prepare
    die('Add to user players prepare() failed: ' . htmlspecialchars($con->error));
}
$draft_stmt1->bind_param('iissss', $i, $shuffle_id, $shuffle_firstname, $shuffle_lastname, $shuffle_username, $shuffle_email);

foreach ($_POST['id'] as $i => $shuffle_id) {
    $shuffle_firstname = $_POST['firstname'][$i];
    $shuffle_lastname = $_POST['lastname'][$i];
    $shuffle_username = $_POST['username'][$i];
    $shuffle_email = $_POST['email'][$i];

    $draft_stmt1->execute() or
        die('Add to user players execute() failed: ' . htmlspecialchars($draft_stmt1->error));
}

然后,当您想要以随机顺序处理数据时,请在ORDER BY shuffle_order查询中使用SELECT