php url and pagination

i have a problem in my code, when i click on page number the page is opening correctly but there are only 6 pages but there is no 7th page but when i am replacing the page number in the url to '7' the page is opening :(


why is this happening,,???

also i've created the auto links using url ecnode for fetched data of mysql but the the page which are not exist are also opening :(

for example , i have fetched a database row 'abc' so the link has created.

but when i am replacing this abc in the url with something else then the page is still opening :( it is not exist then why it is opening :( ?

<?php $name=$_POST['name']; ?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="text" name="name">
<input type="submit" value="GO" name="submit">


	include ('db.php');
	mysql_query ("INSERT INTO example (name) VALUES('$name')") or die(mysql_error());

	if (!isset($_GET['pagenum']) or !is_numeric($_GET['pagenum'])) {
	$pagenum = 0;
	else {
	$pagenum = (int)$_GET['pagenum'];
	$query = "SELECT * FROM example ORDER BY id DESC LIMIT $pagenum, $per_page"; 

$result = mysql_query($query) or die(mysql_error());
	while($row = mysql_fetch_array($result)){
	echo $row['name'] ." "." <a href= 'like.php?quote=" . urlencode( $row['name'] ) . "'>Click Here</a>";
	echo "<br>";
	$last = ceil ($total_rows/$per_page);
	if ($pagenum > $last){
	else if ($pagenum < 1 ){
	$pagenum = 1;
	for ($i=1; $i<=$last; $i++){
	echo '<a href="'.$_SERVER['PHP_SELF'].'?pagenum='.($i).'"> '.$i.'</a>';
6 Years
Discussion Span
Last Post by diafol

I'm not at a computer with php installed but I have a hunch. Line 30 includes $total_rows/$per_page . Both of those variables are integers so the rules of integer math are in play -- the result will be an integer, not a float. ceil() is left with nothing to round up.

Try setting $per_page = 10.0;


Just a quick look, your pagenum is initially set at 0? SO your 5 pages:

Your ceil() function will probably return page = 5.

Decide on your common base - 0 or 1.


i replaced 10 by 10.0 but this is not my problem the problem is that the pages which are no exist are also opening like page number 12 , or 13 or etc...

Edited by Xufyan: n/a

$records_per_page = 10;

$r = mysql_query("SELECT id FROM table");
$total_records = mysql_num_rows($r);

//have a routine here if $total_records = 0;

$num_pages = ceil($total_records/$records_per_page);

if(isset($_GET['page']) && is_int($_GET['page']) && $_GET['page'] > 0){
  $page_candidate = $_GET['page']; //assume pages start at page=1...
  if($page_candidate > $num_pages)$page_candidate = $num_pages; 
  $page_candidate = 1;

$i = 1;
$page_links = "";

  $page_links .= "<a href=\"?page=$i\" />$i</a> ";
$offset = ($page_candidate-1)*$records_per_page;
$r = mysql_query("SELECT fields FROM table LIMIT $offset,$records_per_page");

...(return your records)...

echo "<div id=\"page_links\">$page_links</div>";

Not checked, but something like that. Maybe.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.