开发者

How to remove "next" option in pagination script when there are no further entries to show?

I have this script that I have posted a bunch of times but was never able to explain clearly how it was working. But now I can, so here it is. I am trying to show the option to see "next" only when there are more entries to be shown.

  $result =   mysql_query("SELECT * FROM table开发者_JS百科 WHERE field1 LIKE '%$q%' 
  OR field2 LIKE '%$q%' OR field3 LIKE '%$q%' OR field4 LIKE '%$q%'");


  $num_rows1 = mysql_num_rows($result);

//check if the starting row variable was passed in the URL or not
if (!isset($_GET['pg']) or !is_numeric($_GET['pg'])) {
  //we give the value of the starting row to 0 because nothing was found in URL
  $startrow = 0;
  //otherwise we take the value from the URL
} else {
  $startrow = (int)mysql_real_escape_string($_GET['pg']);
}

$sql = "SELECT field1, field2, field3, field4 FROM table WHERE
user_id='$id' limit $startrow, 10";
$result = mysql_query($sql);

$query = mysql_query($sql) or die ("Error: ".mysql_error());

if ($result == "") {
  echo "";
}

echo "";

$rows = mysql_num_rows($result);

if($rows == 0) {
  print("");
} elseif($rows > 0) {
  while($row = mysql_fetch_array($query)) {
    $field1 = $row['field1'];
    $field2 = $row['field2'];
    $field3 = $row['field3'];
    $field4 = $row['field4'];

    print("");
  }
}

if($num_rows1 > 10) {
  echo '<a id=pg href="'.$_SERVER['PHP_SELF'].'?pg='.($startrow+10).'&q='.($q).'"> Next</a>';
} 

$prev = $startrow - 10;

//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
  echo '<a id=pgnvg2 href="'.$_SERVER['PHP_SELF'].'?pg='.$prev.'&q='.($q).'">
  Previous</a>';
}


Pagination:

 Total Items
 Items per Page
 Current Page

You only need to show the next navigation if:

 Total Items >= Items per Page * (Current Page + 1)

That simple it is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜