Hey everyone. So what i am looking to do is achieve a cleaner url for my pagination. Currently I am passing variables through GET, and reloading the same page and modifying the query.
I am looking to do something like: http://designspiration.net/
Their pages show up as folders with the corresponding numbers. I am assuming they are doing this on the fly, but i am not sure how. Not particularly sure what I may be getting into, or what may be involved, but I am up for a challenge so let me know!
Here is the current test I have set up which uses $_GET. Already had some help with some online tutorials to get the theory down for the pagination.
<?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>";
?>
My assumptions: I will be POST’ing the variable for my pagination/mysql_query to a new folder and index.php which I create on the fly based on my page number.. I also looked into a .htaccess rewrite rule which can modify the url, however I am a beginner with php, trying to learn, and this seems like a shortcut.
Post code, or just point me in the correct direction! Not having any luck through searches.
Hey, what you are looking for is routing techniques, you could simply see how some frameworks do it, and implement it yourself, or just use the framework.
The basic premise is to re-route all trafic to a specific file ( generally index.php) in that file parse the URI and separate what you want, then route to the appropriate code.
re routing the request to the url is done via apache mod-rewrite, and then the parsing is up to you.
See this documentation to see how zend does it.
good luck
edited to add this link that seems to be more readable