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

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条回答 默认 最新

相关推荐 更多相似问题