是否可以在不借助 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';
?>
最佳答案
是的,你可以。您必须在此处遵循分页规则。您需要在 url 中传递页码和记录限制。
www.example.com/index.php?page=1&limit=10
现在您创建一个文本或 php 文件并使用 var_export 导出所有数据以返回数组或您喜欢的其他方式。
现在您必须获取页码,技巧就在这里。
你从文本文件中得到的数组应该被限制分割。
$rows = array_chunk($array,$_GET['limit'])
;
page = $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>
这里是 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'
],
];
关于javascript - 如何在没有数据库的情况下创建 AJAX 分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31265095/