如何在没有数据库的情况下创建AJAX分页?

Pow*_*ser 8 javascript php ajax jquery pagination

没有MySQL的帮助,是否可以获取AJAX分页的页面?我不能只添加一个PHP文件,其中包含我需要显示的文本和标记,并通过单击页码向用户提供该内容吗?那么这可以通过纯jQuery和PHP来实现吗?你会采用什么样的代码来保持简单,我的意思是我需要在我的网站上添加3个文本墙,但在页面上一次添加它们会让用户感到困惑.因此,我决定找到一种方法,并根据用户的决定一次只保留其中一种.一个代码示例会很棒!

编辑: PHP代码应该是这样的吗?

<?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';
?>
Run Code Online (Sandbox Code Playgroud)

Pra*_*oni 4

是的你可以。您必须遵守此处的分页规则。您需要在 url 中传递页码和记录限制。

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

现在,您创建一个文本或 php 文件,并使用 var_export 导出其中的所有数据以返回数组或您喜欢的其他方式。

现在你必须获取页码,技巧就在这里。

从文本文件中获得的数组应按限制划分。

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

您的第 = $n 页的结果就在这里。 $result = $row[$_GET['page']]

这是我的index.php 文件。

<?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>
Run Code Online (Sandbox Code Playgroud)

这是 data.php 文件。我已经使用数组来返回值。

    <?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'
    ],
];
Run Code Online (Sandbox Code Playgroud)