Display row number continuesly when using pagination

Hi,

I wonder how to display row numbers…from 1 to…whatever.

When I’m not using pagination I just used this:



$count = 0;
while($author = mysql_fetch_array($authors)){
    $id = $author['id'];
    $name = $author['name'];

$count = ++$count;

echo $count;

}


But with pagination the number for the next page starts from 1 again instead of continuing.

So I wonder how to do this.

Thanks

Bjorn

put the $count variable in the query string of the paginated links. Change your loop to function like the below:


$count = (is_numeric($_GET['count']) ? $_GET['count'] : 0);
while($author = mysql_fetch_assoc($authors)){
	$id = $author['id'];
	$name = $author['name'];
	$count++;
	echo $count;
}

So your link would be /sitepage.php?page=$page&count=$count

Thanks for your reply.

I did what you said and it worked partly. Since I have 4 links(first,previous,next, last) wasn’t sure where to put the &count=$count. I tried different combinations, but nothing really worked.

Maybe you can take a look at part of the pagination code and let me know where to put the &count=$count or if there is some other solution.

Thanks

Bjorn


// This shows the user what page they are on, and the total number of pages
echo " Page $pagenum of $last ";

// First we check if we are on page one. If we are then we don't need a link to the previous page
// or the first page so we do nothing. If we aren't then we generate links to the first page, and to
// the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo "  ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&count=$count'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}


put the count on all urls that are used in the pagination. Try the below:


// This shows the user what page they are on, and the total number of pages

echo " Page $pagenum of $last ";
// First we check if we are on page one. If we are then we don't need a link to the previous page
// or the first page so we do nothing. If we aren't then we generate links to the first page, and to
// the previous page.
if ($pagenum != 1) {
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
	echo " ";
	$previous = $pagenum-1;
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous&count=$count'> <-Previous</a> ";
}
//just a spacer

echo "  ";
//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links

if ($pagenum != $last)
	$next = $pagenum+1;
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next&count=$count'>Next -></a> ";
	echo " ";
	echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last&count=$count'>Last ->></a> ";
}

Hi,

I did what you said, but it’s still not working. When I randomly click the links.
it doesn’t show the correct numbers. Like if I jump from the first page to the last page. Then it displays the next page numbers instead of the last page number…and the same thing with when I go backwards…then it sometimes counts forwards. So I get all sorts of weird numbers.

Do you know how to solve this? I don’t know if you added something more in that code…I assumed it was only the links that you added to, correct?

Bjorn

I saw that you used != instead of == in two places. So I tried that. But that didn’t work either. It still mixes up the numbers.

Then I tried to copy the whole code incase I missed something, but that resulted in an error.

What did you change in that code except for the links and the above?

Bjorn

I did nothing but IT work at the office today, no programming at all :nono:, so I might have overdone this a little. :slight_smile:


<?

$limit = 10;
$page = $_GET['page'];//or somewhere

//add you're query string paramters here
//this adds the default query string paramaters into links 
$AddQuery = '&foo=bar&foo1=bar2';//CHANGE THIS

//get the total number of pages
// Sets what we want to pull from the database 
$query_count    = "SELECT count(*) FROM TABLE"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count   = mysql_query($query_count);
if(!$result_count){
	$error = mysql_error()."<br>$query_count";
} else {
	$totalrows = mysql_result($result_count,0);
	//get the total number of pages 
	//THIS IS WHAT YOU WERE LOOKING FOR
	//just divide the total rows by the limit
	$totalpages = $totalrows/$limit; 
}

//just divide the total rows by the limit
$totalpages = $totalrows/$limit; 

if(empty($page)){    // Checks if the $page variable is empty (not set) 
	$page = 1;      // If it is empty, we're on page 1 
} 

$limitvalue = $page * $limit - ($limit); 
// Ex: (2 * 25) - 25 = 25 <- data starts at 25 

//start pagination
if($page != 1){ 
	$pageprev = $page-1; 
	$PaginationLinks .= '<a href="'.$_SERVER['PHP_SELF'].'?page=$pageprev&'.$AddQuery.'">PREV</a>&nbsp;&nbsp;&nbsp;'; 
}else{ 
		$PaginationLinks .= 'PREV&nbsp; '; 
} 

$numofpages = $totalrows / $limit; 

for($i = 1; $i <= $numofpages; $i++){ 
	if($i == $page){ 
		$PaginationLinks .=$i.'&nbsp; '; 
	}else{ 
		$PaginationLinks .= ' <a href="'$_SERVER['PHP_SELF'].'?page=$i'.$AddQuery.'">$i</a>&nbsp;'; 
	} 
} 


if(($totalrows/$limit) != 0){ 
	if($i == $page){ 
		$PaginationLinks .= $i.'&nbsp;'; 
	}else{ 
		$PaginationLinks .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'&'.$AddQuery.'">$i</a>&nbsp;'; 
	} 
} 

if(($totalrows - ($limit * $page)) > 0){ 
	$pagenext = $page+1; 
	$PaginationLinks .= '&nbsp;&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$pagenext.'&'.$AddQuery.'>NEXT</a>' ; 
}else{ 

	$PaginationLinks .='&nbsp;&nbsp;&nbsp;NEXT&nbsp;'; 
}
//end pagination


//now get the page data
$query_count    = "SELECT ROWS FROM TABLE LIMIT $limitvalue, $limit"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count   = mysql_query($query_count);
if(!$result_count){
	$error = mysql_error()."<br>$query_count";
} 
//start page

echo ' Page $pagenum of $last ';
echo $PaginationLinks;
?>

Read through the comments carefully to understand what’s going on and, obviously, change the SQL. I also added the numbered pagination links. just comment out any line in the url/pagination block that uses the $i variable.

Wheeeeeewwww…man, I thought it was going to be easier than that.
Now my head is really mush…:)))
It’s going to take me a while to understand this.

I wish it was an easy solution like just add a field to the table that was incrementing in numbers. But I guess that would be a problem with sorting.
no easy solutions to things are there? :)))

Thanks for your hard work.

Bjorn

When you navigate to the next page, don’t you use an OFFSET in the mysql query? If so, you could add this value to $count.

Edit:

Think I missunderstod what you wanted to do

I’m just a beginner. I don’t know what OFFSET is.
How can this be used?

Bjorn

Hi again,

Here is the full code(at least the code that is important) for that page. Maybe you could fill in the blanks to make the pagination work with the correct counting of rows. You can use the table and field names that are in this page.

If there is anyway you can highlight or mark where every you add or fill in your code that would be great.

Thanks

Bjorn






<?php

//___________________________________


//PAGINATION START

if (!(isset($pagenum)))
{
$pagenum = 1;
}


//Here we count the number of results
//Edit $data to be your query
$data = mysql_query("SELECT * FROM joke") or die(mysql_error());
$rows = mysql_num_rows($data);


//This is the number of results displayed per page
$page_rows = 20;


//This tells us the page number of our last page
$last = ceil($rows/$page_rows);

//this makes sure the page number isn't below one, or more than our maximum pages
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}

//This sets the range to display in our query
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;

//_______________________________________________________




// GET RESULT SET AND DISPLAY ROWS


$jokes = @mysql_query("SELECT DISTINCT id, jokedate, joketitle, joketext FROM joke WHERE 1=1 ORDER BY joketitle $max");
   if (!$jokes){
   echo '</table>';
      exit('Error requesting jokes' . mysql_error());
   }


//WHILE LOOP

while($joke = mysql_fetch_array($jokes)){
    echo "<tr valign='top'>\
";
	$id = $joke['id'];
    $jokedate = $joke['jokedate'];
    $joketitle = $joke['joketitle'];
    $joketext = htmlspecialchars($joke['joketext']);


//DISPLAY ROWS

echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'>$count</div></td>";
echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'><a href=\\"joke3.php?id=$id\\">$joketitle </a></div></td>";
echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'>$joketext</div></td>\
";
echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'>$jokedate</div></td>\
";
echo "<td class='rightbottomborder' valign='middle'><div align='middle'><a href='editjoke3.php?id=$id'>Edit</a> | Delete <input type=\\"checkbox\\" name=\\"variable[]\\" value=\\"$id\\" /></div></td>\
";
   echo "</tr>\
";
}

//____________________________________________________

echo "<p><br>";

//DISPLAY PAGINATION

// This shows the user what page they are on, and the total number of pages
echo " Page $pagenum of $last ";

// First we check if we are on page one. If we are then we don't need a link to the previous page
// or the first page so we do nothing. If we aren't then we generate links to the first page, and to
// the previous page.
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$previous'> <-Previous</a> ";
}

//just a spacer
echo "  ";

//This does the same as above, only checking if we are on the last page, and then generating the Next and Last links
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?pagenum=$last'>Last ->></a> ";
}

//END PAGINATION


//______________________________________________





?>




Try this:

<?
$dbname = "mbafast_se";$dbuser = "root";$dbpw = "";$dbhost = "localhost";$conn_id = mysql_connect($dbhost, $dbuser, $dbpw);mysql_select_db ($dbname);

$limit    = 10;
$page     = isset($_GET['page']) ? $_GET['page'] : 1;//or somewhere
$AddQuery = '&foo=bar&foo1=bar2';//CHANGE THIS, this adds the default query string paramaters into links

//get the total number of pages
// Sets what we want to pull from the database 
$query_count    = "SELECT count(*) FROM joke"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count   = mysql_query($query_count);
if(!$result_count){
    $error = mysql_error()."<br>$query_count";
} else {
    $totalrows  = mysql_result($result_count,0);
    $totalpages = ceil($totalrows/$limit); //Total number of pages.
    //just divide the total rows by the limit
    $limitvalue = $page * $limit - ($limit); 
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25 
}

//start pagination
if($page != 1){ 
    $pageprev = $page-1; 
    $PaginationLinks = '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pageprev.'&'.$AddQuery.'">PREV</a>&nbsp;&nbsp;&nbsp;'; 
}else{ 
        $PaginationLinks = 'PREV&nbsp; '; 
} 

$numofpages = $totalrows / $limit; 

for($i = 1; $i <= $numofpages; $i++){ 
    if($i == $page){ 
        $PaginationLinks .=$i.'&nbsp; '; 
    }else{ 
        $PaginationLinks .= ' <a href="'.$_SERVER['PHP_SELF'].'?page='.$i.$AddQuery.'">'.$i.'</a>&nbsp;'; 
    } 
}


if(($totalrows/$limit) != 0){ 
    if($i == $page){ 
        $PaginationLinks .= $i.'&nbsp;'; 
    }else{ 
        $PaginationLinks .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'&'.$AddQuery.'">'.$i.'</a>&nbsp;'; 
    } 
} 

if(($totalrows - ($limit * $page)) > 0){ 
    $pagenext = $page+1; 
    $PaginationLinks .= '&nbsp;&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$pagenext.'&'.$AddQuery.'>NEXT</a>' ; 
}else{ 

    $PaginationLinks .='&nbsp;&nbsp;&nbsp;NEXT&nbsp;'; 
}
//end pagination


//now get the page data
$query_count  = "SELECT DISTINCT id, jokedate, joketitle, joketext FROM joke ORDER BY joketitle LIMIT $limitvalue, $limit"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count = mysql_query($query_count);
if(!$result_count){
    $error = mysql_error()."<br>$query_count";
} 

echo "<table>";
while($joke = mysql_fetch_array($jokes)){
  $id = $joke['id'];
  $jokedate = $joke['jokedate'];
  $joketitle = $joke['joketitle'];
  $joketext = htmlspecialchars($joke['joketext']);
  
  //DISPLAY ROWS
  echo "<tr valign='top'>\
";
  echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'>$count</div></td>";
  echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'><a href=\\"joke3.php?id=$id\\">$joketitle </a></div></td>";
  echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'>$joketext</div></td>\
";
  echo "<td height='20' class='leftbottomborder' valign='middle'><div align='left' id='coolmenub'>$jokedate</div></td>\
";  
  echo "<td class='rightbottomborder' valign='middle'><div align='middle'><a href='editjoke3.php?id=$id'>Edit</a> | Delete <input type=\\"checkbox\\" name=\\"variable[]\\" value=\\"$id\\" /></div></td>\
";
  echo "</tr>\
";
} 
echo "</table>";
echo "<p><br />";
//DISPLAY PAGINATION
// This shows the user what page they are on, total pages and the rownumbers displayed
$firstRowOnPage = $limitvalue + 1;
$lastRowOnPage  = ($limitvalue + $limit) > $totalrows ? $totalrows : ($limitvalue + $limit);
echo "<div>Page $page of $totalpages</div>";
echo "<div>Rows $firstRowOnPage-$lastRowOnPage of $totalrows</div>";
echo "<div>$PaginationLinks</div>";

…you have some errors in your table… A id must be unique.

A problem appears when you have a hole lot of pages. Maybe you would like a pagination-script that prints out the pagination like google?

I tried it and it finally worked. Thanks a lot!

I don’t understand much of the pagination code though. I’m too much of a beginner for that. There are some syntax and function things I haven’t seen.
At this point I just wanted it to work. I guess I learn the other stuff with time.

I would like to get help to ‘search’ on the page. I used to have a searching feature before I entered the pagination stuff. And after that the search engine stopped working.

I’ll get back to you on that.

If you are referring to the html table, I know I just cut and pasted part of the whole table, so it may not show the whole table.

You said something about the ‘id’. What were you referring to there?

Anyway. Thank you so much for your work on this. I appreciate it a LOT

Thanks

Bjorn

Hi again,

I was wondering if you could help with the search part of the page(s)

I’m going to show the code that I used to have before I started to add the pagination stuff.

I have 4 tables. Authors, Jokes, Categories and one lookup table called jokecategory. Just so you know the code below and the field names.

The pagination page that you helped me with displays the jokes.

Like I said, it used to work before the pagination, so I wonder if you could take a look at the two files below(search page and jokes page=the same page as the pagination is on). This is just part of the code not to mess it up too much

I think the search page is ok and that nothing needs to be change there. But it’s mainly the receiving “jokes” page that needs to be looked at. I only show the search page so you can see the values and stuff and get a better overview.

Let me know if you can do this and just add the the working code to the existing jokes page, where you added the pagination to.

Thanks

Bjorn

SEARCH PAGE



<form action="jokelist.php" method="post">
    By Author:<br />
    <select name="aid" size="1">
      <option selected value="">Any Author</option>
      <?php
while ($author = mysql_fetch_array($authors)){
   $aid = $author['id'];
   $aname = htmlspecialchars($author['name']);
   echo "<option value='$aid'>$aname</option>\
";
   }
?>
    </select>
    <br />
    By Category:<br />
    <select name="cid" size="1">
      <option selected value="">Any category</option>
      <?php
 while ($cat = mysql_fetch_array($cats)){
    $cid = $cat['id'];
    $cname = htmlspecialchars($cat['name']);
    echo "<option value='$cid'>$cname</option>\
";
 }
?>
    </select>
    <br />
    Containing text:<br />
    <input type="text" name="searchtext" />
    <br />
    <br />
    <input type="submit" value="Search" />
  </form>



DISPLAY PAGE


<?php

//THE BASIC SELECT STATEMENT

$select = 'SELECT DISTINCT id, joketext';
$from = ' FROM joke';
$where = '  WHERE 1=1';

$aid = $_POST['aid'];
if ($aid != ''){ //An author is selected
   $where .= " AND authorid='$aid'";
}

$cid = $_POST['cid'];
if ($cid != ''){//A category is selected.
   $from .= ', jokecategory';
   $where .= " AND id=jokeid AND categoryid='$cid'";
}

$searchtext = $_POST['searchtext'];
if ($searchtext != ''){//Some search text was specified.
   $where .= " AND joketext LIKE '%$searchtext%'";

}
?>


Hi Bjorn!

The ID attribute uniquely identifies an element in the document. This means there can only be one element that has one typical id-value. In your table you have some divs: id='coolmenub'. To fix this you can add a number to the end: coolmenub1, coolmenub2… Or, if you only have these to change the apperance using css you should change the attribute name to class and keep the value, like this: class='coolmenub'.

Do you get any kind of error messages? When and how does the error appear?
It seems to me that the pagination script shouldn’t interfere with your search script. And, can you show the part of the code where you set $authors and $cats and the mysql-querys?

Thanks for the info about changing to ‘class’ instead of ‘id’. Yes, it’s for CSS.
It’s always been working with id, so I never really gave it a thought.

There is no Error when I try to search. But instead of the search result coming up for the search, it just displays the whole result set like it does without searching.

Since it worked before I used pagination, something must have happened after that. I don’t know what though.

I wonder if it’s something in the SELECT, that isn’t right? I’m going to show you what the select looked like earlier and what I have right now. Since I added pagination I had to add things to the select and change it a bit.

I’ve always wondered what WHERE 1=1 means. Is that ‘TRUE’. Or what does it do?

This is what SELECT looked like before. This one is together with getting the search result. The variables are later used in getting the result set.



$select = 'SELECT DISTINCT id, joketext';
$from = ' FROM joke';
$where = '  WHERE 1=1';

This is the SELECT where you get the resultset on the same page.


$result = @mysql_query($select . $from . $where);

This is what I have now. I tried to add WHERE 1=1, but that didn’t work. Does it matter where you place the WHERE clause? Or do you even need it?
I place it after FROM, but that didn’t work.

Let me know what you think.

Thanks!

Bjorn


$query_count  = "SELECT DISTINCT id, jokedate, joketitle, joketext FROM  joke WHERE 1=1 ORDER BY joketitle LIMIT $limitvalue, $limit";

First and Last should relate to row numbers. If they do your page is not dynamic. In fact none of the four need relate to row numbers, but index the current page.

WHERE 1=1 selects everything, try this instead:

$searchtext = $_GET['search']; //Or $_POST['search'].
$query_count  = "SELECT DISTINCT id, joketext FROM joke WHERE joketext LIKE '&#37;$searchtext%' ORDER BY joketitle LIMIT $limitvalue, $limit";

$_GET[‘search’] holds the string the user is looking for. This string has to be sent with the pagination links. Use $AddQuery to do this.
Or, you could use POST if submitting a form, then you can store the string in a hidden field.

I tried what you suggested and it works to search a text string…but I also need to be able to search by category and author and they don’t work.
And also, the pagination links at the top shows all the pages and links, like it would be the full result set eventhough the search row may be 1 or 2 displaying on one page only . So there is some weird stuff going on with that too.

I haven’t changed this part in the pagination because I don’t know what to change it to. The pagination seems to work anyway. What should it be changed to? Do I need to change it?
$AddQuery = ‘&foo=bar&foo1=bar2’

I’ve copied the code for the part of the page that we’ve been talking about but included everything.

The first part seems to receive the posted search result, but the SELECT PART(below the pagination stuff)where it displays the data is where I don’t know what to do with.

Do you have any idea how to get both of these to go together?

Thanks

Bjorn




//RECEIVE THE POSTED SEARCH RESULTS

$select = 'SELECT DISTINCT id, jokedate, joketitle';
$from = ' FROM joke';
$where = ' WHERE 1=1';

$aid = $_POST['aid'];
if($aid != ''){ // AN AUTHOR IS SELECTED
  $where .= " AND authorid='$aid'";
}

$cid = $_POST['cid'];

if($cid != ''){//A CATEGORY IS SELECTED
   $from .= ', jokecategory';
   $where .= " AND id=jokeid AND categoryid='$cid'";
}

$searchtext = $_POST['searchtext'];
  if($searchtext != ''){//TEXT HAS BEEN SELECTED
     $where .= " AND joketext LIKE '%$searchtext%'";
  }


//__________________________________


//START PAGINATION

$limit    = 20;
$page     = isset($_GET['page']) ? $_GET['page'] : 1;//or somewhere
$AddQuery = '&foo=bar&foo1=bar2';//CHANGE THIS, this adds the default query string paramaters into links

//get the total number of pages
// Sets what we want to pull from the database
$query_count    = "SELECT count(*) FROM joke"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count   = mysql_query($query_count);
if(!$result_count){
    $error = mysql_error()."<br>$query_count";
} else {
    $totalrows  = mysql_result($result_count,0);
    $totalpages = ceil($totalrows/$limit); //Total number of pages.
    //just divide the total rows by the limit
    $limitvalue = $page * $limit - ($limit);
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25
}

//start pagination
if($page != 1){
    $pageprev = $page-1;
    $PaginationLinks = '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pageprev.'&'.$AddQuery.'">PREV</a>&nbsp;&nbsp;&nbsp;';
}else{
        $PaginationLinks = 'PREV&nbsp; ';
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
    if($i == $page){
        $PaginationLinks .=$i.'&nbsp; ';
    }else{
        $PaginationLinks .= ' <a href="'.$_SERVER['PHP_SELF'].'?page='.$i.$AddQuery.'">'.$i.'</a>&nbsp;';
    }
}


if(($totalrows/$limit) != 0){
    if($i == $page){
        $PaginationLinks .= $i.'&nbsp;';
    }else{
        $PaginationLinks .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'&'.$AddQuery.'">'.$i.'</a>&nbsp;';
    }
}

if(($totalrows - ($limit * $page)) > 0){
    $pagenext = $page+1;
    $PaginationLinks .= '&nbsp;&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$pagenext.'&'.$AddQuery.'>NEXT</a>' ;
}else{

    $PaginationLinks .='&nbsp;&nbsp;&nbsp;NEXT&nbsp;';
}
//end pagination

//_____________________________________


//NOW GET THE PAGE DATA

$query_count  = "SELECT DISTINCT id, jokedate, joketitle, joketext FROM joke ORDER BY joketitle LIMIT $limitvalue, $limit"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count = mysql_query($query_count);
if(!$result_count){
    $error = mysql_error()."<br>$query_count";
}



Lets say you have a form that looks like this:

<form method="get" action="">
	<p>
		<label for="text">Search</label>
		<input type="text" name="text" />
		<label for="author">Author</label>
		<select name="author">
			<option value="">- - -</option>
			<option value="1">coolisdude</option>
			<option value="2">Dr.Flink</option>
			<option value="3">John Doe</option>
		</select>
		<label for="category">Category</label>
		<select name="category">
			<option value="">- - -</option>
			<option value="1">category 1</option>
			<option value="2">category 2</option>
			<option value="3">category 3</option>
		</select>
		<input type="submit" name="submitSearch" value="Search!" />
	</p>
</form>

And, table ‘joke’ has the following columns:

  • id
  • jokedate
  • joketitle
  • joketext (text)
  • author (int)
  • category (int)

Then, this should work (not tested):

<?php
$limit    = 5;
$page     = isset($_GET['page']) ? $_GET['page'] : 1;//or somewhere
//$AddQuery = '&amp;foo=bar&amp;foo1=bar2';//CHANGE THIS, this adds the default query string paramaters into links
$AddQuery = "";
if(isset($_GET['text'])) $AddQuery .= "&amp;text=" . urlencode("$_GET[text]");
if(isset($_GET['author'])) $AddQuery .= "&amp;author=$_GET[author]";
if(isset($_GET['category'])) $AddQuery .= "&amp;category=$_GET[category]";

//get the total number of pages
// Sets what we want to pull from the database
$where = "";
if(isset($_GET['text']) && !empty($_GET['text'])) $where .= " AND joketext LIKE '%$_GET[text]%'";
if(isset($_GET['author']) && !empty($_GET['author'])) $where .= " AND author='$_GET[author]'";
if(isset($_GET['category']) && !empty($_GET['category'])) $where .= " AND category='$_GET[category]'";
$query_count    = "SELECT count(*) FROM joke WHERE 1 $where"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count   = mysql_query($query_count);
if(!$result_count){
    $error = mysql_error()."<br>$query_count";
} else {
    $totalrows  = mysql_result($result_count,0);
    $totalpages = ceil($totalrows/$limit); //Total number of pages.
    //just divide the total rows by the limit
    $limitvalue = $page * $limit - ($limit);
    // Ex: (2 * 25) - 25 = 25 <- data starts at 25
}

//start pagination
if($page != 1){
    $pageprev = $page-1;
    $PaginationLinks = '<a href="'.$_SERVER['PHP_SELF'].'?page='.$pageprev.'&amp;'.$AddQuery.'">PREV</a>&nbsp;&nbsp;&nbsp;';
}else{
        $PaginationLinks = 'PREV&nbsp; ';
}

$numofpages = $totalrows / $limit;

for($i = 1; $i <= $numofpages; $i++){
    if($i == $page){
        $PaginationLinks .=$i.'&nbsp; ';
    }else{
        $PaginationLinks .= ' <a href="'.$_SERVER['PHP_SELF'].'?page='.$i.$AddQuery.'">'.$i.'</a>&nbsp;';
    }
}


if(($totalrows/$limit) != 0){
    if($i == $page){
        $PaginationLinks .= $i.'&nbsp;';
    }else{
        $PaginationLinks .= '<a href="'.$_SERVER['PHP_SELF'].'?page='.$i.$AddQuery.'">'.$i.'</a>&nbsp;';
    }
}

if(($totalrows - ($limit * $page)) > 0){
    $pagenext = $page+1;
    $PaginationLinks .= '&nbsp;&nbsp;&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?page='.$pagenext.$AddQuery.'>NEXT</a>' ;
}else{

    $PaginationLinks .='&nbsp;&nbsp;&nbsp;NEXT&nbsp;';
}
//end pagination


//now get the page data
$query_count  = "SELECT DISTINCT id, jokedate, joketitle, joketext FROM joke WHERE 1 $where ORDER BY joketitle LIMIT $limitvalue, $limit"; //CHANGE THIS TO YOUR ARRAY OF STUFF
$result_count = mysql_query($query_count);
if(!$result_count){
    $error = mysql_error()."<br>$query_count";
}

$count = isset($_GET['page']) && $_GET['page'] > 1 ? $limitvalue : 0;
echo "<table>";
while($joke = mysql_fetch_array($result_count)){
  $id        = $joke['id'];
  $jokedate  = $joke['jokedate'];
  $joketitle = $joke['joketitle'];
  $joketext  = htmlspecialchars($joke['joketext']);
  $count++;

  echo "<tr valign='top'>\
";
  //DISPLAY ROWS
  echo "<td class='leftbottomborder' valign='middle'><div class='coolmenub'>$count</div></td>";
  echo "<td class='leftbottomborder' valign='middle'><div class='coolmenub'><a href=\\"joke3.php?id=$id\\">$joketitle </a></div></td>";
  echo "<td class='leftbottomborder' valign='middle'><div class='coolmenub'>$joketext</div></td>\
";
  echo "<td class='leftbottomborder' valign='middle'><div class='coolmenub'>$jokedate</div></td>\
";
  echo "<td class='rightbottomborder' valign='middle'><div><a href='editjoke3.php?id=$id'>Edit</a> | Delete <input type=\\"checkbox\\" name=\\"variable[]\\" value=\\"$id\\" /></div></td>\
";
  echo "</tr>\
";
}
echo "</table>";

//DISPLAY PAGINATION
// This shows the user what page they are on, total pages and the rownumbers displayed
$firstRowOnPage = $limitvalue + 1;
$lastRowOnPage  = ($limitvalue + $limit) > $totalrows ? $totalrows : ($limitvalue + $limit);
echo "<p>";
echo "<div>Page $page of $totalpages</div>";
echo "<div>Rows $firstRowOnPage-$lastRowOnPage of $totalrows</div>";
echo "<div>$PaginationLinks</div>";
echo "</p>";
?>