PHP:清洁分页网址(文件夹)

时间:2011-03-02 10:40:41

标签: php mysql url pagination

嘿大家好所以我要做的是为我的分页获得一个更清晰的网址。目前我通过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的变量POST到一个新文件夹和index.php,我根据我的页码动态创建它。我还查看了一个.htaccess重写规则可以修改网址,但我是一个初学者用PHP,试图学习,这似乎是一个捷径。

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

1 个答案:

答案 0 :(得分:1)

嘿,您正在寻找的是路由技术,您可以简单地看一些框架是如何做到的,并自己实现它,或者只是使用框架。 基本前提是将所有流量重新路由到该文件中的特定文件(通常为index.php),解析URI并分离您想要的内容,然后路由到相应的代码。

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

请参阅this文档,了解zend是如何做到的。

祝你好运

编辑以添加似乎更具可读性的this链接

相关问题