编码空格字符的网址

时间:2018-08-01 14:48:29

标签: php jquery-pagination

有人请帮助我。我有搜索表单,并使用jquery twbsPagination显示来自数据库和即时消息的数据。所以我的问题是,如果用户搜索两个或多个单词,则url中的空格。我使用了 encodeURIComponent(); ,它的显示效果非常好。但是,当我单击第2页时,它不显示任何数据,当我返回首页时,数据不再显示。请帮助我。我调试了12个小时,对不起我的英语。 这是我的表格

<div class="input-group input-group-lg">
            <input id="searchBar" type="text" class="form-control" placeholder="Search job">
            <span class="input-group-btn">
              <button id="searchBtn" type="button" class="btn btn-info btn-flat">Go!</button>
            </span>
        </div>

这是我的剧本

<script>
 function Pagination(){
<?php
  $limit=5;
  $sql="SELECT COUNT(id_jobpost) AS id from job_post";
  $result=$conn->query($sql);
  if($result->num_rows > 0)
  {
    $row = $result->fetch_assoc();
    $total_records = $row['id'];
    $total_pages = ceil($total_records / $limit);
  } else {
    $total_pages = 1;
  }
?>
$('#pagination').twbsPagination({
  totalPages: <?php echo $total_pages;?>,
  visible: 5,
  onPageClick: function(e, page){
    e.preventDefault();
    $(".target-content").html("Loading....");
    $(".target-content").load("job-pagination.php?page="+page);
  }
});
}
</script>

<script>
 $(function(){
  Pagination();
 });
</script>

<script>
$("#searchBtn").on("click", function(e){
  e.preventDefault();
  var searchResult = $("#searchBar").val();
  var filter = "searchBar";
  if (searchResult != "") {
    $('#pagination').twbsPagination('destroy');
    Search(searchResult,filter);
  }else{
    $('#pagination').twbsPagination('destroy');
    Pagination();
  }
});

<script>
      function Search(val,filter){
        $('#pagination').twbsPagination({
        totalPages: <?php echo $total_pages; ?>,
        visible: 5,
        onPageClick: function(e, page){
          e.preventDefault();
          val = encodeURIComponent(val);
          $(".target-content").html("Loading....");
          $(".target-content").load("search.php?page="+page+"&search="+val+"&filter="+filter);
        }
      });
      }
    </script>

这是我的search.php

<?php
session_start();
require_once("db.php");
 $limit = 5;

 if (isset($_GET['page'])) {
   $page = $_GET['page'];
 }else{
    $page = 1;
 }

 $start_from = ($page-1) * $limit;
 $search = $_GET['search'];
 $sql = "SELECT * FROM job_post WHERE jobtitle LIKE '%$search%' ORDER BY id_jobpost DESC LIMIT $start_from, $limit";
$result=$conn->query($sql);
       if ($result->num_rows>0) {
        while ($row=$result->fetch_assoc()) {
            $sql1 = "SELECT * FROM company WHERE id_company='$row[id_companyname]'";
                $result1 = $conn->query($sql1);
                if($result1->num_rows > 0) {
                while($row1 = $result1->fetch_assoc()) 
                    {
       ?>

            <div class="attachment-block clearfix">
                <img class="attachment-img" src="uploads/logo/<?php echo $row1['logo']; ?>" alt="Attachment Image">
                <div class="attachment-pushed">
                  <h4 class="attachment-heading"><a href="view-job-post.php?id=<?php echo $row['id_jobpost']; ?>"><?php echo $row['jobtitle']; ?></a> <span class="attachment-heading pull-right">₱<?php echo $row['maximumsalary']; ?>/Month</span></h4>
                  <div class="attachment-text">
                      <div><strong><?php echo $row1['companyname']; ?> | <?php echo $row1['province'].",".$row1['city']; ?> | Experience Required(in years): <?php echo $row['experience']; ?></strong></div>
                  </div>
                </div>
              </div>

       <?php
                }
            }
        }
       }else{
        echo "<center><strong>No Job Matched!</strong></center>";
       }
       $conn->close();
       ?>

1 个答案:

答案 0 :(得分:0)

我认为错误在于您的search.php页面代码中。您需要更改结束限制。

 $limit = 5;

 if (isset($_GET['page'])) {
   $page = $_GET['page'];
 }else{
    $page = 1;
 }

 $start_from = ($page-1) * $limit;
 $search = $_GET['search'];
 $sql = "SELECT * FROM job_post WHERE jobtitle LIKE '%$search%' ORDER BY id_jobpost DESC LIMIT $start_from, $limit";

在第一页上,您正在执行:LIMIT 0,5。在第二页上,您正在执行:LIMIT 5,5。

我建议:

$limit = 5;

if (isset($_GET['page'])) {
   $page = $_GET['page'];
}else{
    $page = 1;
}

$start_from = ($page-1) * $limit;
$end_at = $page * $limit;
$search = $_GET['search'];
$sql = "SELECT * FROM job_post WHERE jobtitle LIKE '%$search%' ORDER BY id_jobpost DESC LIMIT $start_from, $end_at";

但是,我还注意到,在您的搜索页面中,您正在查询中进行查询,我认为更好的方法是使用联接,这样您只需打一次数据库即可。我不知道您的数据库的结构,但是下面的代码是一个更好的方法示例,因此您只需打一次数据库即可:

<?php
session_start();
require_once("db.php");
$page_size = 5;

if (isset($_GET['page'])) {
   $page = $_GET['page'];
}else{
    $page = 1;
}

$start_from = ($page-1) * $page_size;
$end_at = $page * $page_size;

$search = $_GET['search'];
$sql = "
  SELECT 
    j_p.*, 
    c.* 
  FROM 
    job_post j_p, 
    company c 
  WHERE 
    jobtitle LIKE '%$search%' 
    and c.id_company = j_p.id_companyname
  ORDER BY 
    id_jobpost DESC 
  LIMIT 
    $start_from, 
    $end_at
";
$result=$conn->query($sql);
if ($result->num_rows>0) {
  while($row1 = $result1->fetch_assoc()) {
?>

  <div class="attachment-block clearfix">
    <img class="attachment-img" src="uploads/logo/<?php echo $row1['logo']; ?>" alt="Attachment Image">
    <div class="attachment-pushed">
      <h4 class="attachment-heading"><a href="view-job-post.php?id=<?php echo $row1['id_jobpost']; ?>"><?php echo $row1['jobtitle']; ?></a> <span class="attachment-heading pull-right">₱<?php echo $row1['maximumsalary']; ?>/Month</span></h4>
      <div class="attachment-text">
          <div><strong><?php echo $row1['companyname']; ?> | <?php echo $row1['province'].",".$row1['city']; ?> | Experience Required(in years): <?php echo $row1['experience']; ?></strong></div>
      </div>
    </div>
  </div>
<?php
  }
}else{
  echo "<center><strong>No Job Matched!</strong></center>";
}
$conn->close();
?>