【问题标题】:how do i achieve pagination when retrieving and displaying rows from a database table从数据库表中检索和显示行时如何实现分页
【发布时间】:2017-10-07 15:00:13
【问题描述】:

我正在从 mysql 数据库中检索很多项目并将它们发布到 html 页面我如何将一些项目自动移动到 php 中的新页面,这样某人就不必过度滚动到 html 页面的底部

<?php
$query = "SELECT * FROM venueimage "; 
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result)){

echo '
<div  class="container">
<div style="border : 2px solid #FDF3E7; background:#ECECEA;"           class="row">';
echo' <div  class="col-sm-4 border-right">';
echo "<u >"; echo "Venue ID :"; echo  $row['id']   ; echo "</u>";            echo "</br>";

echo "Venue name :"; echo  $row['venuename']   ; echo "</br>";

echo "Venue address :"; echo  $row['address']   ; echo "</br>";

echo "Venue phone :"; echo  $row['phone']   ; echo "</br>";

echo "Venue capacity :"; echo  $row['capacity']   ; echo "</br>";

echo "preferredfor :"; echo  $row['preferredfor']   ; echo "</br>";

echo "Venue price :"; echo  $row['price']   ; echo "</br>";

echo "brief description :"; echo  $row['briefdescription']   ; echo "       </br>";

echo '</div>';
echo '<div  class="col-sm-8">';
echo'
  
<img style="width: 790; height: 250"    src="data:image/jpeg;base64,'.base64_encode($row['venueimage']).'    "    />  ';
  echo '</div>';
  echo '</div>';
  echo "<hr>";
  echo '</div>';
  echo "</br>";

  }

  mysqli_close($connect);
  ?>

【问题讨论】:

  • 试着更清楚地解释你想要达到的目标。
  • 查看分页。
  • 我想要的只是每页显示至少 5 条记录的 sql 选择语句

标签: php mysql sql pagination


【解决方案1】:

听起来你想实现一个添加分页(分页)的解决方案。这是一个常见的要求,StackOverflow 和其他页面上存在许多解决方案。

看看这个,它应该让你开始:Simple PHP Pagination script

try {

// Find out how many items are in the table
$total = $dbh->query('
    SELECT
        COUNT(*)
    FROM
        table
')->fetchColumn();

// How many items to list per page
$limit = 20;

// How many pages will there be
$pages = ceil($total / $limit);

// What page are we currently on?
$page = min($pages, filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT, array(
    'options' => array(
        'default'   => 1,
        'min_range' => 1,
    ),
)));

// Calculate the offset for the query
$offset = ($page - 1)  * $limit;

// Some information to display to the user
$start = $offset + 1;
$end = min(($offset + $limit), $total);

// The "back" link
$prevlink = ($page > 1) ? '<a href="?page=1" title="First page">&laquo;</a> <a href="?page=' . ($page - 1) . '" title="Previous page">&lsaquo;</a>' : '<span class="disabled">&laquo;</span> <span class="disabled">&lsaquo;</span>';

// The "forward" link
$nextlink = ($page < $pages) ? '<a href="?page=' . ($page + 1) . '" title="Next page">&rsaquo;</a> <a href="?page=' . $pages . '" title="Last page">&raquo;</a>' : '<span class="disabled">&rsaquo;</span> <span class="disabled">&raquo;</span>';

// Display the paging information
echo '<div id="paging"><p>', $prevlink, ' Page ', $page, ' of ', $pages, ' pages, displaying ', $start, '-', $end, ' of ', $total, ' results ', $nextlink, ' </p></div>';

// Prepare the paged query
$stmt = $dbh->prepare('
    SELECT
        *
    FROM
        table
    ORDER BY
        name
    LIMIT
        :limit
    OFFSET
        :offset
');

// Bind the query params
$stmt->bindParam(':limit', $limit, PDO::PARAM_INT);
$stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
$stmt->execute();

// Do we have any results?
if ($stmt->rowCount() > 0) {
    // Define how we want to fetch the results
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $iterator = new IteratorIterator($stmt);

    // Display the results
    foreach ($iterator as $row) {
        echo '<p>', $row['name'], '</p>';
    }

} else {
    echo '<p>No results could be displayed.</p>';
}

} catch (Exception $e) {
echo '<p>', $e->getMessage(), '</p>';
}

感谢@Nev Stokes

【讨论】:

  • 我已经尝试了各种替代方案,但似乎无法正常工作,您是否有其他解决方案
  • 如果答案是 100% 基于另一个堆栈答案,它确实应该作为副本关闭。
猜你喜欢
  • 2015-02-28
  • 2019-05-23
  • 1970-01-01
相关资源
最近更新 更多