gpt4 book ai didi

php - 如何为单页显示网站创建上一个/下一个功能?

转载 作者:行者123 更新时间:2023-11-28 23:37:45 27 4
gpt4 key购买 nike

我想创建一个函数来在单页网站的标题上显示上一页-下一页按钮。

$query = "SELECT * FROM `issue` ORDER BY `issue_no` DESC LIMIT 1";

整个站点的内容来自数据库中名为“issue_no”的主键,issue_no 中的所有内容都是要在一页上显示的内容。

if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<article id='date_published'><p>Date published:&nbsp;" . $row['date_published'] . "</p></article>";
echo "<article id='article_head'>" . $row['article_head'] . "</article>";
echo "<article id='article_body'>" . $row['article_body'] . "</article>";
echo "<article id='vidpicks_embed'>" . $row['vidpick'] . "</article>";
echo "<article id='quote_body'>" . $row['quote_body'] . "&mdash;</article>";
echo "<article id='quote_auth'>" . $row['quote_auth'] . "</article>";
echo "<article id='wotd_body'>" . $row['wotd_body'] . "</article>";
echo "<article id='wotd_desc'>" . $row['wotd_desc'] . "</article>";
}
}

这显示在 index.php 上

目前,它显示的是发布日期未决的最新一期,但我想添加返回上一版和下一版的功能。

除此之外,我想在下拉选择菜单中推出所有问题,您可以选择“问题 23”,它会链接到该问题中包含的适当内容。

另外 - 我将如何确保每个都有干净的 URL?

例如http://www.example.com/issue/023

非常感谢任何帮助。

我已经尝试阅读有关分页的内容,但我发现围绕这个问题进行思考有点复杂。

谢谢。

最佳答案

你说:

On top of this, I'd like to rollout all issues inside a dropdown select menu that allows you to select say "Issue 23" and it would link you through to the appropriate content contained in that issue.

假设(例如)一个像“http://www.example.com/issue.php?issue=23”这样的 URL ”(现在推迟了‘干净’的 URL 问题)你可以用这种方式解决所有问题:

$issueId = $_GET['issue'];

/* 01: Retrieve ALL id */
$result = $con->query( "SELECT GROUP_CONCAT( `issue_no` ORDER BY `issue_no` DESC ) FROM `issue`" );
$allId = explode( ',', $result->fetch( PDO::FETCH_NUM )[0] );

/* 02: Check if $issueId exists and set previous/next id */
$prev = $next = False;
$found = array_search( $key, $allId );
if( $found === False ) $found = count( $allId )-1;
if( $found > 0 ) $prev = $allId[$found-1];
if( $found < count($allId)-1 ) $next = $allId[$found+1];

/* 03: Retrieve current issue: */
$result = $con->query( "SELECT * FROM `issue` WHERE `issue_no` = '{$allId[$found]}'" );
$row = $result->fetch_assoc();

/* 04: Output previous/next page link: */
if( $prev !== False ) echo "<a href=\"?issue=$prev\">Prev Page</a>";
else echo "<span class=\"inactive\">Prev Page</span>";
if( $next !== False ) echo "<a href=\"?issue=$next\">Next Page</a>";
else echo "<span class=\"inactive\">Next Page</span>";

// Your code from here

以上代码仅作为示例。通过这种方式,所有issue_no存储在 $allId变量,因此您也可以轻松实现下拉菜单issue_no字段按降序检索,如果您喜欢升序,可以更改 ORDER BY issue_no ASC 中的第一个查询 (#01) .

请注意,如果用户不询问任何特定问题(即调用 http://www.example.com/issue.php ),则当前问题将设置为 $allId 的第一个数组值(#02):如果你更喜欢产生像“这个问题不存在”这样的警报,你必须以这种方式修改脚本:

$found = array_search( $key, $allId );
if( $found === False )
{
// Your error routine here
}
else
{
if( $found > 0 ) $prev = $allId[$found-1];
(...)
}

在上一页/下一页 URL 的输出 (#04) 中,我使用基本的 <a href>标记,但您可以使用按钮。

干净的 URL

我强烈建议首先生成一个完整的工作代码,忽略“干净”的 URL 问题。然后,您只需更改一行即可。

顺便说一下,要激活干净的 url,您必须按如下方式修改站点的 .htaccess 文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^issue/.*$ /issue.php [NC,L]
</IfModule>

然后,在你的 issue.php脚本,你必须更改 $issueId = $_GET['issue'];与:

$issueId = array_pop( explode( '/', $_SERVER['REQUEST_URI'] ) );

这个 RewriteRule 只是一个例子,实际上我认为您对所有站点的干净 URL 感兴趣,所以最好的解决方案是将所有传入的 URL 重定向到 redirect.php处理 REQUEST_URI并包含适当的页面或回显“未找到”消息。

Here您可以在 RewriteRule

上找到更多关于基础知识的信息

关于php - 如何为单页显示网站创建上一个/下一个功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293855/

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