Pagination not working

Hey folks,
so I added a rating system to my php generated table and decided to build upon the rating system structure to make the table sortable and paginated.
The sortable part worked easily but I am stuck with the pagination part.

This is the part of the function which deals with pagination and all of that gets posted on index.php

if(!isset( $_GET['p']) ){$_GET['p'] =0;} 	
   $per_page=  5;
   $sqlzine= "SELECT * FROM `banner_clicks` WHERE `active` = 1" ;
   $page= mysql_num_rows ( $this->objDb->query($sqlzine));
    $pages= ceil($page / $per_page);
   	$sql = "SELECT * FROM `banner_clicks` WHERE `active` = 1 ORDER BY ".$order_var." LIMIT ".$_GET['p'].",". $per_page or die ('sql2 not working');
				
		$statement = $this->objDb->query($sql);
		return $statement->fetchAll(PDO::FETCH_ASSOC);
	
	}
	

On the index.php file I use a for loop right after a foreach loop, displaying the database entries, to display the numbers at the bottom of the table.

for ( $i=0; $i<$pages; $i++){
   echo ($i == $_GET['p'] / $per_page) ? '<strong> <a href="index.php?p=' . ($i * $per_page) . '&order_var='.$order_var.'">'.($i +1). '</a></strong>' :
   '   <a href="index.php?p=' . ($i * $per_page) . '&order_var='.$order_var.'">'.($i +1). '</a>';

}

When I type in the current code basically nothing changes. The table entries can still be sorted and rated on but there is not pagination. If I change the url to ?p=5 for example then the pagination works appropriately.
So the code works but the for loop is not being executed correctly. The problem might have to do with the fact that not all variables are defined on the index.php page but I tried adding some definitions to make sure that all variables in the for loop are defined on the same page, but it didnt change anything.

Just one last note: the pagination code was working perfectly when I did not use the rating system and had the sql_query and the for loop echoing the page numbers all on the same .php page.

Thank you for your help. I really wish I was better at this stuff :frowning:
Shibbs

Ok so I inserted a few or die messages and I now am pretty sure what the problem is

$page= mysql_num_rows ( mysql_query ($sqlzine)) or die ('sqlzine not working');

This line is not working therefore it wont show the pagination numbers. Anyone have any idea what I have to change to make this first query work?

Solved.

The questions I fear most on this forum are those about pagination, I’ll admit it.

Pagination questions seem to epitomise the LAMP/web challenge, and represent one of the pinnacles of that mixture of languages/markup that can take forever to debug.

There are results totals coming from SQL using PHP which is doing some math, and maybe accessing SESSION vars and then displaying HTML. There are HTML query string links generating more constructed by PHP SQL strings and the opportunity of some dodgy comparison code forks. Sometimes the pagination is clearly coming from some unknown framework, or is being output as template code and to top it all, all that gets posted are a handful of lines.

We are really glad you fixed your problem and that you took time to come back and tell us, but for others who may come across this thread and if you can remember them, what were the logical steps you took to find out just where your pagination was going wrong?

Hello Cups,

It turned out that it was not so much a challenge about pagination but about understanding PDO. This was the first time I was confronted with PDO so it took me a bit to get used to it. But after a while it all made sense.
I feel as though pagination itself is simple but once you combine it with other things it can become tricky.

I myself had problems with creating a table which is sortable and paginateable at the same time.
Luckily I solved that with the help of StarLion. If anyone is interested here is the link.

If anyone reading this has any questions about the code feel free to pm me.

Shibbs

Oh boy, you have just underlined my point about fear of pagination questions so well.

Thanks for replying!