如何将搜索结果显示为“搜索结果”;显示300〃中的1-10〃;


How do I display results from search as "Showing 1-10 of 300"

我很难找到一种方法来显示在我的分页页面上找到的结果,例如,如果搜索只有9个结果,我会得到"显示9个结果中的10个",并且我希望它显示为"1-10个,11-20个,或1-9个,等等"。

这是代码。

<?php
$con = mysqli_connect("localhost", "root", "root", "courses");
// $total = mysqli_query($con, "SELECT count(title) as total")->fetch()['total']);

if(isset($_GET['searchword'])){
  $searchword = $_GET['searchword'];
}
else {
  $searchword = "";
}
$result = mysqli_query($con, "SELECT count(title) FROM course WHERE concat(award,' ',title) LIKE '%$searchword%'");
$row = mysqli_fetch_row($result);
// Total rows.
$rowstotal = $row[0];
//pages
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 100 ? (int)$_GET['per-page'] : 10;
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;
$pages = ceil($rowstotal / $perPage);
$sql = "SELECT * FROM course WHERE concat(award,' ',title) LIKE '%$searchword%' ORDER BY title ASC LIMIT {$start},{$perPage}";
$query = mysqli_query($con, $sql);
$resultsText = "$rowstotal results";
$resultsList = '';
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
  $courseId = $row["id"];
  $courseAward = $row["award"];
  $courseName = $row["title"];
  $courseDetails = $row["summary"];
  $resultsList .= '<a href="course-page.php?id='.$courseId.'">'.$courseAward.' '.$courseName.'</a> <br>'.$courseDetails.'<br/>';
}
mysqli_close($con);
?>


<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Courses</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="style.css">
  </head>
  <body>
    <nav class="navbar navbar-default navbar-static-top navbar-inverse">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">
            <img alt="Napier" src="">
          </a>
        </div>
      </div>
    </nav>
    <div class="container">
        <!-- Search -->
        <form method="GET">
           <input type="text" class="form-control" id="searchword" name="searchword" placeholder="Search" value="<?php echo $searchword ?>" style="width:30%;"><br>
        </form>
        <!-- Results -->
        <div class="results">
          <p></p>
          <p>Showing <?php echo $perPage; ?> of <?php echo $resultsText; ?></p>
          <p id="content"><?php echo $resultsList; ?></p>
        </div>
        <div class="pagination">
            <?php for($x = 1; $x <= $pages; $x++): ?>
                <a href="?page=<?php echo $x; ?>&per-page=<?php echo $perPage; ?>&searchword=<?php echo $searchword; ?>"<?php if($page === $x) { echo ' class="selected"'; } ?>><?php echo $x; ?></a>
            <?php endfor; ?>
        </div>

    </div>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function() {
        $('#searchword').autocomplete({
          source: function(request, response){
            $.ajax({
              url:"auto.php",
              dataType:"json",
              data:{term:request.term},
              success: function(data){
                response(data.slice(0,10));
              }
            });
          },
          minLength: 1,
          select: function(event,ui){
          }

        });
      });
    </script>
  </body>
</html>

更新您的查询:

$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM course WHERE concat(award,' ',title) LIKE '%$searchword%' ORDER BY title ASC LIMIT {$start},{$perPage}";

这个查询就在上面的查询下面。Select found_rows() as tot此tot将返回总行数(不考虑限制子句)