Jump to content

[SOLVED] Page Pagination is stuffed - NEXT goes to PREV and PREV goes to NEXT etc..


Recommended Posts

Evening Everyone,

 

I have integrated the Pagination script from one of the tutorials here, and it is working fine, except for the links are alittle muddled up. What it does, is limit the amount of results echo'd to the page from my database, that works fine, creates a new page for each list, and creates the number of the page at the bottom(thus the pagination). So it looks like this:

PREV 1 2 3 4 5 NEXT

But, the PREV link and NEXT link are reversed, PREV goes to the next page, and NEXT goes to the previous page. And the number '1' never seems to be a link no matter what page you are on.

 

So with my current script, what needs to be changed and modded to correct these problems?

 

pagination script:

<?php
if($vpage != 1){ 
        $pageprev = $vpage--;
        
        echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pageprev\">PREV</a> "); 
    }else{
        echo("PREV ");
    }

$numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $vpage){
            echo($i." ");
        }else{
            echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $vpage){
            echo($i." ");
        }else{
            echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $vpage)) > 0){
        $pagenext = $vpage++;
         
        echo("<a href=\"http://www.crikeygames.com.au/conflictingforces/index.php?page=attack&vpage=$pagenext\">NEXT</a> "); 
    }else{
        echo("NEXT "); 
    }
    
    mysql_free_result($result);
?>

 

Regards ACE

Sorry to be lazy at the moment, but maby If I give you a refined version of my own pagignation you can sort yours out:

 

      $cards_per_page = 10;
      if ($_GET['page'] > 0){
        $page_number = $_GET['page'];
      } else {
        $page_number = 1;
      }
      
      $cardsq = 'SELECT * FROM tblCards';
      $cards_data = mysql_query($cardsq) or die(mysql_error());
      $pages = ceil(mysql_num_rows($cards_data) / $cards_per_page);
      if ($page_number > $pages){
        $page_number = $pages;
      }
      $p_start = ($page_number * $cards_per_page) - $cards_per_page;
      
      $cardsq = 'SELECT * FROM tblCards LIMIT '. $p_start .', '. $cards_per_page;
      
      $cards_data = mysql_query($cardsq) or die(mysql_error() .' Query:<br />'. $cardsq );
      
      if ($pages > 1){
        echo '<p>Page:<br />';
        for($i = 1; $i <= $pages; $i++){
          if ($page_number != $i){
            echo ' <a href="?action=lookup_card&page='. (string)$i .'">'. (string)$i .'</a>';
          } else {
            echo ' '. (string)$i;
          }
          echo '</p>';
        }
      }
      
      while($card = mysql_fetch_assoc($cards_data)){
        //do something for each record that is on this page
      }
      
      if ($pages > 1){
        echo '<p>Page:<br />';
        for($i = 1; $i <= $pages; $i++){
          if ($page_number != $i){
            echo ' <a href="?action=lookup_card&page='. (string)$i .'">'. (string)$i .'</a>';
          } else {
            echo ' '. (string)$i;
          }
        }
        echo '</p>';
      };

Hmm, I didn't like that pagination script, kinda crapped my script when I used it before. Try this one, simpler, and a bit better:

 

<?php

// Database Connection
include 'db.php';

// If current page number, use it
// if not, set one!

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 10;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results); 

// Perform MySQL query on only the current page number's results

$sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
    echo $row['title']."<br />";
}

// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM pages"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);

// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";
?>

 

It basically auto-defines those links for you. The following pieces need to be changed, and that's it:

 

// Database Connection
include 'db.php';

// Define the number of results per page
$max_results = 10;

// Perform MySQL query on only the current page number's results
$sql = mysql_query("SELECT * FROM pages LIMIT $from, $max_results");
//Keep the LIMIT $from, $max_results in the end, it's needed to limit number of results per page

while($row = mysql_fetch_array($sql)){
    // Build your formatted results here.
    echo $row['title']."<br />";
}

 

http://www.phpfreaks.com/tutorials/73/1.php

 

Why do I prefer this? I can keep it in a class, so I can run it with a given query, results per page, and formatted results, and it will run through it all :-) It's nice. Let me know if this one works off the bat.

I know you are just trying to fix your code, but I was messing around with a mixture of codes and ended up with this:

 

<?php
$base_url = $_SERVER['PHP_SELF']; // this is just to get the page your on
$num_items = 121; // this is the total amount of results
$per_page = 5; // this is obviously the amount of results you want on each page
$start_item = (empty($_GET['start'])) ? 1 : $_GET['start']; // this is the get to get where to start
function pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE)
{

$begin_end = 3; // this is how many numbers will be placed at the beggining ie 1,2,3 ... 8,9,10, if 5 then 1,2,3,4,5 .... 11,12,13,14,15 etc
$from_middle = 1; // how many results wil be displayed in middle ie for 1 ... 7,8,9 ... if 3 then 7,8,9,10,11,12,13

$total_pages = ceil($num_items/$per_page);

if ( $total_pages == 1 )
{
	return '';
}

$on_page = floor($start_item / $per_page) + 1;

$page_string = '';
if ( $total_pages > ((2*($begin_end + $from_middle)) + 2) )
{
	$init_page_max = ( $total_pages > $begin_end ) ? $begin_end : $total_pages;

	for($i = 1; $i < $init_page_max + 1; $i++)
	{
		$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' .$base_url . "?start=" . ( ( $i - 1 ) * $per_page )  . '">' . $i . '</a>';
		if ( $i <  $init_page_max )
		{
			$page_string .= ", ";
		}
	}

	if ( $total_pages > $begin_end )
	{
		if ( $on_page > 1  && $on_page < $total_pages )
		{
			$page_string .= ( $on_page > ($begin_end + $from_middle + 1) ) ? ' ... ' : ', ';

			$init_page_min = ( $on_page > ($begin_end + $from_middle) ) ? $on_page : ($begin_end + $from_middle + 1);
			$init_page_max = ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? $on_page : $total_pages - ($begin_end + $from_middle);

			for($i = $init_page_min - $from_middle; $i < $init_page_max + ($from_middle + 1); $i++)
			{
				$page_string .= ($i == $on_page) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "?start=" . ( ( $i - 1 ) * $per_page )  . '">' . $i . '</a>';
				if ( $i <  $init_page_max + $from_middle )
				{
					$page_string .= ', ';
				}
			}

			$page_string .= ( $on_page < $total_pages - ($begin_end + $from_middle) ) ? ' ... ' : ', ';
		}
		else
		{
			$page_string .= ' ... ';
		}

		for($i = $total_pages - ($begin_end - 1); $i < $total_pages + 1; $i++)
		{
			$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>'  : '<a href="' . $base_url . "?start=" . ( ( $i - 1 ) * $per_page )  . '">' . $i . '</a>';
			if( $i <  $total_pages )
			{
				$page_string .= ", ";
			}
		}
	}
}
else
{
	for($i = 1; $i < $total_pages + 1; $i++)
	{
		$page_string .= ( $i == $on_page ) ? '<b>' . $i . '</b>' : '<a href="' . $base_url . "?start=" . ( ( $i - 1 ) * $per_page )  . '">' . $i . '</a>';
		if ( $i <  $total_pages )
		{
			$page_string .= ', ';
		}
	}
}

if ( $add_prevnext_text )
{
	if ( $on_page > 1 )
	{
		$page_string = ' <a href="' . $base_url . "?start=" . ( ( $on_page - 2 ) * $per_page )  . '">Previous</a>  ' . $page_string;
	}

	if ( $on_page < $total_pages )
	{
		$page_string .= '  <a href="' . $base_url . "?start=" . ( $on_page * $per_page )  . '">Next</a>';
	}

}

$page_string = 'Go To ' . $page_string;

return $page_string;
}
echo pagination($base_url, $num_items, $per_page, $start_item, $add_prevnext_text = TRUE); // set to false if you dont want next and prev link
?>

 

I haven't found any problems with it yet :D So I thought I would throw this out there as it is nearly impossible to find a good pagination script.

 

Well I hope someone finds it useful ;D

 

~ Chocopi

change:

$pageprev = $vpage--; to $pageprev = $vpage-1;

and

$pagenext = $vpage++; to $pagenext = $vpage+1;

 

Thats done the trick  ;D , thank you so much  :) , I can't believe it was that simple lol.

 

Also thanks to everyone else. We've got a nice variety of Pagination scripts here now lol.  :D

 

Regards ACE

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.