PHP 基本随机报价生成器.如何防止最近出现的报价



编辑:感谢您更正格式...没有注意到那个错误

我已经完成了这个基本的报价生成器作为第一个项目,但注意到它经常在刷新时显示相同的报价。当然,目前只有五个,但是当我填写数据库时,这个问题可能仍然很明显。我目前正在学习更高级的JQuery AJAX - 但才刚刚开始。据我所知,这不是一个重复的问题。在重新加载时显示数据库查询。谢谢!这是代码:

连接.php:

<?php
$connection = new mysqli('localhost', 'root', '', 'random_quotes');
if ($connection->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ')'
        . $mysqli->connect_error);
}
?>

功能.php:

<?php
require ('connect.php');
$query = "SELECT id, quote, author FROM quotes ORDER BY RAND() LIMIT 1";
    $getQuote = $connection->query($query);
?>

.HTML:

<!doctype html>
<html lang="en">
<?php
require ('includes/connect.php');
require ('includes/functions.php');
?>
<head>
  <meta charset="utf-8">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  <link href='https://fonts.googleapis.com/css?family=Candal' rel='stylesheet' type='text/css'>
  <link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div class="container-fluid text-center">
    <h1>Random Quote Generator</h1>
    <p>Some of my favourite all around quotes!</p>
    <br/>
    <button class="btn btn-default" onclick="newQuote()" type="submit">New Quote
    </button>
    <div class="quote_wrap text-center">
        <span class="quote">
            <?php
            while($row = $getQuote->fetch_assoc()){
                $stringID = $row['id'];
                $stringQuote = $row['quote'];
                $stringAuthor = $row['author'];
                echo $stringQuote;
                }
              ?>
        </span>
      </br>
      <div class="author text-center"><?php echo $stringAuthor?></div>
      <div class="quoteid" id="<?php echo $stringID?>"></div>
    </div>
</div>
<script src="https: /ajax.googleapis.com/ajax/libs/jquery/2.2.3/jquery.min.js"></script>
<script type="text/javascript">
function newQuote() {
    location.reload();}
</script>
</body>
</html>

生成一个随机数,然后通过该 id 访问引号。 可能效果更好。

    $query = "SELECT COUNT(*) as noQuotes FROM quotes ";
    $result = $connection->query($query);
    $row = $result->fetch_assoc();
    $noQuotes = $row["noQuotes"];
    srand(time());
    //echo(rand(1,$noQuotes));
    $randNo = rand(1,$noQuotes);

    $query = "SELECT id, quote, author FROM quotes WHERE id = $randNo ";
    $getQuote = $connection->query($query);

最新更新