PHPackages                             usmanhalalit/strana - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. usmanhalalit/strana

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

usmanhalalit/strana
===================

Pagination library for PHP, framework agnostic, with built-in adapters for Doctrine, Eloquent, Pixie and PHP Array.

1.x-dev(12y ago)947.4k20[1 issues](https://github.com/usmanhalalit/strana/issues)[1 PRs](https://github.com/usmanhalalit/strana/pulls)3MITPHPPHP &gt;=5.3.0

Since Nov 13Pushed 8y ago15 watchersCompare

[ Source](https://github.com/usmanhalalit/strana)[ Packagist](https://packagist.org/packages/usmanhalalit/strana)[ Docs](https://github.com/usmanhalalit/strana)[ RSS](/packages/usmanhalalit-strana/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (2)Used By (3)

Strana
======

[](#strana)

### Smart Pagination Library for PHP

[](#smart-pagination-library-for-php)

[![Build Status](https://camo.githubusercontent.com/09f7110132f7c9fea42912e4c5314bcab211747369bab86e47414a3f1a932b8a/68747470733a2f2f7472617669732d63692e6f72672f75736d616e68616c616c69742f737472616e612e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/usmanhalalit/strana) [![SensioLabsInsight](https://camo.githubusercontent.com/fbadef8830c5ba059284d257af423ff8ae2af71ccfce115ac3f95edd469c70d7/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f34393039313238342d626434652d343535642d623832312d6164366233336432356433372f736d616c6c2e706e67)](https://insight.sensiolabs.com/projects/49091284-bd4e-455d-b821-ad6b33d25d37 "This project passes all Insight checks successfully. This is very rare, and is worthy of the Platinum medal. Congratulations to all the contributors to this project for such a high quality.") [![Scrutinizer Code Quality Score](https://camo.githubusercontent.com/b7dd51f598589ac4be6c1b526a1b8ad81799c16ee9e89871384240ec0eea83e0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f75736d616e68616c616c69742f737472616e612f6261646765732f7175616c6974792d73636f72652e706e673f733d34316431336235633761393833643165643336333739393864353939653865303230653837353338)](https://scrutinizer-ci.com/g/usmanhalalit/strana/) [![Bitdeli Badge](https://camo.githubusercontent.com/76abe11c573d507c2cca1fde380478136acf1df1c37c42998d9e29278e61aeae/68747470733a2f2f64327765637a68766c38323376302e636c6f756466726f6e742e6e65742f75736d616e68616c616c69742f737472616e612f7472656e642e706e67)](https://bitdeli.com/free "Bitdeli Badge") [![Dependency Status](https://camo.githubusercontent.com/a018a6129ae49594f6e8ab32aac69064c546157b8b0aaecc308c79e511d077a9/687474703a2f2f7777772e76657273696f6e6579652e636f6d2f7068702f75736d616e68616c616c69743a737472616e612f6465762d6d61737465722f62616467652e706e67)](http://www.versioneye.com/php/usmanhalalit:strana/dev-master)

---

A framework agnostic, smart pagination library for PHP. Just a few lines of code and fully functional pagination is ready.

Paginate your records with Strana. Strana will slice(limit and offset) these records, generate pagination links for you and reads page number from them, all automatically.

#### Features:

[](#features)

- Built-in adapters for [Doctrine](http://www.doctrine-project.org/projects/dbal.html), [Eloquent (Laravel)](https://github.com/illuminate/database), [Pixie](https://github.com/usmanhalalit/pixie), PHP Array and you can do it manually.
- Readable syntax
- Add Infinite Scroll with one line
- It automatically detects which DBAL you are using.
- Styles automatically with Twitter Bootstrap, Zurb Foundation and most of other CSS frameworks.

#### Screenshot:

[](#screenshot)

[![Screenshot](https://camo.githubusercontent.com/11d6ec4067e54aef7779ac6e7b393d22c0223b2e99f5ec16e40857096f64b5e8/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f31353832373436312f737472616e612d73616d706c652e706e67)](https://camo.githubusercontent.com/11d6ec4067e54aef7779ac6e7b393d22c0223b2e99f5ec16e40857096f64b5e8/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f31353832373436312f737472616e612d73616d706c652e706e67)

Example
-------

[](#example)

Basically Strana makes it very easy, like the code below:

```
$paginator = $strana->perPage(10)->make($records);
```

**That's basically it.**

### Full Usage Example

[](#full-usage-example)

```
// Make sure you have Composer's autoload file included
require 'vendor/autoload.php';

$strana = new \Strana\Paginator();
$records = array(1, 2, 3, .... 100);
$paginator = $strana->perPage(10)->make($records);

// Loop paginated items
foreach ($paginator as $item) {
    echo $item['field_name'] . '';
}

// Print pagination links
echo '' . $paginator;
```

There are some advanced options which are documented below. Sold? Lets install.

Installation
------------

[](#installation)

Strana uses [Composer](http://getcomposer.org/doc/00-intro.md#installation-nix) to make things easy, its a requirement.

Learn to use composer and add this to require section (in your composer.json):

```
"usmanhalalit/strana": "1.*@dev"

```

And run:

```
composer update

```

Library on [Packagist](https://packagist.org/packages/usmanhalalit/strana).

Full Walkthrough
----------------

[](#full-walkthrough)

Strana has built in adapters for Doctrine Dbal, Laravel Eloquent, Pixie and PHP native arrays. If you want to paginate from any of these then you're in luck. More adapters will come according to feedback.

### Step 1:

[](#step-1)

Prepare your database or array records:

**Doctrine Dbal Example:**

```
$records = $qb->select('*')->from('sample', 'sample');
```

**Laravel Eloquent (or Query Builder) Example:**

```
$records = Capsule::select('*')->from('sample');
```

**Pixie Example:**

```
$records = QB::select('*')->from('sample');
```

**Array Example:**

```
$records = array(1, 2, 3, 4, 5);
```

### Step 2:

[](#step-2)

Paginate your records with Strana. Strana will slice(limit and offset) these records, generate pagination links for you and reads page number from them, all automatically.

```
$strana = new \Strana\Paginator();
$paginator = $strana->perPage(10)->make($records);
```

### Step 3:

[](#step-3)

Loop paginated records to display:

```
foreach ($paginator as $item) {
    echo $item['field'] . '';
}
```

### Step 4:

[](#step-4)

Print your pagination links:

```
echo $paginator;
```

It will produce something like this:

[![Screenshot](https://camo.githubusercontent.com/11d6ec4067e54aef7779ac6e7b393d22c0223b2e99f5ec16e40857096f64b5e8/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f31353832373436312f737472616e612d73616d706c652e706e67)](https://camo.githubusercontent.com/11d6ec4067e54aef7779ac6e7b393d22c0223b2e99f5ec16e40857096f64b5e8/68747470733a2f2f646c2e64726f70626f7875736572636f6e74656e742e636f6d2f752f31353832373436312f737472616e612d73616d706c652e706e67)

Infinite Scroll
---------------

[](#infinite-scroll)

Strana comes with out of the box Infinite Scrolling, enable it with just one method.

```
$strana->infiniteScroll()->perPage(10)->make($records);
```

And then wrap all your pagination items and pagination links with certain CSS classes, like the example below.

#### Example

[](#example-1)

```
echo '';
foreach ($paginator as $item) {
    print('' . $item['t_value'] . '');
}

echo '' . $paginator;
echo '';
```

**That's it**, you're done with infinite scrolling.

---

Strana uses the awesome [Infinite Ajax Scroll](https://github.com/webcreate/Infinite-Ajax-Scroll) jQuery plugin. All config options supported by this plugin can be passed with Strana.

```
$iasConfig = array(
    'loaderDelay' => 600,
    'loader'      => '',
);

$strana->infiniteScroll($iasConfig)->perPage(10)->make($records);
```

Cool, yeah?

Usage API
---------

[](#usage-api)

#### perPage($perPage)

[](#perpageperpage)

Number of items on per page, default 10.

#### page($page)

[](#pagepage)

Which page to show, default is read from query string else 1.

#### infiniteScroll(Array $config = array())

[](#infinitescrollarray-config--array)

Enable Infinite Scrolling using Ajax. Options can be passed using `$config`.

#### make($records, $adapter = null, $config = array())

[](#makerecords-adapter--null-config--array)

Make and return paginator object.

`$records` = records to be paginated.

`$adapter` = which adapter you want to use as string, `DoctrineDbal`, `Eloquent`, `Pixie`, `Array` and so. If omitted or a falsy value is given then Strana is smart enough to detect the adapter itself. If you want to use your own(custom) adapter then pass the object/instance here. Your custom adapter must implement ` Strana\Interfaces\CollectionAdapter` interface.

`$config` = all Strana config can be passed here too, as an array. like `$config = array('maximumPages' => 7)`

The Recordset Object
--------------------

[](#the-recordset-object)

`make()` method returns an instance of `Strana\RecordSet` class. Its a **polymorphic** object, for example:

```
$paginator = $strana->make($records);
```

Here, if you loop `$paginator` with `foreach` then it will work like an array and iterate through paginated items, if you `echo` `$paginator` it will work like a string and print the pagination links. And of course you can use like a class object. `$paginator->records()` will return paginated records. `$paginator->total()` will return total records count. `$paginator->links()` will return pagination links.

Developing Your Own Adapter
---------------------------

[](#developing-your-own-adapter)

If you are not using the database tool whose adapter ships with Strana then you can build your own adapter with ease. Create your class which must implement `Strana\Interfaces\CollectionAdapter`.

Example adapter, please read comments to understand.

```
