简体   繁体   English

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

[英]PHP Basic Random Quote Generator. How do I prevent quotes that have appeared recently?

EDIT: Thank you for correcting the formatting... did not notice that mistake 编辑:谢谢您纠正格式...没有注意到该错误

I have completed this basic quote generator as a first project, but have noticed that it frequently displays the same quote upon refresh. 我已经完成了第一个项目的基本报价生成器,但是注意到它经常在刷新时显示相同的报价。 Granted, there are only five at the moment, but this problem might still be apparent as I fill the database. 当然,目前只有五个,但是当我填充数据库时,这个问题可能仍然很明显。 I am currently learning JQuery AJAX for a more advanced one - but am only starting. 我目前正在学习JQuery AJAX,这是一种更高级的语言-但仅仅是开始。 To my knowledge, this is not a duplicate question. 据我所知,这不是重复的问题。 Displays database query upon reload. 重新加载时显示数据库查询。 Thank you! 谢谢! Here is the code: 这是代码:

connect.php: connect.php:

<?php

$connection = new mysqli('localhost', 'root', '', 'random_quotes');

if ($connection->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ')'
        . $mysqli->connect_error);
}
?>

functions.php: functions.php:

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

HTML: 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>

Generate a random number and then access quote by that id. 生成一个随机数,然后按该ID访问报价。 May work better.. 可能会更好。

    $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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM