PHP code example of ttskch / pagination-service-provider

1. Go to this page and download the library: Download ttskch/pagination-service-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ttskch / pagination-service-provider example snippets


$app->register(new Ttskch\Silex\Provider\PaginationServiceProvider());

$app['knp_paginator.options'] = array(
    'default_options' => array(
        'sort_field_name' => 'sort',
        'sort_direction_name' => 'direction',
        'filter_field_name' => 'filterField',
        'filter_value_name' => 'filterValue',
        'page_name' => 'page',
        'distinct' => true,
    ),
    'template' => array(
        'pagination' => '@knp_paginator_bundle/sliding.html.twig',
        'filtration' => '@knp_paginator_bundle/filtration.html.twig',
        'sortable' => '@knp_paginator_bundle/sortable_link.html.twig',
    ),
    'page_range' => 5,
);

// in your controller.

$pagination = $app['knp_paginator']->paginate($someData);

return $app['twig']->render('index.html.twig', array(
    'pagination' => $pagination,
));

// in your controller.

$array = /* some two dimensional array */;

$sort = $request->get('sort');
$direction = $request->get('direction', 'asc');
$filterField = $request->get('filterField');
$filterValue = $request->get('filterValue');

$array = Hash::extract($array, "{n}[{$filterField}=/{$filterValue}/]");
$array = Hash::sort($array, "{n}.{$sort}", $direction);

$pagination = $app['knp_paginator']->paginate($array); // You can get filtered and sorted pagination object.

// in your controller.

$sort = $request->get('sort');
$direction = $request->get('direction', 'asc') === 'asc' ? 'asc' : 'desc';
$filterField = $request->get('filterField');
$filterValue = $request->get('filterValue');

$qb = $app['db']->createQueryBuilder()
    ->select('t.*')
    ->from('table', 't')
    ->where("{$app['db']->quoteIdentifier($filterField)} like {$app['db']->quote('%' . $filterValue . '%')}")
    ->orderBy($app['db']->quoteIdentifier($sort), $direction)
;

$pagination = $app['knp_paginator']->paginate($qb);

$app['knp_paginator.options'] = array(
    'template' => array(
        'pagination' => '@ttskch_silex_pagination/pagination-bootstrap3.html.twig',
        'filtration' => '@ttskch_silex_pagination/filtration-bootstrap3.html.twig',
    ),
);

$app['knp_paginator.limits'] = array(10, 25, 50, 100, 200, 500),

$app['translator.domains'] = array(
    'messages' => array(
        'ja' => array(
            'Previous' => '前へ',
            'Next' => '次へ',
        ),
    ),
);

$app['translator.domains'] = array(
    'messages' => array(
        'ja' => array(
            'Items per page' => '1ページの件数',
            'Filter' => '絞り込み',
        ),
    ),
);