How to do Pagination for JSON data in PHP?

20,436

Solution 1

If you want to really just do it in PHP you can just roll up your own. But I strongly suggest using jquery plugins to do the pagination to make it easier. That being said, if you want to roll up your own you could do something like this (lil bit messy but something like this.) Consider this example:

$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>

Just a sample

Solution 2

This is a working code based on your example

<?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>
Share:
20,436
Smokey
Author by

Smokey

A software professional with 5.3 years of diverse experience in Software Development.

Updated on May 29, 2020

Comments

  • Smokey
    Smokey almost 4 years

    I don't have direct access to database from PHP. if it was so, I could have done pagination simply. Here I send a GET request to a PHP web service, and the result from the database is fetched as JSON. And I have a table and I want to show the database values into it. Since database table contains more than 1000 records, I want to show the data paginated.

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

    The above given is the GET request and the corresponding JSON is stored in $array.

    How can I do pagination to a JSON array using PHP??

    My JSON data is like this:

    {
        "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"
                }
    
            }
        ]
    }
    

    And below given is my HTML table

     <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>