MySQL / PHP结果无序显示

时间:2017-12-30 02:38:11

标签: php mysql

我正在一个群众评分网站上工作,该网站从列表中的节目中随机选择剧集,并生成用户点击的按钮,每个按钮都表示对该剧集的投票。

我已经发现,由于某种原因,第一集("小猫")不是页面上的第一集,即使这是显示的内容代码似乎应该说。这实际上不是一个问题,因为我可以继续开发而不需要修复'它,但这是一个不直观的结果,所有事情都是平等的,我不喜欢在我的代码中包含我不理解的东西。

$total_kittens_query = 'SELECT max(`id`) AS max FROM `kittens`';
$query_result = mysqli_query($link,$total_kittens_query);

$count = mysqli_fetch_array($query_result);

$random_kitten_one = mt_rand(1, $count['max']);
$random_kitten_two = mt_rand(1, $count['max']);

while($random_kitten_one == $random_kitten_two) {
    $random_kitten_two = mt_rand(1, $count['max']); // Prevent duplicates
}

$query = "SELECT * FROM `kittens` WHERE `kittens`.`id`='" . $random_kitten_one . "' OR `kittens`.`id`='" . $random_kitten_two . "'";

$result = mysqli_query($link,$query);

if(mysqli_num_rows($result) == 2) {
    // Create a game
        $game_query = "INSERT INTO `games` (`kitten_one`, `kitten_two`, `winner`, `ip`) VALUES ('" . $random_kitten_one . "', '" . $random_kitten_two . "', -1, '" . $_SERVER['REMOTE_ADDR'] . "')";
    $result2 = mysqli_query($link,$game_query);
    // Game table: id (auto_inc, int, 10), kitten_one (int, 10), kitten_two (int, 10), winner (int, 10), ip (varchar, 15)
    $game_id = mysqli_insert_id($link);

    $kitten_one = mysqli_fetch_array($result);
    $kitten_two = mysqli_fetch_array($result);

    echo "<table border='1' cellspacing='5' cellpadding='5'><tr><th scope='row'>Name:</th>";
echo "<td>".$kitten_one['name']."</td><td>".$kitten_two['name']."</td></tr><tr><th scope='row'>Album:</th><td>".$kitten_one['album']."</td><td>".$kitten_two['album'].""; 
echo "</td></tr><tr><th scope='row'>Summary:</th><td>".$kitten_one['summary']."</td><td>".$kitten_two['summary']."</td></tr><tr><th scope='row'>Quote(s):</th><td>".$kitten_one['quotes']."</td><td>".$kitten_two['quotes']."</td></tr>
<tr><th scope='row'>Elo Rating:</th><td>".$kitten_one['elo']."</td><td>".$kitten_two['elo']."</td></tr></table>";
echo  "<table id='ratings' border='5' cellspacing='5' cellpadding='5'>";
echo "<thead><tr><th>Name</th><th>Matches</th><th>Wins</th><th>Win Rate</th><th>Elo</th></tr>";
echo "</thead>";

    echo '<form action="?" method="post"><input type="hidden" name="game_id" value="', $game_id, '"><input type="hidden" name="kitten_id" value="', $kitten_one['id'], '"><input type="submit" value="Vote 4 ', $kitten_one['name'], '"></form>';
    echo '<form action="?" method="post"><input type="hidden" name="game_id" value= "', $game_id, '"><input type="hidden" name="kitten_id" value="', $kitten_two['id'], '"><input type="submit" value="Vote 4 ', $kitten_two['name'], '"></form>';
} else {
    die('Error in the matrix!');
}

0 个答案:

没有答案