分页以使用php显示10项
我的数据库中有100000多个数据,当我试图在前端显示所有数据时,加载所有数据需要5分钟以上,所以我决定使用分页 在这里,我尝试每页显示10条记录。我正在成功显示前10条记录,分页方式如下所示(?以前的123456789…1235012351下一步?)但如果我正在检查下一页或选择分页中的任何页面,它将刷新页面,并通过url中的所选页面分页以使用php显示10项,php,mysql,pagination,Php,Mysql,Pagination,我的数据库中有100000多个数据,当我试图在前端显示所有数据时,加载所有数据需要5分钟以上,所以我决定使用分页 在这里,我尝试每页显示10条记录。我正在成功显示前10条记录,分页方式如下所示(?以前的123456789…1235012351下一步?)但如果我正在检查下一页或选择分页中的任何页面,它将刷新页面,并通过url中的所选页面http://localhost/var/cms/page.php?page=2如果不显示接下来的10个数据,则始终显示前10个数据 下面是我的代码,任何人都可以告
http://localhost/var/cms/page.php?page=2
如果不显示接下来的10个数据,则始终显示前10个数据
下面是我的代码,任何人都可以告诉我如何在下一页单击“谢谢”时显示接下来的10个数据
完整代码:
<?php
$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '1234fedf';
$dbDatabase = 'cms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");
$tbl_name="contact"; //your table name
$adjacents = 3;
$query = "SELECT COUNT(*) as num FROM $tbl_name";
//print $query;
$queryRes = mysql_query($query);
if($queryRes === FALSE) {
die(mysql_error()); // TODO: better error handling
}
while($rows = mysql_fetch_array($queryRes))
{
$total_pages=$rows['num'];
//print $total_pages;
}
$targetpage = "page.php"; //your file name (the name of this file)
$limit = 10; //how many items to show per page
$start = 0; //if no page var is given, set start to 0
/* Get data. */
$sql = "SELECT DISTINCT contact.`id` , contact.`contactgroup` , contact.`media` ,contact.`media2` , contact.`email1` , contact.`nationality` , contact.`country3` , contact.`twon` , contact.`area` , contact.`gender` , contact.`married` , contact.`children` , contact.`driverslicense` FROM $tbl_name LIMIT $start, $limit";
//print $sql;
$result = mysql_query($sql);
/* Setup page vars for display. */
$page = 1; //if no page var is given, default to 1.
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
$lastpage = ceil($total_pages/$limit); //lastpage is = total pages / items per page, rounded up.
//print $lastpage;
$lpm1 = $lastpage - 1; //last page minus 1
/*
Now we apply our rules and draw the pagination object.
We're actually saving the code to a variable in case we want to draw it more than once.
*/
$pagination = "";
if($lastpage > 1)
{
$pagination .= "<div class=\"pagination\">";
//previous button
if ($page > 1)
$pagination.= "<a href=\"$targetpage?page=$prev\">? previous</a>";
else
$pagination.= "<span class=\"disabled\">? previous</span>";
//pages
if ($lastpage < 7 + ($adjacents * 2)) //not enough pages to bother breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
{
//close to beginning; only hide later pages
if($page < 1 + ($adjacents * 2))
{
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//in middle; hide some front and some back
elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
$pagination.= "...";
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";
}
//close to end; only hide early pages
else
{
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
$pagination.= "...";
for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
}
}
}
//next button
if ($page < $counter - 1)
$pagination.= "<a href=\"$targetpage?page=$next\">next ?</a>";
else
$pagination.= "<span class=\"disabled\">next ?</span>";
$pagination.= "</div>\n";
}
?>
<?php
echo "<table border='1'>
<tr>
<th>Contactgroup</th>
<th>Email</th>
<th>Nationality</th>
<th>Country3</th>
<th>Twon</th>
<th>Area</th>
<thGender</th>
<th>Married</th>
<th>Children</th>
<th>Driverslicense</th>
</tr>";
while($rows = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $rows['contactgroup'] . "</td>";
echo "<td>" . $rows['email1'] . "</td>";
echo "<td>" . $rows['nationality'] . "</td>";
echo "<td>" . $rows['country3'] . "</td>";
echo "<td>" . $rows['twon'] . "</td>";
echo "<td>" . $rows['area'] . "</td>";
echo "<td>" . $rows['gender'] . "</td>";
echo "<td>" . $rows['married'] . "</td>";
echo "<td>" . $rows['children'] . "</td>";
echo "<td>" . $rows['driverslicense'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php echo $pagination;?>
在未找到任何页面时,将$start
变量初始化为0
。如果找到页面,则将页面与限制相乘,并加1以获得如下值
if(isset($_GET['page']) && $_GET['page']!="")
$start = ($_GET['page'] * 10) + 1;
else
$start = 0;
查看更多详细信息。我尝试了,但它显示了错误解析错误:语法错误,意外的T_BOOLEAN_和,在第34if行的C:\wamp\www\var\cms\page.php中应为“,”或“,”(isset($\u GET['page']&&$\u GET['page']!=”)在这行中它可以工作,但你能告诉我为什么我的分页看起来像这样吗?以前的123456789…1235012351下一页?同样,以前的页面不工作,如果我单击第二页,那么我就无法单击第一页。你必须使用css显示格式,并且你必须与我给定的链接进行比较,以供参考。你可以查看此幻灯片现在可能是更改密码的好时机。