gpt4 book ai didi

php - 随机帖子的动态链接与帖子 ID 不符

转载 作者:行者123 更新时间:2023-11-29 03:32:59 25 4
gpt4 key购买 nike

下面的代码应该显示我的表格中的随机帖子;它执行此操作,但它显示的帖子的 ID 与 URL 中的 ID 不同。

如何让它显示 URL 中的内容?谢谢。

$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');           
$sql = $db->query('SELECT id FROM posts ORDER BY RAND() LIMIT 1');
$row = $sql->fetch(PDO::FETCH_ASSOC);
echo "<a href='random?page=".$row['id']."'>Randomize!</a><br>";

请在下面找到“random.php”页面的相关部分:

$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
$sql = $db->query('SELECT * from posts ORDER BY RAND() LIMIT 1');

$row = $sql->fetch(PDO::FETCH_ASSOC);
echo "<a href = 'random.php?page=".$row['id']."'>Re - Randomize!</a><br>";
if ($row['sort'] == "image"){
echo "<b>Update: </b>", $row['tag'], "<br>";
echo "<img src='resize.php?w=240&img=./uploads/".$row['message']."'><br>";
echo "<b>", "By: ", "</b>", ($row['username']), "<br>";
echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";
if($row['locked'] !='locked'){
echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
echo "&nbsp;", "&nbsp;";
echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);

echo "&nbsp;", "&nbsp;";

}

else if ($row['sort'] == "audio"){
echo "<b>Update: </b>", $row['tag'], "<br>";
echo "<audio controls>";
echo "<source src='./uploads/".$row['message']."'>";
echo "</audio>", "<br>";
echo "<b>", "By: ", "</b>", ($row['username']), "<br>";
echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";
if($row['locked'] !='locked'){
echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
echo "&nbsp;", "&nbsp;";
echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);
}

else if ($row['sort'] == "video"){
echo "<b>Update: </b>", $row['tag'], "<br>";
echo "<video controls>";
echo "<source src='./uploads/".$row['message']."'>";
echo "</video>", "<br>";
echo "<b>", "By: ", "</b>", ($row['username']), "<br>";
echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";
if($row['locked'] !='locked'){
echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
echo "&nbsp;", "&nbsp;";
echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);
}

else {
echo "<b>Update:</b><br>";
echo ($row['message']), "<br>";
echo "<b>", "By: ", "</b>", ($row['username']), "<br>";
echo "<b>", "At: ", "</b>", ($row['timestamp']), "<br>";
if($row['locked'] !='locked'){
echo "<a href ='edit.php?pid=".$row['id']."&login=true&edit=true' method='post' id ='form-id' data-ajax='false' '><b><span class = 'small_blue'>EDIT</span></b></a>";}
echo "&nbsp;", "&nbsp;";
echo "<span class = 'small_green'>", ($row['edited']), "</span>", "&nbsp;", "&nbsp;", ($row['locked']);
}

最佳答案

如果我没记错的话,你想在设置页面时检索帖子,否则随机化?

如果那是你想要的,你可以试试这个(假设页面是整数并且 id 是唯一的):

$id = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_NUMBER_INT);
$q = ($id) ? "SELECT * from posts WHERE id=$id" : "SELECT * from posts ORDER BY RAND() LIMIT 1";
$db->query($q);

然后重新随机化只链接到没有参数的页面:

echo "<a href='random.php'>Re - Randomize!</a><br>";

希望对您有所帮助。

亲切的问候。

关于php - 随机帖子的动态链接与帖子 ID 不符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27659688/

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