0

我使用 Sencha Touch 开发了一个应用程序,它将在我的服务器上使用 JSONP 获取数据。

这是我在煎茶的商店

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'
    },

},

每当请求使谷歌浏览器像这样显示

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

到目前为止,一切看起来都很好,并且应用程序运行得非常好。

我的问题是:我有超过 1000 个条目要从 PHP 返回,在这种情况下,加载它们都是无用的,所以我想使用 PAGE 功能,我对 PHP 很陌生,你能指出我的 PHP 脚本吗将与分页一起使用任何示例都会非常有帮助。

谢谢帕万

4

1 回答 1

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!

于 2013-05-27T20:39:05.033 回答