从api.php获取数据后,使用JQuery进行分页
Make a pagination using JQuery after getting data from api.php
我的代码-
client.php
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
#show{
background:red;
}
</style>
</head>
<body>
<?php
<div id="show"></div>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
setInterval(function () {
$('#show').load('api.php')
});
});
</script>
</body>
</html>
api.php
<?php
$conn = new mysqli('localhost', 'root', '', 'ajax01');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
$result = $conn->query("SELECT name FROM variables");
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo $row['name'] . '<br>';
}
}
?>
这些代码给我的结果是-
以上代码的结果
我正在从数据库中获取值,它正在获取所有数据。因此,我需要一个具有这些值的分页。需要帮助。
根据你的评论,
我想要一个分页,将只显示一个值,下一页将显示另一个…点击next>
后显示第二个结果
这里有几件事你需要考虑,
- 代替
setInterval()
和load()
函数,只需使用AJAX请求来实现分页功能 - 使用预处理语句,因为这将帮助您防止SQL注入。另外,请阅读如何在PHP中防止SQL注入。
基于以上几点和你下面的评论,解决方案应该是这样的:
client.php:
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
#show{
background:red;
}
</style>
</head>
<body>
<div id="show">
<?php
$conn = new mysqli('localhost', 'root', '', 'ajax01');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
// prepare query statement
if($stmt = $conn->prepare("SELECT name FROM variables LIMIT 0, 1")){
// execute statement
$stmt->execute();
// bind result variables
$stmt->bind_result($name);
// fetch values
$stmt->fetch();
// display name and pagination link
if(isset($name) && !empty($name)){
echo $name . '<br />';
?>
<div id='link-div' style='background-color:#ffffff'>
<a href='' class='showmore' id='1'>Next »</a>
</div>
<?php
}
// close statement
$stmt->close();
}
?>
</div>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(document).on('click','.showmore',function(event){
event.preventDefault();
var offset = $(this).attr('id');
$.ajax({
type: 'POST',
url: 'api.php',
cache: 'false',
data: {offset: offset},
beforeSend: function(){
$('#link-div').html('<span>Loading...</span>');
},
success: function(data){
$('#link-div').remove();
$('#show').html(data);
},
error: function(jqXHR, textStatus, errorThrown){
// error
}
});
});
});
</script>
</body>
</html>
api.php:
<?php
if(isset($_POST['offset'])){
$offset = $_POST['offset'];
$prev = $offset - 1; // Previous link in the pagination series
$next = $offset + 1; // Next link in the pagination series
$conn = new mysqli('localhost', 'root', '', 'ajax01');
if ($conn->connect_error) {
die("Connection error: " . $conn->connect_error);
}
// prepare query statement
if($stmt = $conn->prepare("SELECT COUNT(name) FROM variables")){
// execute statement
$stmt->execute();
// bind result variables
$stmt->bind_result($total_rows);
// fetch values
$stmt->fetch();
// close statement
$stmt->close();
}
// prepare query statement
if($stmt = $conn->prepare("SELECT name FROM variables LIMIT ?, 1")){
// bind parameter
$stmt->bind_param('i', $offset);
// execute statement
$stmt->execute();
// bind result variables
$stmt->bind_result($name);
// fetch values
$stmt->fetch();
// display name and pagination link
if(isset($name) && !empty($name)){
echo $name . '<br />';
?>
<div id='link-div' style='background-color:#ffffff'>
<?php
if($offset > 0){
?>
<a href='' class='showmore' id='<?php echo $prev; ?>'>«Prev </a>
<?php
}
if($offset < $total_rows - 1){
?>
<a href='' class='showmore' id='<?php echo $next; ?>'>Next »</a>
<?php
}
?>
</div>
<?php
}
// close statement
$stmt->close();
}
}
?>
相关文章:
- jQuery使用anoSlide在手机上滑动
- jQuery使用api获取typeform结果
- JQuery使用相同的功能自动完成各种输入文本
- jQuery:使用“jQuery验证”后无法提交表单's远程验证
- jQuery使用ajax自动完成问题-TypeError:this.source不是函数
- jQuery:使用substr()的另一种方法
- 我如何让jQuery使用Greasemonkey 0.8脚本(在Firefox 2上),在没有互联网的情况下使用PC
- jquery使用name from变量按类选择元素
- JQuery:使用clone()生成的元素不采用原始的事件属性
- jQuery使用$.post上传文件
- 如何将数据附加到DOM元素以供jQuery使用
- jquery使用val()复制值似乎可以删除换行符
- jQuery使用.of()删除事件处理程序时出现的问题
- jquery使用输入框内容
- jQuery:使用实时事件添加tabindex属性
- Jquery使用html对象添加更多内容
- jQuery 使用 .scroll(handler) 滚动到元素
- PHP jQuery使用多个单词传递值
- 防止已选择的选项 Jquery 使用 MYSQL 自动完成
- jQuery使用XML数据更新UL列表