gpt4 book ai didi

PHP:更清晰的分页 url(文件夹)

转载 作者:行者123 更新时间:2023-11-29 03:13:05 24 4
gpt4 key购买 nike

大家好。所以我想要做的是为我的分页实现一个更清晰的 url。目前我正在通过 GET 传递变量,并重新加载同一页面并修改查询。

我想做类似的事情:http://designspiration.net/

他们的页面显示为具有相应编号的文件夹。我假设他们正在即时执行此操作,但我不确定如何操作。不是特别确定我可能会进入什么,或者可能涉及什么,但我准备迎接挑战,所以让我知道!

这是我设置的当前测试,它使用 $_GET。已经通过一些在线教程获得了一些帮助,以了解分页的理论。

<?php
$host = "localhost";
$username = "user";
$password = "pass";
$db_name = "database";
$directory = "/newProject";

mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$postsPerPage = 2;
$start = $_GET['start'];
$totalRecords = mysql_num_rows(mysql_query("SELECT * FROM posts"));

if (!$start)
$start = 0;

$result = mysql_query("SELECT * FROM posts ORDER BY id DESC LIMIT $start, $postsPerPage");

while ($post = mysql_fetch_assoc($result)){
echo "<span>" ."Title: " .$post['title'] ."</span><br />\n";
echo "<span>" ."Author: " .$post['author'] ."</span><br />\n";
echo "<span>" ."Description: " .$post['description'] ."</span><br />\n";
echo "<a href='full_post.php?id=" .$post['id'] ."'>\n" ."<img src='" .$directory .$post['thumbUrl'] ."' /></a>" ."<br />\n";
echo "<span>" ."Type: " .$post['type'] ."</span><br />\n";
echo "<span>" ."Tags: " .$post['tags'] ."</span><br />\n";
echo "<span>" ."Submitted: " .$post['date'] ."</span><br />\n";
echo "<span>" ."ID: " .$post['id'] ."</span><br /><br /><br />\n";
}

$prev = $start - $postsPerPage;
$next = $start + $postsPerPage;

//previous button
if (!($start<=0))
echo "<a href='index.php?start=$prev'>Prev</a> ";

//page numbers
$pageNumbers = 1;

for($x=0;$x<$totalRecords;$x=$x+$postsPerPage){
if ($start!=$x)
echo "<a href='index.php?start=$x'> $pageNumbers</a> ";
else
echo "<a href='index.php?start=$x'><b> $pageNumbers</b></a> ";
$pageNumbers++;
}

//next button
if(!($start>=$totalRecords-$postsPerPage))
echo "<a href='index.php?start=$next'>Next</a>";

?>

我的假设:我将把我的 pagination/mysql_query 的变量发布到一个新文件夹和我根据页码动态创建的 index.php。我还研究了一个 .htaccess 重写规则,它可以修改url,但是我是php的初学者,正在尝试学习,这似乎是一个捷径。

发布代码,或者只是指出正确的方向!通过搜索没有任何运气。

最佳答案

你好,你找的是路由技术,你可以简单看看一些框架是怎么做的,然后自己实现,或者直接使用框架。基本前提是将所有流量重新路由到特定文件(通常是 index.php),在该文件中解析 URI 并分离您想要的内容,然后路由到适当的代码。

将请求重新路由到 url 是通过 apache mod-rewrite 完成的,然后解析由您决定。

参见 this文档以了解 zend 是如何做到的。

祝你好运

编辑添加this看起来更具可读性的链接

关于PHP:更清晰的分页 url(文件夹),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5166281/

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