0

I've developed an app using Sencha Touch which will fetch data using JSONP on my server.

Here is my Store in Sencha

config: {
storeId: 'actor',
autoLoad: true,
model: 'App.model.actor',
proxy:{
    type: 'jsonp',
    url: 'http://domain.com/actorjson.php?getActors',

    reader: {
        type: 'json',
        rootProperty: 'items.feed'
    },

},

And when ever the request made google chrome shows like this

http://domain.com/actorjson.php?getActors&_dc=1369408455693&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback95

So far everything looks fine, and App running very good.

My Question is: i've more than 1000 entries to return from PHP, in this case load all of them is useless, so i want to use PAGE functionality, I'm very new to PHP, Can you point me out PHP Script which will work with Pagination Any examples will be really helpful.

Thanks Pavan

Pavan
  • 99
  • 1
  • 12

1 Answers1

1

What you have to do on the server side is reading the limit parameter of the request as well as either start or page. start tells is the index of the first item to include, while page is the index of the page... As you see, by default, Ext send them both. start is easier to use with some databases like MySQL, because they use the same parameter.

In order to access request parameter send with method GET (i.e. in the URL), in PHP you can use a "magic" global named $_GET (there's also a $_POST variable, and a $_REQUEST one, in which both GET and POST params are available).

That being said, that's not possible to give you a server script example that will work with your Javascript without knowing anything about your data source and your data model...

Here the general principle:

<?php

// --- Source data ---

$records = array(
    array('id' => 1, 'name' => 'eric', 'age' => 30),
    array('id' => 2, 'name' => 'foo', 'age' => 99),
    array('id' => 3, 'name' => 'bar', 'age' => 18),
    // trailling commas in array are OK in PHP ;)

    // let's pretend there's 1000 of them
);
$nRecords = count($records);

// --- Read request params ---

$defaultLimit = 25;
// isset($variable) ensures that the variable exists in order to
// avoid a fatal error
$limit = isset($_GET['limit']) ? $_GET['limit'] : $defaultLimit;
$start = isset($_GET['start']) ? $_GET['start'] : 0;

// --- Construct returned item array ---

$items = array();
for ($i = 0; $i < $limit; $i++) {
    $index = $start + $i;

    // leave the loop if we've reached the end of the source array
    if ($index >= $nRecords) {
       break; 
    }
    // or push the record in the item array
    else {
        $items[] = $records[$index];
    }
}

// --- Construct and send the response ---

$responseData = array(
    'total' => count($items),
    // using the root property you've configured
    'items.feed' => $items,
);

// Encode response data to JSON
$jsonData = json_encode($responseData);

// If a callback is specified, the request has been made by a
// JsonP proxy
if (isset($_REQUEST['callback'])) {
    $callback = $_REQUEST['callback'];
    // HTTP response header
    header('Content-Type: text/javascript');
    // Encode to JSON
    // string concatenation is done with dots in PHP
    echo $callback . '(' . $jsonData . ')';
}
// Else we'll return raw json
else {
    // HTTP response header
    header('Content-Type: application/x-json');
    // Encode to JSON and write to output
    echo $jsonData;
}

// --- We're done ---

Generally, the start and limit params are applied at the DB engine level, in order to save memory on the server side too, and avoid useless processing... I hope you'll be able to adapt from here.

Good luck!

rixo
  • 23,815
  • 4
  • 63
  • 68