如何删除“下一个”当没有更多条目可显示时分页脚本中的选项?
我有这个脚本,我已经发布了很多次,但始终无法清楚地解释它是如何工作的。但现在我可以了,所以就在这里。我试图仅在有更多条目要显示时才显示查看“下一个”的选项。
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
分页:
您只需在以下情况下显示下一个导航:
就这么简单。
Pagination:
You only need to show the next navigation if:
That simple it is.