如何删除“下一个”当没有更多条目可显示时分页脚本中的选项?

发布于 2024-12-09 10:49:48 字数 1496 浏览 0 评论 0原文

我有这个脚本,我已经发布了很多次,但始终无法清楚地解释它是如何工作的。但现在我可以了,所以就在这里。我试图仅在有更多条目要显示时才显示查看“下一个”的选项。

  $result =   mysql_query("SELECT * FROM table 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>';
}

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 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>';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丢了幸福的猪 2024-12-16 10:49:49

分页:

 Total Items
 Items per Page
 Current Page

您只需在以下情况下显示下一个导航:

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

就这么简单。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文