I would like to thank everyone who has helped me accomplish today's task of creating a working script that displays X number of database search results on Y number of pages (ie pg 1 2 3 4 5 ).
so, to further show my appreciation i will include the core programming here so that others who still may be having problems with theirs or are still wondering how to do it will wonder no more :p. As I am a true beginner and appreciate detailed explainations of how things work when I look at others programming, I have tried to include very thourough comments explaining how each section of the code works. I am using a MYSQL database, but it should port to any DB that supports the LIMIT syntax.
I'm also posting this here (and not as a snippet) because I am a beginner and I am sure there is a bug in it somewhere (although it works fine). This subject baffled me for weeks and I can't believe its this simple, so something has to be wrong with it =) again, this is the core, bare bones code so if you use it, of course you should include your own html to modify how the results are displayed. without further adieu, the code.
<?php
//connect to the server.
$connection = mysql_connect ($user, $root, $pass) or die ("couldn't connect to server");
//select the database you will be using.
mysql_select_db ($db) or die ("couldn't connect to database");
//query the database and select from your table so that the results can be counted.
$stmt = mysql_query ("select from $tablename");
//$limit is the number of results to be returned for each page
$limit=5;
//numrows is an integer showing the number of results in your $stmt query.
$numrows=mysql_num_rows($stmt);
echo ("There are $numrows results in the database. <br><br><br>");
//check to see if there is already a value for offset. if not, set this
//value to 0 (so when the page loads for the very first time, it has a
//starting point).
if (empty($offset) || $offset < 0)
{
$offset = 0;
}
//calculate the number of pages needed to display all the results
//this is done by taking the total number of rows from the query result ($numrows) and
//dividing them by the number of results you wish to return per page ($limit).
$NumOfPages = intval($numrows/$limit);
//this calculates the remainder of $numrows/$limit (if there is a remainder)
//and adds a page to the final number of pages to be displayed so that
//the remaining results can be displayed as well.
if ($numrows%$limit)
{
$NumOfPages++;
}
//this is another database query that pulls the information you want
//from your database. I am using a simple example here, but of course
//it can become quite complex. the most important aspect of the statement
//is "limit $offset,$limit". This tells the database to return results based
//on our $offset and $limit values. To understand the LIMIT syntax more, see
//the mysql documentation.
$sqlresult = mysql_query ("select $something from $db LIMIT $offset,$limit");
//here is where we actually calculate how many pages will be displayed.
//we need a first page regardless, so $pagenumber is set to 1. if there is
//more than one page that needs to be displayed in our results, then its calculated
//here and the number needed is displayed sequentially seperated by a space ($nbsp).
//here we give a new value to offset in each loop so that our previous mysql query
//($sqlresult) works properly.
for ($pagenumber = 1; $pagenumber <= $NumOfPages; ++$pagenumber)
{
$offset = $limit * ($pagenumber - 1);
echo ("<a href=\"$PHP_SELF?offset=$offset\">$pagenumber</a>   ");
}
echo ("<br>");
//this command puts your sql query results into an array and echos
//the results until there there is no more data to loop thru. the
//following code uses $field to represent some field in your database.
while ($row = mysql_fetch_array ($sqlresult))
{
$field = $row["$field"];
echo ("$field <br>");
}
?>
that's it! again, I can't believe the concept is so simple. please email me or post with bugs or errors. otherwise, enjoy!!