Pagination Pages Issue


I have created a new wordpress template page that reads in the results from my custom table (5 at a time).

I have done something wrong as I am getting the correct # of pages as href’s at bottom of page (3 page links), but when I click these page #'s, I get the same results on each page (the first 5 records).

My variable $this_page_first_result just isn’t working right.

Much appreciated if someone could review and let me know what I have done wrong.


get_header(); ?>

	<div id="primary" class="site-content">
		<div id="content" role="main">

		<?php get_template_part( 'content', 'page' ); ?>
		<?php comments_template( '', true ); ?>

		global $wpdb;

		$iResultsPerPage 	= 5;
		$sSql  		= "SELECT * FROM fruit"; 
		$iRows 		= $wpdb->get_results($sSql); 
		$iTotalRows	= $wpdb->num_rows;
		$iNumPages 	= ceil($iTotalRows / $iResultsPerPage);

		if (! isset($_SESSION['page']))
		      $_SESSION["page"] = "1";
		      $page = $_SESSION["page"];
		      $page = $_SESSION["page"];

		$this_page_first_result = ($page-1) * $iResultsPerPage;

		$sSql  		  = "SELECT * FROM fruit LIMIT " . $this_page_first_result . "," . $iResultsPerPage;
		$iRows 		  = $wpdb->get_results($sSql); 
		$iTotalRows	  = $wpdb->num_rows;

		foreach ($iRows as $row)
		     echo "$row->fruit_id "; 
		     echo "$row->name "; 
		     echo "$row->variety<br/>"; 

		echo "<br><br>";

		for ($page = 1; $page <= $iNumPages; $page++)
		    echo '<a href="http://localhost/mysite/test-pagination?page=' . 	$page . '">' . $page . '</a>   ';


		<br /><br />
 		</div><!-- #content -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
echo '<a href="http://localhost/mysite/test-pagination?page=' . $page . '">' . $page . '</a>   ';

You pass the “page number” through as part of the URL, that is it will come in as part of the $_GET array, but you look in a session variable to find it. I don’t see where you set that session variable (other than to set it to “1” if it doesn’t exist), so it will surely always bring up the first page unless you get the page from the appropriate array entry?


I tried using the href, however I’m getting a url of this:

Looks like the _GET isn’t working or my wp url isn’t working.

Got it working

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.