douxing1850 2018-10-31 10:15
浏览 65
已采纳

PHP / SQL - 模糊搜索结果的分页

I have created a search function for the products on my site and I tried to put in pagination so that it is not just a big long list of results. So I started off with something like this:

**Note: I just replaced $_GET['search_term'] with 'whatever' just for this example, and where I have var_dump() I have a function that displays the products for each id in the array it is given.

$term = 'whatever'; //$_GET['search_term'];
$new_term = '%'.$term.'%';

if(isset($_GET['page'])){
    $page = $_GET['page'];
}else{
    $page = 1;
}
$per_page = 20;
$last_page = ceil($resultCount/$per_page);
if($page<1){
    $page = 1;
}else if($page>$last_page){
    $page = $last_page;
}
$pagination = "";
$limit = "LIMIT ".($page-1)*$per_page.",".$per_page;
if($last_page!=1){
    if($page!=1){
        $prev = $page-1;
        $pagination .= "<a class='pagination' href='store'><<</a>";
        $pagination .= "<a class='pagination' href='store/$prev'><</a>";
    }
    for($i=$page-2; $i<=$page+2; $i++){
        if($i>0 && $i<=$last_page){
            if($i == $page){
                $pagination .= "<a class='pagination selected'>$i</a>";
            }else{
                $pagination .= "<a class='pagination' href='store/$i'>$i</a>";
            }
        }
    }
    if($page!=$last_page){
        $next = $page+1;
        $pagination .= "<a class='pagination' href='store/$next'>></a>";
        $pagination .= "<a class='pagination' href='store/$last_page'>>></a>";
    }
}

if(isset($term)){
    echo $pagination;
    $ids = [];
    $params = [$new_term];
    $sql = "SELECT * FROM products WHERE name LIKE ? $limit";
    $stmt = DB::run($sql,$params);
    $resultCount = $stmt->rowCount();
    if($resultCount > 0){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $id = $row["pro_id"];
            $params3 = [$id];
            $sql3 = "SELECT * FROM products WHERE id=?";
            $stmt3 = DB::run($sql3,$params3);
            while($row = $stmt3->fetch(PDO::FETCH_ASSOC)){
                $id = $row["id"];
                array_push($ids,$id);
            }
        }
        var_dump($ids);
    }
    echo $pagination;
}

This worked fine, but then I wanted to make it a fuzzy search so I did:

$term = 'whatever'; //$_GET['search_term'];
$new_term = '%'.$term.'%';

$params = [$new_term];
$sql = "SELECT * FROM products WHERE name LIKE ?";
$stmt = DB::run($sql,$params);
$resultCount = $stmt->rowCount();
if($resultCount < 1){
    $sql = "SELECT * FROM products";
    $stmt = DB::run($sql);
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        $id = $row["pro_id"];
        $result = $row[$lang];
        similar_text($term,$result,$similarity);
        $similar_array[$similarity][] = $id;
    }
    $closest_match = array_keys($similar_array);
    rsort($closest_match);
    $match_count = count($closest_match);
    $similar_ids = [];
    for($i=0; $i<$match_count; $i++){
        foreach($similar_array[$closest_match[$i]] as $id){
            array_push($similar_ids,$id);
        }
    }
    $resultCount = count($similar_ids);
}

if(isset($_GET['page'])){
    $page = $_GET['page'];
}else{
    $page = 1;
}
$per_page = 20;
$last_page = ceil($resultCount/$per_page);
if($page<1){
    $page = 1;
}else if($page>$last_page){
    $page = $last_page;
}
$pagination = "";
$limit = "LIMIT ".($page-1)*$per_page.",".$per_page;
if($last_page!=1){
    if($page!=1){
        $prev = $page-1;
        $pagination .= "<a class='pagination' href='store'><<</a>";
        $pagination .= "<a class='pagination' href='store/$prev'><</a>";
    }
    for($i=$page-2; $i<=$page+2; $i++){
        if($i>0 && $i<=$last_page){
            if($i == $page){
                $pagination .= "<a class='pagination selected'>$i</a>";
            }else{
                $pagination .= "<a class='pagination' href='store/$i'>$i</a>";
            }
        }
    }
    if($page!=$last_page){
        $next = $page+1;
        $pagination .= "<a class='pagination' href='store/$next'>></a>";
        $pagination .= "<a class='pagination' href='store/$last_page'>>></a>";
    }
}

if(isset($term)){
    echo $pagination;
    $ids = [];
    $params = [$new_term];
    $sql = "SELECT * FROM products WHERE name LIKE ? $limit";
    $stmt = DB::run($sql,$params);
    $resultCount = $stmt->rowCount();
    if($resultCount > 0){
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $id = $row["pro_id"];
            $params3 = [$id];
            $sql3 = "SELECT * FROM products WHERE id=?";
            $stmt3 = DB::run($sql3,$params3);
            while($row = $stmt3->fetch(PDO::FETCH_ASSOC)){
                $id = $row["id"];
                array_push($ids,$id);
            }
        }
        var_dump($ids);
    }else{
        var_dump($similar_ids);
    }
    echo $pagination;
}

There was probably a much better way of doing this but this is what I have. My question then is how can I get the pagination to work here for the fuzzy results ($similar_ids)? I was thinking of some sort of function that will splice the array depending on the page number but I am not sure how I would go about this.

  • 写回答

1条回答 默认 最新

  • doufang3001 2018-10-31 10:24
    关注

    This does not answer your question, but I'm going to say these anyway:

    • Looking closely, you have a possible sql injection bug in handling of $limit.
    • Also, using a search index like Apache Solr or ElasticSearch would give you pagination nearly for free anyway. You might want to look into that. Setting up a dedicated index is another can of worms, yes, but then you would also have more and better options to handle that fuzzy search part too.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况