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 :(

http://localhost/php/like.php?pagenum=7

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.
http://localhost/php/like.php?quote=abc

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">
</form>

<?php

	include ('db.php');
	
	if(isset($_POST['submit'])) 
	{
	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'];
	}
	$per_page=10;
	$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){
	$pagenum=$last;
	}
	else if ($pagenum < 1 ){
	$pagenum = 1;
	}
	
	for ($i=1; $i<=$last; $i++){
	echo '<a href="'.$_SERVER['PHP_SELF'].'?pagenum='.($i).'"> '.$i.'</a>';
	}
	
?>

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;

Member Avatar for diafol

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

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...

Member Avatar for diafol
$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; 
}else{
  $page_candidate = 1;
}

$i = 1;
$page_links = "";

while($i<=$num_pages){
  $page_links .= "<a href=\"?page=$i\" />$i</a> ";
  $i++;
}
$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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.