gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 20:53:59 24 4
gpt4 key购买 nike

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

我已经完成了这个基本报价生成器作为第一个项目,但注意到它在刷新时经常显示相同的报价。当然,目前只有五个,但当我填充数据库时,这个问题可能仍然很明显。我目前正在学习 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);

关于PHP 基本随机报价生成器。如何防止最近出现的报价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37781120/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com