这是我的分页功能......
<?php
include('connection.php');
function getPagingNav($sql, $pageNum, $rowsPerPage, $queryString = '')
{
$result = mysqli_query($conn,$sql) or die('Error, query failed. ' . mysqli_error());
$row = mysqli_fetch_array($result, MYSQLi_ASSOC());
$numrows = $row['numrows'];
$maxPage = ceil($numrows/$rowsPerPage);
if ($_SERVER['SERVER_PORT']!=443)
{
$self = 'http://' . $_SERVER['PHP_SELF'] ;
}
else{
$self = 'https://' . $_SERVER['PHP_SELF'] ;
}
if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href=\"$self?pagenum=$page{$queryString}\">[Prev]</a> ";
$first = " <a href=\"$self?pagenum=1{$queryString}\">[First Page]</a> ";
}
else{
$prev = ' [Prev] ';
$first = ' [First Page] ';
}
if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href=\"$self?pagenum=$page{$queryString}\">[Next]</a> ";
$last = " <a href=\"$self?pagenum=$maxPage{$queryString}{$queryString}\">[Last Page]</a> ";
}
else{
$next = ' [Next] ';
$last = ' [Last Page] ';
}
return $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
}
function getPagingQuery($sql, $itemPerPage = 10)
{
if (isset($_GET['pagenum']) && (int)$_GET['pagenum'] > 0)
{
$page = (int)$_GET['pagenum'];
}
else{
$page = 1;
}
$offset = ($page - 1) * $itemPerPage;
return $sql . " LIMIT $offset, $itemPerPage";
}
function getPagingLink($sql, $itemPerPage = 10, $strGet = '')
{
$result = mysqli_query($conn,$sql);
$pagingLink = '';
$totalResults = mysqli_num_rows($result);
$totalPages = ceil($totalResults / $itemPerPage);
$numLinks = 10;
if ($totalPages > 1)
{
if ($_SERVER['SERVER_PORT']!=443)
{
$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
}
else{
$self = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;
}
if (isset($_GET['pagenum']) && (int)$_GET['pagenum'] > 0)
{
$pageNumber = (int)$_GET['pagenum'];
}
else{
$pageNumber = 1;
}
if ($pageNumber > 1)
{
$page = $pageNumber - 1;
if ($page > 1)
{
$prev = " <a href=\"$self?pagenum=$page&$strGet/\">[Prev]</a> ";
}
else{
$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
}
$first = " <a href=\"$self?$strGet\">[First]</a> ";
}
else{
$prev = '';
$first = '';
}
if ($pageNumber < $totalPages)
{
$page = $pageNumber + 1;
$next = " <a href=\"$self?pagenum=$page&$strGet\">[Next]</a> ";
$last = " <a href=\"$self?pagenum=$totalPages&$strGet\">[Last]</a> ";
}
else{
$next = '';
$last = '';
}
$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;
$end = min($totalPages, $end);
$pagingLink = array();
for($page = $start; $page <= $end; $page++)
{
if ($page == $pageNumber)
{
$pagingLink[] = " $page ";
}
else{
if ($page == 1)
{
$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
}
else{
$pagingLink[] = " <a href=\"$self?pagenum=$page&$strGet\">$page</a> ";
}
}
}
$pagingLink = implode(' | ', $pagingLink);
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}
return $pagingLink;
}
?>
这是我调用该函数的view.php文件。
<html>
<table border="1" align="center">
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
<td>Password</td>
<td>Gender</td>
<td>Mobile No</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
include('connection.php');
include('Function/Pagging.php');
$ins = "select * from reg";
$result=mysqli_query($conn,$ins);
$rowperpage=3;
$result=mysqli_query(getPagingQuery($result,$rowperpage));
$noofrec=mysqli_num_raws($result);
$Pagginglink=getPaggingLink($ins,$rowperpage,'');
if ($noofrec==0) {
echo "<tr><td align=center colspan=9> NO record Found </td></tr>";
}
else{
while ($row=mysqli_fetch_array($result)) {
$id= $row['id'];
$name= $row['name'];
$email= $row['email'];
$password= $row['password'];
$gender= $row['gender'];
$mobile= $row['mobile'];
?>
<tr>
<td> <?php echo $id; ?> </td>
<td> <?php echo $name; ?> </td>
<td> <?php echo $email; ?> </td>
<td> <?php echo $password; ?> </td>
<td> <?php echo $gender; ?> </td>
<td> <?php echo $mobile; ?> </td>
<td><a href="Edit.php?id=<?php echo $id; ?>">Edit </a> </td>
<td><a href="Delete.php?id=<?php echo $id; ?>">Delete </a> </td>
</tr>
<?php } ?>
<tr><td align="center" colspan="9"><?php echo $Pagginglink; ?></td></tr>
<?php } ?>
</table>
</html>
当我调用向我显示的分页函数时,对象无法在第52行转换为字符串。如何解决该类型的错误。我的第52行是这样的:返回$ sql。 “LIMIT $ offset,$ itemPerPage”;
答案 0 :(得分:0)
执行后无法修改查询:
...
$ins = "select * from reg";
$result=mysqli_query($conn,$ins);
$rowperpage=3;
$result=mysqli_query(getPagingQuery($result,$rowperpage));
^^^^^^^ you need the query string here, not the result
...
您需要将最后一行更改为(编辑:添加缺失参数):
$result=mysqli_query($conn, getPagingQuery($ins,$rowperpage));
^^^^ add pagination to the query string and then
execute it
然后:
$noofrec=mysqli_num_rows($result);
^ ummmmmm
答案 1 :(得分:0)
完全运行的查询的最终输出...
<html>
<head>
<style type="text/css">
a {
text-decoration:none;
}
</style>
</head>
<body>
<?php
ini_set('display_error', 1);
error_reporting(E_ALL);
include('connection.php');
$perpage=5;
if (isset($_GET['page'])) {
$page=$_GET['page'];
}
else {
$page=1;
}
$start_from=($page-1) * $perpage;
$query="select * from reg limit $start_from, $perpage";
$res=mysqli_query($conn,$query);
?>
<table align="center" border="2" cellpadding="2">
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
<td>Password</td>
<td>Gender</td>
<td>Mobile No</td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php
while ($row=mysqli_fetch_assoc($res)) {
?>
<tr align="center">
<td> <?php echo $row['id']; ?></td>
<td> <?php echo $row['name']; ?></td>
<td> <?php echo $row['email']; ?></td>
<td> <?php echo $row['password']; ?></td>
<td> <?php echo $row['gender']; ?></td>
<td> <?php echo $row['mobile']; ?></td>
<td><a href="Edit.php?id=<?php echo $row['id']; ?>">Edit </td>
<td><a href="Delete.php?id=<?php echo $row['id']; ?>">Delete </td>
</tr>
<?php } ?>
</table>
<div style="">
<?php
$query="select * from reg";
$res=mysqli_query($conn,$query);
$total_record=mysqli_num_rows($res);
$total_pages= ceil($total_record / $perpage);
echo "<center><a href='vie.php?page=1'> First Page </a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='vie.php?page=".$i."'>".$i."</a>";
}
echo "<a href='vie.php?page=$total_pages'> Last Page </a></center>";
?>
</div>
</body>
</html>