我试图使用 jquery、mySql 和 PHP 使用分页显示每页 3 个用户,因此我遵循此页面中的一些教程:
http://php-dev-zone.blogspot.com/2013/09/simple-pagination-using-jquerymysql-and.html
我有两个错误
第一个错误是我的平台不支持Mysql_query,所以我将其更改为Mysqli_query,所以我认为它已修复
第二个错误是页面只显示页码,没有用户信息!?
谁能帮帮我
这是一张显示页面情况的图像:
http://store1.up-00.com/2014-02/1391845579081.jpg
这些是我将它们更改为 Mysqli_query 后的文件
配置.php
<?php
$db_username = '****';
$db_password = '*****';
$db_name = '*****';
$db_host = '********';
$item_per_page = 3;
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name)or die('could not connect to database');
?>
数据.php
<?php
include('config.php');
$per_page = 3;
if($_GET)
{
$page=$_GET['page'];
}
//getting table contents
$start = ($page-1)*$per_page;
$sql = "select * from users order by id limit $start,$per_page";
$rsd = mysqli_query($sql);
?>
<table id="tb1">
<th><b>Id</b></th>
<th><b>FirstName</b></th>
<th><b>Email</b></th>
<th><b>LastName</b></th>
<?php
while($row = mysqli_fetch_array($rsd))
{
$id = $row['id'];
$fname = $row['first_name'];
$mname = $row['email'];
$lname = $row['last_name'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $mname; ?></td>
<td><?php echo $lname; ?></td>
</tr>
<?php
} //End while
?>
</table>
<style>
#tbl
{
width:800px;
border:1px solid #000000;
margin-top:50px;
}
#tbl tr:nth-child(odd) {
background: #000000
}
#tbl td{
border:1px solid #000000
}
#tbl th
{
background: #00000;
border:1px solid #000000
}
</style>
index.php
<?php
include('config.php');
$per_page = 3;
//getting number of rows and calculating no of pages
$sql = "select count(*) from users";
$result = mysqli_query($connecDB,$sql);
$count = mysqli_fetch_row($result);
$pages = ceil($count[0]/$per_page);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>PHPDevZone : Pagination Using Mysql and jQuery </title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript" src="http://yourjavascript.com/2141420575/pagination.js"></script>
<style>
body { margin: 0; padding: 0; font-family:Verdana; font-size:15px }
a
{
text-decoration:none;
color:#000000;
}
a:hover
{
color:#DF3D82;
text-decoration:underline;
}
#loading {
width: 100%;
position: absolute;
}
#pagination
{
text-align:center;
margin-left:120px;
}
li{
list-style: none;
float: left;
margin-right: 16px;
padding:5px;
color:#0063DC;
}
li:hover
{
color:#FF0084;
cursor: pointer;
}
</style>
</head>
<body>
<div align="center">
<div style="margin-top:300px;"><b><a href="http://php-dev-zone.blogspot.com" > PHPDevZone </a></b>: Pagination Demo</div>
<div id="content" ></div>
<table width="800px">
<tr><Td>
<ul id="pagination">
<?php
//Show page links
for($i=1; $i<=$pages; $i++)
{
echo '<li id="'.$i.'">'.$i.'</li>';
}
?>
</ul>
</Td>
</tr></table>
<div id="loading" ></div>
</div>
</body>
</html>
分页.js
$(document).ready(function(){
//Loading Image Display
function Display_Load()
{
$("#loading").fadeIn(100);
$("#loading").html("<img src='loading.gif' />");
}
//Hide Loading Image
function Hide_Load()
{
$("#loading").fadeOut('slow');
};
//Default Starting Page Results
$("#pagination li:first").css({'color' : '#FF0084','border' : 'none'});
$("#content").load("data.php?page=1", Hide_Load());
//Pagination Click
$("#pagination li").click(function(){
Display_Load();
//CSS Styles
$("#pagination li")
.css({'border' : 'solid #dddddd 1px'})
.css({'color' : '#0063DC'});
$(this)
.css({'color' : '#FF0084'})
.css({'border' : 'none'});
//Loading Data
var pageNum = this.id;
$("#content").load("data.php?page=" + pageNum, Hide_Load());
});
});
谢谢
最佳答案
问题出在 Data.php 页面中的 mysqli_query 上。 mysqli_query 首先需要一个数据库链接,然后进行查询,因此您的代码必须如下所示才能使其正常工作:
<?php
include ('config.php');
$per_page = 3;
if ($_GET) {
$page = $_GET['page'];
}
//getting table contents
$start = ($page - 1) * $per_page;
$sql = "select * from users order by id limit $start,$per_page";
$rsd = mysqli_query($connecDB,$sql);
?>
<table id="tb1">
<th><b>Id</b></th>
<th><b>FirstName</b></th>
<th><b>Email</b></th>
<th><b>LastName</b></th>
<?php
while($row = mysqli_fetch_array($rsd))
{
$id = $row['id'];
$fname = $row['first_name'];
$mname = $row['email'];
$lname = $row['last_name'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $fname; ?></td>
<td><?php echo $mname; ?></td>
<td><?php echo $lname; ?></td>
</tr>
<?php } //End while ?>
</table>
<style>
#tbl {
width: 800px;
border: 1px solid #000000;
margin-top: 50px;
}
#tbl tr:nth-child(odd) {
background: #000000
}
#tbl td {
border: 1px solid #000000
}
#tbl th {
background: #00000;
border: 1px solid #000000
}
</style>
关于javascript - Ajax 出错,每页显示 3 个用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21643488/