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 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。