I messed around with the code for a while and finally figured it out. For anyone that has their code split into 2 pages (form page and form processor page), here's my solution for anyone else in the same boat:
Form processor page:
1) First I create the SQL search string.
2) Then I run the query to determine the total number of results returned
3) I then save the search string, the number of results per page, and the total number of results returned by the query. I save them in temp variables for the current script and also $_SESSION variables to save the values for the next script.
4) I then append to my SQL search string a LIMIT value and hardcode in the LIMIT values from 0 to the number of results per page, and then run the query
5) Count and print the rows.
6) This is where you start incorporating the pagination code. I hardcode in the value for page. I set it equal to 1.
7) Print out the results from the first section
8) If the total results exceeds the results per page, print out links at the bottom to point to another page.
This comes out to:
//Test query the listings db with the SQL search string that we've created
//just to get the total number of records found (for pagination)
$searchresults = mysql_query($searchstring);
if (!$searchresults)
{
$problemtext = 'Error searching listings db. \r\n '.mysql_error($connresult);
trigger_error($problemtext, E_USER_ERROR);
}
//count the number of rows in the result set
$totalnumrows = mysql_num_rows($searchresults);
//setting variables and SESSION variables to help with pagination
$_SESSION['sqlsearchstring'] = $searchstring;
$_SESSION['resultsperpage'] = 10;
$_SESSION['numberofrows'] = $totalnumrows;
$resultsperpage = 10;
$searchstring .= " limit 0,10";
//Query the listings db with the SQL search string that we've created
$searchresults = mysql_query($searchstring);
if (!$searchresults)
{
$problemtext = 'Error searching listings db. \r\n '.mysql_error($connresult);
trigger_error($problemtext, E_USER_ERROR);
}
//count the number of rows in the result set
$numrows = mysql_num_rows($searchresults);
//if there are no records that fit the search criteria
if ($numrows == 0)
{
echo '<table align="center" height="37"><tr><td><img src = "images/nosearchresults.jpg" alt="No Search Results" /></td></tr></table><br /><BR />';
}
//else records matched our search criteria, and a display preference other
//than "show all" was chosen
elseif (($displaypref == "10 results per page") || ($displaypref == "25 results per page"))
{
//Since this will be the first page in our result set, we
//set the $page variable equal to 1.
$page = 1;
//We want to display the current range of listings being shown.
//This helps us determine how many listings are being shown on the
//current page.
if ($totalnumrows <= $resultsperpage)
$endrange = $totalnumrows;
else
$endrange = $resultsperpage;
echo '<table align="center" height="37"><tr><td><img src = "images/searchresults.jpg" alt="Search Results" /></td></tr></table><br />';
echo '<div align="left"><B>   Listings 1-'.$endrange.' of '.$totalnumrows.'</B></div>';
//call this script which loops through and prints out the results
include('/blah/blah/dbresults.php');
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($totalnumrows / $resultsperpage);
//Build page number hyperlinks for pagination
echo '<br /><div align="center">Result Pages:<br />';
//Build Previous Link
if($page > 1)
{
$prev = ($page - 1);
echo '<a href="pageresults.php?page='.$prev.'"><<Previous</a> ';
}
//Building page number links
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
echo "<B> [$i] </B>";
else
echo '<a href="pageresults.php?page='.$i.'">'.$i.'</a> ';
}
// Build Next Link
if($page < $total_pages)
{
$next = ($page + 1);
echo '<a href="pageresults.php?page='.$next.'">Next>></a> ';
}
echo '</div>';
I then call the file "pageresults.php" and this handles the pagination, including if someone clicks on page 1 to go back to the first section of the result set. In this file I call "pageresults.php" with the appropriate value for the $page $GET variable
Second page (used for pagination):
session_start();
if ((!isset($_SESSION['sqlsearchstring'])) || (!isset($_SESSION['resultsperpage'])) || (!isset($_SESSION['numberofrows'])))
errorcheck(2, 'User attempted accessing "pageresults.php" without going through "searchlistings.php" or "pageresults.php".');
//to determine page #
if(!isset($_GET['page']))
$page = 1;
else
$page = $_GET['page'];
//Define the number of results per page. This value is stored in
//a SESSION variable set by the form processing page.
$max_results = $_SESSION['resultsperpage'];
//Figure out the limit for the query based
//on the current page number.
$from = (($page * $max_results) - $max_results);
//connecting to database to continue with pagination
include('/blah/blah/dbconnect.php');
//Define the SQL search string that we're going to run. This base
//value is stored in a SESSION variable set by the form processing
//page. We then concatenate parts to define the values for the
//LIMIT part of the SQL search string.
$searchstring = $_SESSION['sqlsearchstring']." limit ".$from.",".$max_results;
//Query the listings db with the SQL search string that we've created
$searchresults = mysql_query($searchstring);
if (!$searchresults)
{
$problemtext = 'Error searching listings db. \r\n '.mysql_error($connresult);
trigger_error($problemtext, E_USER_ERROR);
}
//Determines the beginning part of the range being shown
$beginningrange = $from + 1;
//We want to display the current range of listings being shown.
//This helps us determine how many listings are being shown on the
//current page.
if (($from + $max_results) < $_SESSION['numberofrows'])
$endrange = $from + $max_results;
else
$endrange = $_SESSION['numberofrows'];
//continue with displaying results
echo '<table align="center" height="37"><tr><td><img src = "images/searchresults.jpg" alt="Search Results" /></td></tr></table><br />';
echo '<div align="left"><B>   Listings '.$beginningrange.'-'.$endrange.' of '.$_SESSION['numberofrows'].'</B></div>';
//call this script which loops through and prints out the results
include('/blah/blah/dbresults.php');
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil(($_SESSION['numberofrows']) / $max_results);
//Build page number hyperlinks for pagination
echo '<br /><div align="center">Result Pages:<br />';
//Build Previous Link
if($page > 1)
{
$prev = ($page - 1);
echo '<a href="pageresults.php?page='.$prev.'"><< Previous</a> ';
}
//Building page number links
for($i = 1; $i <= $total_pages; $i++)
{
if(($page) == $i)
echo "<B> [$i] </B>";
else
echo '<a href="pageresults.php?page='.$i.'">'.$i.'</a> ';
}
// Build Next Link
if($page < $total_pages)
{
$next = ($page + 1);
echo '<a href="pageresults.php?page='.$next.'">Next >></a> ';
}
echo '</div>';
Hope this helps someone!