如何在Laravel 5中实现对Sphinx Search的分页

时间:2016-05-02 06:53:57

标签: php laravel search pagination sphinx

我在Laravel 5中实现了Sphinx Search。现在我想在此实现分页。我没有得到任何帮助,Laravel 5 paginate没有开发Sphinx Search。

这是我的代码:

public function index() {
   $sphinx = new SphinxSearch();
   $results = $sphinx->search('php','jobma1')->query()->paginate(5); //geting error :(
   $results = $sphinx->search('php','jobma1')->query(); //working fine :)
   dd($results); die;
}

请告诉我如何在Laravel 5

中实施Sphinx搜索的分页

2 个答案:

答案 0 :(得分:1)

使用此

在控制器中

 $page = 1
    $queryString = $request->all();
    if (isset($queryString['page'])) { $page = $queryString['page']; }
    if ($page > 0) { $startFrom = ($page-1) * $numRecPerPage; }
    $startFrom = 0;
    $numRecPerPage = 6;
    $sphinx = new SphinxSearch();
    $sphinx->setMatchMode('SPH_MATCH_BOOLEAN');
    $sphinx->search('php Developer software ', 'jobma1');
    // Limit parameters (Number of records display, start from )
    // If first parameter 10 then 10 records will display
    // If second parameter is 5 then first 5 records will not display = display from 6 
    $result =  $sphinx->limit($numRecPerPage, $startFrom)->query();

在视图中

<?php //dd($result);?>
<?php if (isset($result['matches'])) { ?>
    <table>
        <tr style="text-align:left">
            <th style= "width:100px;">
                ID
            <th>
            <th>
                Post Name
            </th>
        </tr>
        <tbody>
        <?php
            foreach ($result['matches'] as $key => $value) {
                echo '<tr>';
                    echo '<td>'; echo $key; echo '<td>';
                    echo '<td>'; echo $value['attrs']['jobma_post_name']; echo '<td>';
                echo '<tr>';
            }
        ?>
        </tbody>
    </table>
    <?php
        // Pagination Logic start from here
        $totalRecords = $result['total_found'];
        if ($totalRecords > $numRecPerPage) {
            $totalPages = ceil($totalRecords / $numRecPerPage);
            $startLoop = 1;
             $endLoop = $totalPages;
            if ( $totalPages > 6) {
                $endLoop = 6;
            }
            $page = $_GET['page'];
            $endPage = $page+1;
            if ($page >= 4) {
                $startLoop = $page - 3;
                $endLoop = $page + 3;
                if ($endLoop > $totalPages) {
                    $startLoop = $totalPages - 6;
                    $endLoop = $totalPages;
                }
                if ($startLoop < 1) {
                    $startLoop = 1;

                }
            }
            if ($page > 1) {
                $prePage = $page - 1;
                echo "<a href='users?page=1'>".'<<'."</a> ";
                echo "<a href='users?page=$prePage'>".'<'."</a> ";
            }
            for ($i=$startLoop; $i<=$endLoop; $i++) { 
                $class ="";

                if ($i == $page) { $class ="class='activeClass'"; }

                if ($page == $i ) { echo "<a href='javascript:void(0);' $class> ".$i; } else { echo "<a href='users?page=".$i."' $class> ".$i; }

                if ($i < $endLoop) { echo  "  </a> ";  } else { echo "</a>"; }
            }
            if ($endPage <= $totalPages  ) {
                echo "<a href='users?page=$endPage'>".'>'."</a> "; // Goto last page
                echo "<a href='users?page=$totalPages'>".'>>'."</a> ";
            }
            echo '<br/>';
            echo '<br/>';
        }
        echo 'Total Number of Records: '. $totalRecords;
        // Pagination Logic end from here
    ?>
    <style>
    a { background: none repeat scroll 0 0 #FFD700; border: 1px solid #FFD700; margin: 0 3px 2px; padding: 2px 3px 0; text-decoration: none; }
    a.activeClass { color:green; font-weight: bolder; }
    </style>
<?php } else {  ?>
    No records found
<?php } ?>

答案 1 :(得分:0)

一种更简单的方法是使用Illuminate\Pagination\LengthAwarePaginator

public function doSearch()
{
    $template = 'search/search_results';
    $filters = Input::get('filter');
    $currentPage = Input::get('page') ? Input::get('page') : 1;
    $perPage = 5;
    // prepare $sphinxQuery using the filters received
    $results = SphinxSearch::search($sphinxQuery, 'indexname')
        ->setFieldWeights(
            array(
                    'name' => 10,
            )
        )
        ->setMatchMode(SphinxClient::SPH_MATCH_EXTENDED)
        ->setSortMode(SphinxClient::SPH_SORT_EXTENDED, "name ASC")
        ->limit(1000)
        ->get(true);
    $paginator = new LengthAwarePaginator(
        collect($results)->forPage($currentPage, $perPage), sizeof($results), $perPage, $currentPage
    );
    $paginator->setPath(route('name.of.the.route'));
    $paginator->setPageName('page');  
    $data['items'] = $paginator;  

    return View::make($template, $data);
}

在刀片模板中:

<?php echo $items->appends(['filter' => Request::get('filter')])->render(); ?>