如何显示搜索结果为"显示300的300"

时间:2016-04-15 10:20:07

标签: javascript php jquery mysql

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

这是代码。

<?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>

1 个答案:

答案 0 :(得分:0)

更新您的查询:

$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将返回总行数(不考虑限制子句)