分页的MySql高级搜索结果

时间:2017-02-05 11:21:28

标签: php mysql

对于像这样的高级搜索,选择一个或多个条件。如果超过10(每页),如何将它们存储为分页? 我只收到第一页的结果,但是第二页,第三页等等。它只会让我回复:

 SELECT b.sid, b.filename, b.bgname, b.type, b.data, b.tree, b.uploader,c.uid, c.username, b.active, b.lng, b.description, b.year, 
(SELECT COUNT(*) AS tod FROM sub_comments WHERE sid=b.sid) as comments, 
(SELECT name AS frames FROM sub_frames WHERE id=b.sframe) as frame,
(SELECT dl AS tot FROM sub_dl WHERE sid=b.sid) as total,
(SELECT SUM(rate)/COUNT(*) FROM sub_rate WHERE id_POST=b.sid) as rating,
(SELECT name AS post FROM sub_posters WHERE sid=b.sid) as poster FROM subtitles.sub_files AS b left join forums.mybb_users AS c ON uid=b.uploader WHERE LIMIT 10, 10

没有 WHERE 条件。

Mysql和PHP代码:

if(isset($_POST['name']) && !empty($_POST['name'])){
        $name=mysqli_real_escape_string($conn,strip_tags(trim($_POST['name']))); 
        $where[1]= " b.filename LIKE '%$name%' OR b.bgname LIKE '%$name%' ";

    }
    if(isset($_REQUEST['year']) && !empty($_REQUEST['year'])){
        $year=mysqli_real_escape_string($conn,strip_tags(trim($_REQUEST['year'])));
        $where[2]= " b.year LIKE '$year' ";
    }

    if(isset($_REQUEST['lng']) && !empty($_REQUEST['lng'])){
        $slang=mysqli_real_escape_string($conn,strip_tags(trim($_REQUEST['lng'])));
    }

 if(count($where>0)){
    $where=' WHERE '.implode(' AND ',$where);
   } else {
      $where ="";
  }               

                            $sql="SELECT b.sid, b.filename, b.bgname, b.type, b.data, b.tree, b.uploader, c.uid, c.username, b.active, b.lng, b.description, b.year, 
                            (SELECT COUNT(*) AS tod FROM sub_comments WHERE sid=b.sid) as comments, 
                            (SELECT name AS frames FROM sub_frames WHERE id=b.sframe) as frame, 
                            (SELECT dl AS tot FROM sub_dl WHERE sid=b.sid) as total, 
                            (SELECT SUM(rate)/COUNT(*) FROM sub_rate WHERE id_POST=b.sid) as rating, 
                            (SELECT name AS post FROM sub_posters WHERE sid=b.sid) as poster 
                            FROM $sitedb.sub_files AS b left join $forumdb.mybb_users AS c ON uid=b.uploader $where ";


    $targetpage = "index.php";  
    $limit = 10; 

$go=mysqli_query($conn,$sql);
            $tpages=mysqli_num_rows($go);
            $total_pages = $tpages;

    $stages = 3;
    $page = mysqli_escape_string($conn,$_GET['page']);
    if($page){
        $start = ($page - 1) * $limit; 
    }else{
        $start = 0; 
        }   

    $sql .= " LIMIT $start, $limit";
    $result = mysqli_query($conn,$sql);

    if ($page == 0){$page = 1;}
    $prev = $page - 1;  
    $next = $page + 1;                          
    $lastpage = ceil($total_pages/$limit);      
    $LastPagem1 = $lastpage - 1;

while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
...............
}

HTML:

    <div align="center"><form id="test" action="index.php" method="post">
        <input tabindex="" type="text" name="name" id="name" placeholder="search name" value="<?php if(isset($name)){echo  $name; }?>" />
          <input type="number" min="1890" max="2017" name="year" id="year" placeholder="year" value="<?php if(isset($year)){echo  $year; }?>" />
          <input tabindex="" type="text" name="upnal" id="upnal" placeholder="username" value="<?php if(isset($upnal)){echo  $upnal; }?>" />
        <select name="lng">
  <option value="BG">Lang1</option>
  <option value="EN">Lang2</option>
  <option value="RU">Lang3</option>
  <option value="ALL">LangAll</option>
    </select>
<input type="submit" class="btn btn-blue fa-input" type="button" value="&#xf002; Search">

对于分页,我会进行2次查询(对于结果页面,我希望显示的不仅仅是下一个)。

0 个答案:

没有答案