Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create AJAX pagination without a database?

Is it possible to fetch pages for AJAX pagination without the help of MySQL? Can't I just add a PHP file with the text and markup I need to have displayed and by clicking page numbers serve that content to the user? So can this be achieved with pure jQuery and PHP? What code approach would you do to keep things simple, I mean I have 3 walls of text I need to add to my site but adding them at once on the page would confuse the users. So I decided to find a way and keep only one of them at a time based on the user's decision. A code example would be great!

EDIT: Should the PHP code look something like this?

<?php
    htmlspecialchars($_GET["page_number"]);
    if ($page_number == 1)
    {
        $text = var_export($text1, true);
    }

    if ($page_numer == 2)
    {
        $text = var_export($text2, true);
    }

    if ($page_number == 3)
    {
        $text = var_export($text3, true);
    }

    $text1 = 'some looong text...';
    $text2 = 'another wall of text';
    $text3 = 'and yet another one';
?>
like image 898
PowerUser Avatar asked Jul 07 '15 09:07

PowerUser


1 Answers

Yes you can. You have to follow pagination rules here. You will need to pass the page number and limit of records in url.

www.example.com/index.php?page=1&limit=10

Now you create a text or php file and export all data there using var_export to return array or some other way you like.

Now you have to get page number and the trick goes here.

The array you got from text file should be divided in by limit.

$rows = array_chunk($array,$_GET['limit']);

Your result for page = $n is here. $result = $row[$_GET['page']]

here is my index.php file.

<?php
$array = include_once('data.php');
$page = $_GET['page'] ? $_GET['page'] : 0;
$limit = $_GET['limit'];
$rows = array_chunk($array,$limit);
$result = $rows[$page];
?>
<table>
    <tr>
        <th>Id</th>
        <th>Name</th>
        <th>Desc</th>
        <th>Status</th>
    </tr>
    <?php
        foreach ($result as $res) {
    ?>
        <tr>
            <td><?php echo $res['id'] ?></td>
            <td><?php echo $res['name'] ?></td>
            <td><?php echo $res['desc'] ?></td>
            <td><?php echo $res['status'] ?></td>
        </tr>
    <?php
        }
    ?>
    <tr>
        <td colspan="2">
            <?php
                if($page>0)
                {
            ?>
            <a href="index.php?limit=<?php echo $limit; ?>&page=<?php echo ($page-1); ?>">Previous</a>
            <?php
                }
            ?>
        </td>
        <td colspan="2">
        <?php
            if (isset($rows[$page+1]))
            {
        ?>
            <a href="index.php?limit=<?php echo $limit; ?>&page=<?php echo ($page+1); ?>">Next</a>
        <?php
            }
        ?>
        </td>
    </tr>
</table>

And here is the data.php file. i have used the array to return the value.

    <?php
return
$array = [
    [
        'id' => 1,
        'name' => 'A',
        'desc' => 'Test',
        'status' => 'Active'
    ],
    [
        'id' => 2,
        'name' => 'B',
        'desc' => 'Test',
        'status' => 'Active'
    ],
    [
        'id' => 3,
        'name' => 'C',
        'desc' => 'Test',
        'status' => 'Active'
    ],
    [
        'id' => 4,
        'name' => 'D',
        'desc' => 'Test',
        'status' => 'Active'
    ],
    [
        'id' => 5,
        'name' => 'E',
        'desc' => 'Test',
        'status' => 'Active'
    ],
    [
        'id' => 6,
        'name' => 'F',
        'desc' => 'Test',
        'status' => 'Active'
    ],
];
like image 112
Pratik Soni Avatar answered Oct 19 '22 03:10

Pratik Soni