Jump to content

[SOLVED] Pagination


xoligy

Recommended Posts

Ok thought i just fixed this but seems i was also copying every post 3x lol

 

Basically everything is set up but here is the problem i want it to display the numbers 1,2,3...99, 100, 101 but without disrupting how many items per page are displayed so if i have 700posts i should have 10 pages and the pagination will look like 1, 2, 3...8, 9, 10 but how it is is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

 

anyhow here is the code:

<?
$query = mysql_query("SELECT * FROM $tab[forum_post] WHERE topic_id=$topicid ORDER BY post_id LIMIT $start, $itemperpage");

$this_page = mysql_num_rows($query) or die("No post records found!");



$paging = generate_pagination("viewtopic.php?topicid=$topicid", $all_record, $itemperpage, $start, $tru);
?>

Link to comment
Share on other sites

You need to post what's in generate_pagination for us to give you any kind of specific help.  But overall, you just need to find the loop that generates the numbers (I'm assuming it's in generate_pagination), and alter it.  Instead of just doing a generic

 

for ($x = 1;$x < $pages; $x++) { ... }

 

you could do several things.  One thing you could do is do two separate loops; one for the 1,2,3 and another for the 8,9,10.  Or you could make a condition inside your loop to only display the numbers you want, like

 

for ($x = 1;$x < $pages; $x++) { 
   if (($x < 4) && ($x > ($pages - 4))) {
      // display page numbers here
   }
}

 

 

Link to comment
Share on other sites

Well im back i did get the pagination working how i wanted by changing if ($total_pages > 10){ to 5 but now i have this display issue "Prev  1, 2, 3, , 4, 5, 6  Next" or this one "Prev  1, 2, 3 ... , 6, 7, 8  Next" (depends on how many pages there are.

 

function generate_pagination($base_url, $num_items, $per_page, $start_item, $tru, $add_prevnext_text = TRUE) {

$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 > 5 ) {

	$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;

	for($i = 1; $i < $init_page_max + 1; $i++) {

		$page_string .= ( $i == $on_page ) ? "<b>" . $i . "</b>" : "<a href=\"$base_url&tru=$tru&start=" . ( ( $i - 1 ) * $per_page )  . "\">" . $i . "</a>";

		if ( $i <  $init_page_max ) {

			$page_string .= ", ";

		}

	}

	if ( $total_pages > 3 ) {

		if ( $on_page > 1  && $on_page < $total_pages ) {

			$page_string .= ( $on_page > 5 ) ? " ... " : ", ";

			$init_page_min = ( $on_page > 4 ) ? $on_page : 5;

			$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;



			for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++) {

				$page_string .= ($i == $on_page) ? "<b>" . $i . "</b>" : "<a href=\"$base_url&tru=$tru&start=" . ( ( $i - 1 ) * $per_page )  . "\">" . $i . "</a>";

				if ( $i <  $init_page_max + 1 ) {

					$page_string .= ", ";

				}

			}

			$page_string .= ( $on_page < $total_pages - 4 ) ? " ... " : ", ";

		}

		else {

			$page_string .= " ... ";

		}

		for($i = $total_pages - 2; $i < $total_pages + 1; $i++) {

			$page_string .= ( $i == $on_page ) ? "<b>" . $i . "</b>"  : "<a href=\"$base_url&tru=$tru&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&tru=$tru&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&tru=$tru&start=" . ( ( $on_page - 2 ) * $per_page )  . "\">Prev</a>  " . $page_string;

	}



	if ( $on_page < $total_pages ) {

		$page_string .= "  <a href=\"$base_url&tru=$tru&start=" . ( $on_page * $per_page )  . "\">Next</a>";

	}

}



return $page_string;

}

Link to comment
Share on other sites

Sorry but i admit i am an idiot Crayon lol

 

Anyhow i believe i fixed the error with the following changes in the code:

<?
function generate_pagination($base_url, $num_items, $per_page, $start_item, $tru, $add_prevnext_text = TRUE) {

$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 > 5 ) {

	$init_page_max = ( $total_pages > 3 ) ? 3 : $total_pages;

	for($i = 1; $i < $init_page_max + 1; $i++) {

		$page_string .= ( $i == $on_page ) ? "<b>" . $i . "</b>" : "<a href=\"$base_url&tru=$tru&start=" . ( ( $i - 1 ) * $per_page )  . "\">" . $i . "</a>";

		if ( $i <  $init_page_max ) {

			$page_string .= ", ";

		}

	}

	if ( $total_pages > 3 ) {

		if ( $on_page > 1  && $on_page < $total_pages ) {

			$page_string .= ( $on_page > 4 ) ? " ... " : ", ";

			$init_page_min = ( $on_page > 3 ) ? $on_page : 6;

			$init_page_max = ( $on_page < $total_pages - 4 ) ? $on_page : $total_pages - 4;



			for($i = $init_page_min - 1; $i < $init_page_max + 2; $i++) {

				$page_string .= ($i == $on_page) ? "<b>" . $i . "</b>" : "<a href=\"$base_url&tru=$tru&start=" . ( ( $i - 1 ) * $per_page )  . "\">" . $i . "</a>";

				if ( $i <  $init_page_max + 1 ) {

					$page_string .= ", ";

				}

			}

			$page_string .= ( $on_page < $total_pages - 2 ) ? " ... " : ", ";

		}

		else {

			$page_string .= " ... ";

		}

		for($i = $total_pages - 2; $i < $total_pages + 1; $i++) {

			$page_string .= ( $i == $on_page ) ? "<b>" . $i . "</b>"  : "<a href=\"$base_url&tru=$tru&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&tru=$tru&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&tru=$tru&start=" . ( ( $on_page - 2 ) * $per_page )  . "\">Prev</a>  " . $page_string;

	}



	if ( $on_page < $total_pages ) {

		$page_string .= "  <a href=\"$base_url&tru=$tru&start=" . ( $on_page * $per_page )  . "\">Next</a>";

	}

}



return $page_string;

}
?>

Link to comment
Share on other sites

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.