以下代码正在进行正常的分页。我想了解如何变成Ajax的分页。使用PHP OO时,我的头脑仍然感到困惑
如何在分页链接中使用ajax? ".$self."?page_no=".$next."
以下代码位于表格末尾
的index.php
<?php
$query = "SELECT * FROM users ORDER BY id DESC";
$records_per_page=7;
$newquery = $crud->paging($query,$records_per_page);
$crud->dataview($newquery);
?>
<tr>
<td colspan="7" align="center">
<nav aria-label="Page navigation example">
<?php $crud->paginglink($query,$records_per_page); ?>
</nav>
</td>
</tr>
class.crud.php
public function paginglink($query,$records_per_page)
{
$self = $_SERVER['PHP_SELF'];
$stmt = $this->db->prepare($query);
$stmt->execute();
$total_no_of_records = $stmt->rowCount();
if($total_no_of_records > 0)
{
?><ul class="pagination"><?php
$total_no_of_pages=ceil($total_no_of_records/$records_per_page);
$current_page=1;
if(isset($_GET["page_no"]))
{
$current_page=$_GET["page_no"];
}
if($current_page!=1)
{
$previous =$current_page-1;
echo "<li class='page-item'><a class='page-link' href='".$self."?page_no=1'>First</a></li>";
echo "<li class='page-item'><a class='page-link' href='".$self."?page_no=".$previous."'>Back</a></li>";
}
for($i=1;$i<=$total_no_of_pages;$i++)
{
if($i==$current_page)
{
echo "<li class='page-item'><a class='page-link' href='".$self."?page_no=".$i."' style='color:red;'>".$i."</a></li>";
}
else
{
echo "<li class='page-item'><a class='page-link' href='".$self."?page_no=".$i."'>".$i."</a></li>";
}
}
if($current_page!=$total_no_of_pages)
{
$next=$current_page+1;
echo "<li class='page-item'><a class='page-link' href='".$self."?page_no=".$next."'>Next</a></li>";
echo "<li class='page-item'><a class='page-link' href='".$self."?page_no=".$total_no_of_pages."'>Last</a></li>";
}
?></ul><?php
}
}
我不知道如何在标题中填写ajax
<script>
function () { $.ajax({
url: url,
type: "GET",
data: ,
success: ,
error:
});
}
</script>
--- --- UPDATE
我正在学习ajax如何运作,在我已经能够发展的帮助下
我有两个引导列,左边是注册表单,右边是已经注册的列表,其中包含分页。当我点击分页页面(我将id放在<table #content-of-table>
标签中的表格标签中)整个页面在内部重复,然后分页正在运行,但整个屏幕内容出现在表格标签中。 / p>
我从href
获取了网址并输入了data-href
"<a href='#' class='page-link' data-href='".$self."?page_no=".$previous."'>Back</a>";
Jquery的
$('.page-link').click(function (e) {
e.preventDefault();
var url = $(this).data('href');
$.ajax({
url: url,
success: function (response) {
$('#content-of-page').html(response)
}
});
})
答案 0 :(得分:0)
从哪里开始。
严格来说,这与OOP本身没什么关系,而且只是看似困难。
通常使用的ajax是允许客户端与服务器端交互,“通常”,一旦页面呈现在客户端,它就不能。
在实践中,这主要归结为能够通过使用ajax访问资源(如数据库),然后更新页面内容而无需页面刷新,如您所知。
让我们看一下php:
<?php
//file somephpfile.php
$somevalueyouwillget = $_POST["somevaryouwanttopass"];
echo $somevalueyouwillget;
?>
和一些html:
<!-- since we use it below, your include of jquery - but if you do it with js it is not necessary -->
<div id="somepage">
Some text that was rendered normally on landing.
<div id="thecontentsIwanttoupdatewithajax">
Some contents rendered on landing.
</div>
<button onclick="updateallthethings();">A button that updates stuff</button>
</div>
最后是什么使它全部工作,一点javascript或jquery。我自己喜欢使用$ .post(只是一个例子,你应该处理成功,失败等等):
function updateallthethings() {
$.post('somephpfile.php', { somevaryouwanttopass: 'duuuude', orsomearrayinstead: { varone : valone, vartwo: valtwo } }, function(out) {
console.log(out); //debugging saves millions of lives every year
//"out" is ANYTHING that was rendered on the php side of things.
//for our example we use that to update the contents of our div - in your case, to replace page 1 with page 2.
$("#thecontentsIwanttoupdatewithajax").html(out);
});
}
结果将是
<div id="thecontentsIwanttoupdatewithajax">
duuuude
</div>
简而言之。它有意义吗?