Paged Results - PHP

I hope someone can help me I’m really struggling with some code to return paged results in PHP.

I am trying to return 1 page per result, at the moment I have got the code to return the results and show each result on an individual page.

My problem occurs when I try to view the next page. If I click the link, it will refresh result1.php and therefore should show the next result. Instead it seems to run the query again and shows the same result as page one.

Any ideas? Here is the code to have a look at:


/* you could put these following variables in a seperate file and include them on the page. That's how I normally do it, I only changed it to look like this for the example */

$server = "localhost";
$username = "user";
$password = "password";
$database = "jobclever";
$table = "job";

// Include Functions

// Connect To MySQL Server
@mysql_connect($server, $username, $password) or die("Couldn't Connect to Database");

// Select Database
@mysql_select_db($database) or die("Couldn't Select Database");

// set number of results to display per page
$pagelimit = "1";

// run query (change yourtable to the name of your table)
   $strSQL = mysql_query("SELECT * FROM $table");

// count number of matches
   $totalrows = mysql_num_rows($strSQL);

// determine how many pages there will be by using ceil() and dividing total rows by pagelimit
   $pagenums = ceil ($totalrows/$pagelimit);

// if no value for page, page = 1
    if ($page==''){
// create a start value
    $start = ($page-1) * $pagelimit;

// blank matches found
echo "<b>" . $totalrows . " matches found</b><br>

// Showing Results 1 to 1 (or if you're page limit were 5) 1 to 5, etc.
$starting_no = $start + 1;

if ($totalrows - $start < $pagelimit) {
   $end_count = $totalrows;
} elseif ($totalrows - $start >= $pagelimit) {
   $end_count = $start + $pagelimit;

echo "Results $starting_no to $end_count shown.<br>

// create dynamic next, previous, and page links

/* lets say you're set to show 5 results per page and your script comes out with 7 results.
this will allow your script to say next2 if you're on the first page and previous5 if you're on the second page. */

if ($totalrows - $end_count > $pagelimit) {
   $var2 = $pagelimit;
} elseif ($totalrows - $end_count <= $pagelimit) {
   $var2 = $totalrows - $end_count;

$space = "&nbsp;";

// previous link (make sure to change yourpage.php to the name of your page)
if ($page>1) {
        echo "« <a href='result1.php?page=".($page-1)."' class=main>Previous" . $space . $pagelimit . "</a>" . $space . $space . "";

// dynamic page number links (make sure to change yourpage.php to the name of your page)

    for ($i=1; $i<=$pagenums; $i++) {
        if ($i!=$page) {
            echo " <a href='result1.php?page=$i' class=main>$i</a>";
        else {
            echo " <b class='red'>$i</b>";

// next link (make sure to change yourpage.php to the name of your page)

    if ($page<$pagenums) {
        echo "" . $space . $space . $space . $space . " <a href='result1.php?page=".($page+1)."' class=main>Next " . $var2 . "</a> »";

/* output your data wherever you'd like.


in order for this all to work, before outputting your data, you have to run the query over using MySQL's LIMIT. This will limit how many results are actually displayed on the page. */

   $strSQL = mysql_query("SELECT * FROM $table LIMIT $start,$pagelimit");

// LIMIT 0,10 will start at 0 and display 10 results
// LIMIT 10,5 will start at 10 and display 5 results

/* now you can do whatever you'd like with this query. it will only output one page at a time. change the $pagelimit variable to whatever to output more than 1 result per page. */

$sql = "SELECT jobid, date, title, description, location, salary, industry FROM $table LIMIT $start,$pagelimit";
$sql_result = mysql_query($sql) or die('SQL Error');
$num_rows = mysql_num_rows($sql_result);

while ($row = mysql_fetch_array($sql_result)){
	$jobid = $row["jobid"];
	$date= $row["date"];
	$title = $row["title"];
	$desc = $row["description"];
	$location = $row["location"];
	$salary = $row["salary"];
	$industry = $row["industry"];
	print "<table><tr><td>Job ID: $jobid</td></tr>";
	print "<tr><td>Date:";
	echo fixDate($date); 
	Print "</td></tr>";
	print "<tr><td>Title: $title</td></tr>";
	print "<tr><td>Description: $desc</td></tr>";
	print "<tr><td>Location: $location</td></tr>";
	print "<tr><td>Salary: $salary</td></tr>";
	print "<tr><td>Industry: $industry</td></tr></table>";
	print "To apply please click <a href=result_details.php?jobid=$jobid>here</a>";
	print "<p>";


nope… that wasn’t it… lol

I’m thinking… :bounce:

try adding

$page = $_GET['page'];

to the top of your script. The problem might be that the script is not receiving the page variable from the URL string. In that case the script I gave you up there would fix that. And then it would read thru correctly…


You star, I thought no one had an answer.

I set about writing about another 20 lines of code to try and overcome this problem and thats all I’d forgot!!!

Shows you can look at your own code too closely sometimes.

that fixed it? :slight_smile:

Yeah :thumb:

awesome! :slight_smile:

Its always something simple. Overanalyzing your scripts tends to make you skip over the small things. :slight_smile:


I’m sooooo pleases that is sorted out, I’ve spent a good day looking at the script, going away and then coming back to it with no avail. :wink: :stunned: :wink:

Just let me know if you ever need any help with anything. You see to have the hang of it pretty well. How long you been at PHP?

Oh, also, to make your database connections easier, I suggest you looking using includes. Its easier to type up


instead of your entire connection code each time. If you need some help with that let me know. :slight_smile:

Thanks for pointing out the connection, I’ve already done that as my next job - just tidying the code as we speak.

I’ve only really coded PHP on a few jobs, I normally work with ASP but I’m still struggling with the syntax in PHP.

I’m currently working on a job site in PHP so may post a few more questions on here in the coming weeks. I had forgotten about this forum until recently, when I was doing alot of work with Flash + AS I found it invaluable given there are alot of people who ‘know’ what they are talking about rather than trying to flannel you.

Yeah, I have met a few people now that normally use ASP but have recently dipped into PHP. I check the forums once or twice a day when I’m working so if you run into any trouble post away. :slight_smile:

Arrghhh celebrated a bit too soon.

I’ve just been trying to link the code to a search page with three variables being passed $title, $sector, $location

I was hoping to use this code snippet but I am struggling:

if($title!= “”)
$sql = “SELECT jobid, date, title, description, location, industry, salary FROM job WHERE title = ‘$jobid’ LIMIT $start,$pagelimit”;

if($sector!= “”)
$sql_where .= " AND industry = ‘$sector’";

if($location!= “”)
$sql_where .= " AND location = ‘$location’";

$sql = $sql.$sql_where;

This code would replace the last SQL statement in the original code posted above.

At the moment the count is running and returning a number for all the records in the db and is not executing the search. :sigh:

Any ideas?

Anyone got any more ideas on this?

I am really struggling at the moment with editing the code to search via specific variables instead of returning the full recordset from mySQL

I’l take a look today. I got your e-mail but I have been busy with work. I’ll let you know if I see anything…


That is appreciated, I feel as though I have been banging my head against a brick wall.

All I want to do now, is build in some search functionality but am really struggling.

if($title!= "") 
       $sql = "SELECT jobid, date, title, description, location, industry, salary FROM job WHERE title='".$jobid."'"
if($sector!= "") 
       $sql_where .= " AND industry = '$sector'"; 

if($location!= "")
        $sql_where .= " AND location = '$location'"; 

$limit_sql = " LIMIT ".$start.",".$pagelimit; 

$sql = $sql.$sql_where.$limit_sql; 

I dunno, something like that might work…

Ok I’ll give that a go when I get home in about 3 hours.

Cheers for taking the time to look at it again, if you ever need any help with ASP just shout.