简体   繁体   English

PHP限制MySQL返回的行,如果最后一行返回,则执行某些操作

[英]PHP Limit rows returned by MySQL and do something if last row returned

I didn't know exactly how to word this question but by do something I mean that I would like to hide or not show my "next" button that is shown below. 我不知道该怎么表达这个问题,但我的意思是我想隐藏或不显示下面显示的“下一步”按钮。 I have a script that pulls all the images from MySQL and prints them to my page by 30 images per page and the next 30 will create a new page that is activated by my back/next buttons. 我有一个脚本,可以从MySQL中提取所有图像,并按每页30张图像的格式将它们打印到我的页面上,接下来的30张图像将创建一个新页面,该页面由我的后退/下一步按钮激活。 My "back" button has a if statement if $startrow isn't >= 0 than it won't show but I would like the same concept with my next button when the last row in my database is shown and it hides my next button. 我的“后退”按钮上有一个if语句,如果$ startrow不是> = 0则不会显示,但是当数据库中的最后一行显示并隐藏了我的下一个按钮时,我希望下一个按钮具有相同的概念。

I was thinking if you can detect the first empty row or the last row of the database and if so hide the next button. 我在想是否可以检测到数据库的第一行或最后一行,如果可以,则隐藏下一个按钮。 Otherwise it keeps adding 30 to $startrow when nothing is shown on screen. 否则,当屏幕上什么都没有显示时,它将继续在$ startrow中增加30。

I found a script helping me with this here but it didn't tell me how to hide the next button. 我在这里找到了一个脚本来帮助我,但它没有告诉我如何隐藏下一个按钮。

<?php
$startrow = $_GET['startrow'];

if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  $startrow = 0;  
} else {
  $startrow = (int)$_GET['startrow'];
}
?>
<?php

$db = mysqli_connect("localhost", "root", "", "media");
$uploaded = mysqli_query($db, "SELECT * FROM images LIMIT $startrow, 30");

while ($row = mysqli_fetch_array($uploaded)) {
    echo "<div class='img_container'>";
    echo "<li><img class='img_box' src='uploads/images/".$row['image_title']."' ></li>";
    echo "</div>";
}

$prev = $startrow - 30;
if ($prev >= 0) {
    echo '<div class="prevRow"><a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'&startrow='.$prev.'">Back</a></div>';
}
    echo '<div class="nextRow"><a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'&startrow='.($startrow+30).'">Next</a></div>';
?>

You could try something like 您可以尝试类似

$num_rows = 30;  // rows on a page

$db = mysqli_connect("localhost", "root", "", "media");

// get total possible rows
$res = mysqli_query($db, "SELECT count(id) FROM images");
$row = $res->fetch_row();
$total_rows = $row[0];
$res->close();


$uploaded = mysqli_query($db, "SELECT * FROM images LIMIT $startrow, $num_rows");
while ($row = mysqli_fetch_array($uploaded)) {  
    . . .
}


$prev = $startrow - $num_rows;
if ($prev >= 0) {
    echo '<div class="prevRow"><a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'&startrow='.$prev.'">Back</a></div>';
}

if ( $startrow+$num_rows < $total_rows  ) {
    echo '<div class="nextRow"><a href="'.$_SERVER['PHP_SELF'].'?page='.$page.'&startrow='.($startrow+30).'">Next</a></div>';
}

Potentially a little faster than the answer from @RiggsFolly, you can modify your existing query to count the rows. 可能比@RiggsFolly的答案要快一点,您可以修改现有查询以对行进行计数。

SELECT SQL_CALC_FOUND ROWS * FROM images LIMIT $startrow, 30

Then, after the query returns, you run a second query to get the answer: 然后,查询返回后,您运行第二个查询以获得答案:

SELECT FOUND_ROWS()

The FOUND_ROWS() function returns the number of rows the previous query would have returned, without LIMIT (or an offset). FOUND_ROWS()函数返回上一个查询将返回的行数,不带LIMIT (或偏移量)。

This is probably not as fast as your original query would be in isolation, but should be slightly faster than SELECT COUNT(...) ... followed by your original query. 这可能不像原始查询那样快,但是它应该比原始查询后面的SELECT COUNT(...) ...稍快。 With small data sets, though, any differences will likely be below measurable limits. 但是,如果数据集较小,则任何差异都可能会低于可测量的限制。

See also https://dev.mysql.com/doc/refman/5.7/en/information-functions.html 另请参见https://dev.mysql.com/doc/refman/5.7/zh-CN/information-functions.html

You can also combine these things into a stored procedure that accepts items per page and page number, and returns all of the records along with metadata items such as the total number of pages. 您还可以将这些内容组合到一个存储过程中,该存储过程接受每页和页码的项目,并返回所有记录以及元数据项(例如页面总数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM