0

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>';
	}
	
?>
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by diafol
0

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;

0

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.

0

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

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

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.