如何创建Ajax分页没有数据库?分页、数据库、Ajax

2023-09-11 01:01:02 作者:動我女神全撂倒

是否有可能获取对AJAX分页页面没有的MySQL的帮助?我不能只是添加有我需要的显示,并通过点击页码的文字和标记一个PHP文件服务的内容给用户?因此,可以这样用纯粹的jQuery和PHP实现?你会做什么code办法让事情变得简单,我的意思是我有文字的三面墙体我需要添加到我的网站,但网页上添加一次他们会混淆用户。所以我决定找到一种方法,只有其中一人守在根据用户的决定的时候。一个code例子是伟大的!

编辑: 如果PHP code是这个样子?

 < PHP
    用htmlspecialchars($ _ GET [PAGE_NUMBER]);
    如果($ PAGE_NUMBER == 1)
    {
        $文字= var_export($文本1,真正的);
    }

    如果($ page_numer == 2)
    {
        $文字= var_export($文本2,真正的);
    }

    如果($ PAGE_NUMBER == 3)
    {
        $文字= var_export($文本3,​​真正的);
    }

    $的text1 ='一些looong文字......;
    $文本2 ='另一面墙上的文字;
    $文本3 =',但另一个;
?>
 

解决方案

当然可以。你必须在这里遵循分页规则。你将需要传递的URL记录的页码和限制。

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

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

想使用VB中实例的代码,但是这个连接到了数据库,我没有建立数据库

现在你必须得到页码和诀窍放在这里。

您得到的数组从文本文件应以限制被分为

$行= array_chunk($数组,$ _ GET ['限制']);

您结果页= $ n是在这里。 $结果= $行[$ _ GET [页]]

这是我的index.php文件。

 < PHP
$阵列= include_once('data.php');
$页= $ _GET ['页']? $ _GET ['页']:0;
$上限= $ _GET ['限制'];
$行= array_chunk($数组,$限制);
$结果= $行[$页]
?>
<表>
    &其中; TR>
        百分位> ID和LT; /第i个
        百分位>名称< /第i个
        百分位>说明< /第i个
        <第i个状态和LT; /第i个
    < / TR>
    < PHP
        的foreach($结果为$水库){
    ?>
        &其中; TR>
            < TD><?PHP的echo $水库['身份证']>< / TD>
            < TD><?PHP的echo $水库['名称']>< / TD>
            < TD><?PHP的echo $水库['说明']>< / TD>
            < TD><?PHP的echo $水库['状态']>< / TD>
        < / TR>
    < PHP
        }
    ?>
    &其中; TR>
        &所述; TD列跨度=2>
            < PHP
                如果($页大于0)
                {
            ?>
            < A HREF =的index.php上限=<?PHP的echo $限制;?>&安培;网页=< PHP的echo($页面-1);?>> previous< / A>
            < PHP
                }
            ?>
        < / TD>
        &所述; TD列跨度=2>
        < PHP
            如果(使用isset($行[$页+ 1]))
            {
        ?>
            < A HREF =的index.php上限=<?PHP的echo $限制;?>&安培;网页=< PHP的echo($页+ 1);?>>接着< / A>
        < PHP
            }
        ?>
        < / TD>
    < / TR>
< /表>
 

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

 < PHP
返回
$阵列= [
    [
        'ID'=> 1,
        '名'=> '一个',
        '说明'=> '测试',
        状态=> '活性'
    ]
    [
        'ID'=> 2,
        '名'=> B,
        '说明'=> '测试',
        状态=> '活性'
    ]
    [
        'ID'=> 3,
        '名'=> 'C',
        '说明'=> '测试',
        状态=> '活性'
    ]
    [
        'ID'=> 4,
        '名'=> 'D',
        '说明'=> '测试',
        状态=> '活性'
    ]
    [
        'ID'=> 5,
        '名'=> E,
        '说明'=> '测试',
        状态=> '活性'
    ]
    [
        'ID'=> 6,
        '名'=> 'F',
        '说明'=> '测试',
        状态=> '活性'
    ]
]。
 

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';
?>

解决方案

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'
    ],
];