如何在PHP中对JSON数据进行分页


How to do Pagination for JSON data in PHP?

我没有从PHP直接访问数据库。如果是这样的话,我可以简单地进行分页。在这里,我向PHP web服务发送一个GET请求,并从数据库中获取JSON格式的结果。我有一个表,我想把数据库值显示进去。由于数据库表包含超过1000条记录,因此我想显示已分页的数据。

$json_data_fromdb= httpGet($ur3l."fromtable?u=".$var1."&ip=".$var2);
$array = json_decode($json_data_fromdb, true);
$x=count($array['qqq']);
$array = $array['qqq'];

上面给出的是GET请求,对应的JSON存储在$array中。

我怎么能做分页JSON数组使用PHP??

我的JSON数据是这样的:

{
    "qqq": [
        {
            "a": "Conne",
            "b": "1",
            "c": "2014-05-19T15:40:06+05:30",
            "d": {
                "d1": "dani",
                "d6": "admin"
            }
        },
        {
            "a": "igroup'",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d6": "eev"
            }
        }
    ]
}

下面是我的HTML表格

 <table id="show" >
        <thead >
        <tr >
            <th>1stheader</th>
            <th>2stheader</th>
            <th>3stheader</th>
            <th>4stheader</th>
            <th>5stheader</th>
        </tr>
        </thead>
        <tbody>
        <?php
        for($i=0; $i<$x; $i++)
        {
            $a= $array[$i]['a'];
            $b= $array[$i]['d']['d1'] ;
            $c= $array[$i]['d']['d2'] ;
            $d= $array[$i]['b'];
            $e= $array[$i]['c'];
            ?>
            <tr >
                <td><?php echo $a; ?></td>
                <td><?php echo $b  ?></td>
                <td><?php echo c;   ?></td>
                <td><?php echo d;   ?></td>
                <td><?php echo $e;  ?></td>
            </tr>
        <?php }  ?>
        </tbody>
    </table>

如果你真的想在PHP中做你可以卷起你自己的。但我强烈建议使用jquery插件来进行分页,以使其更容易。话虽如此,如果你想卷起你自己的,你可以这样做(有点乱,但像这样。)考虑这个例子:

$sample_data = '{ "qqq": [ { "a": "Conne", "b": "1", "c": "2014-05-19T15:40:06+05:30", "d": { "d1": "dani", "d6": "admin" } }, { "a": "test1", "b": "1235", "c": "2014-    05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test2", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test3", "b":     "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test4", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a":     "test5", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test6", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev"     } }, { "a": "test7", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test8", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev",     "d6": "eev" } }, { "a": "test9", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } }, { "a": "test10", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": {     "d1": "sev", "d6": "eev" } }, { "a": "test11", "b": "1235", "c": "2014-05-27T11:23:11+05:30", "d": { "d1": "sev", "d6": "eev" } } ]}';
// just normal getting data
$raw_data = json_decode($sample_data, true);
$raw_data = $raw_data['qqq'];
// use get variable to paging number
$page = !isset($_GET['page']) ? 1 : $_GET['page'];
$limit = 5; // five rows per page
$offset = ($page - 1) * $limit; // offset
$total_items = count($raw_data); // total items
$total_pages = ceil($total_items / $limit);
$final = array_splice($raw_data, $offset, $limit); // splice them according to offset and limit
?>
<!-- print links -->
<?php for($x = 1; $x <= $total_pages; $x++): ?>
    <a href='index.php?page=<?php echo $x; ?>'><?php echo $x; ?></a>
<?php endfor; ?>
<table border="1" cellpadding="10">
    <tr><th>Column 1</th><th>Column 2</th><th>Time</th><th>Column 4</th></tr>
    <?php foreach($final as $key => $value): ?>
        <tr>
        <?php foreach($value as $index => $element): ?>
            <td><?php echo !is_array($element) ? $element : implode(',', $element); ?></td>
        <?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
</table>

只是一个示例

这是基于您的示例

的工作代码
<?php
$json = '{
    "qqq": [
        {
            "a": "Conne",
            "b": "1",
            "c": "2014-05-19T15:40:06+05:30",
            "d": {
                "d1": "dani",
                "d2": {
                    "d2a": "1",
                    "d2b": "inmin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "1",
                "d4": "web",
                "d5": "8e11e4f63",
                "d6": "admin"
            },
            "e": "145"
        },
        {
            "a": "igroup",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup2",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup3",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup4",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup5",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup6",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup7",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup8",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        },
        {
            "a": "igroup9",
            "b": "1235",
            "c": "2014-05-27T11:23:11+05:30",
            "d": {
                "d1": "sev",
                "d2": {
                    "d2a": "1",
                    "d2b": "admin",
                    "d2c": "1",
                    "d2d": "1",
                    "d2e": "1",
                    "d2f": "1"
                },
                "d3": "7",
                "d4": "c",
                "d5": "changed",
                "d6": "eev"
            },
            "e": "132"
        }
    ]
}';
echo '<pre>';
$jsonarray = json_decode($json,true);
$page = !isset($_GET['page']) ? 1 : $_GET['page'];
$limit = 5; 
$offset = ($page - 1) * $limit;
$total_items = count($jsonarray['qqq']); 
$total_pages = ceil($total_items / $limit);
$array = array_splice($jsonarray['qqq'], $offset, $limit);
for($j=1;$j<=$total_pages;$j++) {
    echo "<span><a href='test.php?page=$j'>$j</a></span>";
}
?>
<table id="show" >
        <thead >
        <tr >
            <th>1stheader</th>
            <th>2stheader</th>
            <th>3stheader</th>
            <th>4stheader</th>
            <th>5stheader</th>
        </tr>
        </thead>
        <tbody>
        <?php
        for($i=0; $i<5; $i++)
        {
            $a= $array[$i]['a'];
            $b= $array[$i]['d']['d1'] ;
            $c= $array[$i]['d']['d2']['d2a'] ;
            $d= $array[$i]['b'];
            $e= $array[$i]['c'];
            ?>
            <tr >
                <td><?php echo $a; ?></td>
                <td><?php echo $b  ?></td>
                <td><?php echo $c;   ?></td>
                <td><?php echo $d;   ?></td>
                <td><?php echo $e;  ?></td>
            </tr>
        <?php }  ?>
        </tbody>
    </table>

你有几个可用的选择,我建议最好的两个是:

PHP解决方案:

使用会话,(也就是把session_start();放在每个需要访问会话变量的php文件之前,也添加一个脚本来导致会话超时)

如果未设置会话变量,则将DB输出存储到会话变量中。

if (!isset($_SESSION['tabledata'])) {
    $json_data_fromdb= httpGet($ur3l."fromtable?u=".$var1."&ip=".$var2);
    $array = json_decode($json_data_fromdb, true);
    $_SESSION['count']=count($array['qqq']);
    $_SESSION['tabledata'] = $array['qqq'];
}

然后,您可以通过发送对特定范围的值的请求来简单地在数据中分页。

或者,您可以使用像datatable这样的javascript解决方案要么加载所有的信息,让数据表过滤,要么让数据表直接与其他PHP服务器通信。

我将给你一个小的例子,希望能帮助你:

jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){
        //init pagenation
             var page =  $("#page option:selected").val();
             $.each($("tr"), function(key, value) {
                    if($(this).attr("id") != page){
                        $(this).css("display", "none");
                    }else{
                        $(this).css("display", "block");
                    }
                });
        //do pagenation          
            $("#page").change(function(){
                 var page =  $("#page option:selected").val();
                 $.each($("tr"), function(key, value) {
                        if($(this).attr("id") != page){
                            $(this).css("display", "none");
                        }else{
                            $(this).css("display", "block");
                        }
                    }); 
            });
         });                
</script>
html + php:

<?php
    $myArr = array('employee-1', 'employee-2', 'employee-3', 'employee-4', 'employee-5');
    $perPage = 2;//The number of items per page
    $output = '<table>';
    $counter = 0;
    $option = 1;// page ids
    foreach($myArr as $name){
        $output .= "<tr id='$option'><td>$name</td></tr>";
        $counter++;
        if($counter == $perPage) {$option++; $counter = 0;}
    }
    $output .= '</table>';
    $output .= "<select id='page'>";
    for($i=1; $i <= $option; $i++){
        $output .= "<option value='$i'>$i</option>";
    }
    echo $output; 
?>

但是对于你的代码:

jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
    $(function(){
        //init pagenation
             var page =  $("#page option:selected").val();
             $.each($(".myTr"), function(key, value) {
                var idInLoop = $(this).attr("id");
                    if(idInLoop != page){
                        $(this).css("display", "none");
                    }else{
                        $(this).css("display", "block");
                    }
                });
        //do pagenation          
            $("#page").change(function(){
                 var page =  $("#page option:selected").val();
                 $.each($(".myTr"), function(key, value) {
                    var idInLoop = $(this).attr("id");
                        if(idInLoop != page){
                            $(this).css("display", "none");
                        }else{
                            $(this).css("display", "block");
                        }
                    });
            });
         });                
</script>
php + html:

<?php
    $output = ' <table id="show" >
                    <thead >
                    <tr id = "header">
                        <th>1stheader</th>
                        <th>2stheader</th>
                        <th>3stheader</th>
                        <th>4stheader</th>
                        <th>5stheader</th>
                    </tr>
                    </thead>
                    <tbody>';
    $perPage = 2;//The number of items per page
    $counter = 0;
    $option = 1;// page ids
    for($i=0; $i<$x; $i++){
        $a= $array[$i]['a'];
        $b= $array[$i]['d']['d1'] ;
        $c= $array[$i]['d']['d2'] ;
        $d= $array[$i]['b'];
        $e= $array[$i]['c'];
        $output .= "<tr class='myTr' id='$option' >";
            $output .= "<td>$a</td>";
            $output .= "<td>$b</td>";
            $output .= "<td>$c</td>";
            $output .= "<td>$d</td>";
            $output .= "<td>$e</td>";
        $output .= "</tr>";
        $counter++;
        if($counter == $perPage) {$option++; $counter = 0;}
    }
    $output .= "</tbody></table>";  
    $output .= "<select id='page'>";
    for($i=1; $i <= $option; $i++){
        $output .= "<option value='$i'>$i</option>";
    }
    echo $output;    
?>