这个问题已经回答了。 Click here看到回答我问题的答案。
<小时/>
我仍然不知道该怎么做。有人可以帮忙吗?我知道它被称为“分页”,但我不知道如何让它在底部显示页面链接。我希望有人能够提供帮助。
<小时/> 我有一张票证表,所有数据都从数据库加载到表中。我有页面(使用?page=1
,?page=2
等)但是,我怎样才能在“下一页”,“上一页”,“最后一页”,“页面<在底部有某事“的东西等等?
<table id="tickets">
<tr>
<th>Name</th>
<th>Email</th>
<th>Subject</th>
<th>Created on</th>
<th style="width:65px;">Status</th>
<th>Actions</th>
</tr><?php
$startpoint = $_GET["page"];
if (isset($_GET["page"])) {
$startpoint = $_GET["page"];
}
else {
$startpoint = 1;
}
$startpoint = $startpoint - 1;
$startpoint = $startpoint * 10;
$endpoint = $startpoint + 10;
function addEllipsis($string, $length) {
$end = "...";
if (strlen($string) > $length) {
$length -= strlen($end); // $length = $length – strlen($end);
$string = substr($string, 0, $length);
$string .= $end; // $string = $string . $end;
}
return $string;
}
$x = 1;
if ($show == "active") {
$result = mysql_query("SELECT * FROM tickets WHERE is_active='1'");
while ($info = mysql_fetch_array($result)) {
$x++;
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
if ($isActive == '1') {
$status = "<span class=\"open\">Open</span>";
}
if ($isActive == '0') {
$status = "<span class=\"closed\">Closed</span>";
}
if ($x > $startpoint && $x < $endpoint) {
echo "
<tr>
<td>".$name."</td>
<td>".$email."</td>
<td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
<td>".$created."</td>
<td>".$status."</td>
<td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket »</a></td>
</tr>";
}
}
}
else if ($show == "closed") {
$result = mysql_query("SELECT * FROM tickets WHERE is_active='0'");
while ($info = mysql_fetch_array($result)) {
$x++;
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
if ($isActive == '1') {
$status = "<span class=\"open\">Open</span>";
}
if ($isActive == '0') {
$status = "<span class=\"closed\">Closed</span>";
}
if ($x > $startpoint && $x < $endpoint) {
echo "
<tr>
<td>".$name."</td>
<td>".$email."</td>
<td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
<td>".$created."</td>
<td>".$status."</td>
<td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket »</a></td>
</tr>";
}
}
}
else {
$result = mysql_query("SELECT * FROM tickets");
while ($info = mysql_fetch_array($result)) {
$x++;
$name = $info['name'];
$email = $info['email'];
$subject = $info['subject'];
$ticketid = $info['ticket'];
$isActive = $info['is_active'];
$created = $info['created'];
if ($isActive == '1') {
$status = "<span class=\"open\">Open</span>";
}
if ($isActive == '0') {
$status = "<span class=\"closed\">Closed</span>";
}
if ($x > $startpoint && $x < $endpoint) {
echo "
<tr>
<td>".$name."</td>
<td>".$email."</td>
<td title=\"".$subject."\">".addEllipsis($subject, 16)."</td>
<td>".$created."</td>
<td>".$status."</td>
<td><a href=\"/employee/employee.php?ticket=".$ticketid."\">View ticket »</a></td>
</tr>";
}
}
}
?>
</table>
我怎样才能让它在底部显示每个页面的链接(只是一个简单的“1”,“2”等)?我不能自己创建它们,因为我不知道有多少页面和东西。当您在某个页面上时,如果链接可能变成灰色不可点击的文本,那也会很棒。有人帮助我使用页面功能,但他们不在线,所以他们无法帮助我。 (我还不擅长PHP)任何人都知道如何实现这个目标吗?
答案 0 :(得分:2)
<强> -edit - 强>
聊天后,解决方案包括向查询添加LIMIT
而不是使用$x
来阻止显示某些行,如下所示:https://gist.github.com/1233850
运行以下代码,因为它可以自行运行,它可能会给你一个想法:
<style>span{background:#ccc;padding:2px 6px;margin:0 2px;}</style>
<?php
$currentPage = isset($_GET['page']) ? $_GET['page'] : 1;
$totalPages = 5; //this must be fetched from the db
//define previous and next
$previous = ($currentPage > 1) ? $currentPage - 1 : false;
$next = ($currentPage < $totalPages) ? $currentPage + 1 : false;
//here we'll save the resulting code
$pagination = array();
//first
$pagination[] = ($currentPage == 1) ? "First" : "<a href='?page=1'>First</a>";
//previous
$pagination[] = $previous ? "<a href='?page=$previous'>Previous</a>" : "Previous";
//current/total
$pagination[] = "Page $currentPage / $totalPages";
//next
$pagination[] = $next ? "<a href='?page=$next'>Next</a>" : "Next";
//last
$pagination[] = ($currentPage == $totalPages) ? "Last" : "<a href='?page=$totalPages'>Last</a>";
//show it
foreach($pagination as $part){
echo "<span>$part</span>";
}
?>
答案 1 :(得分:1)
好的,通常可以这样做:
if (isset($_GET["page"])) {
$currentPage = $_GET["page"];
}
else {
$currentPage = 1;
}
良好的开端。要获取每页数据库/项目中记录计数所需的页数。
$count = mysql_query("SELECT COUNT(id) FROM tickets"); //yop, it's kind of shortcut
$pages = ceil($count/$itemsPerPage);
好的,生成菜单的页码(假设我们想要“&lt; 1 ... 3 4 5 ... last&gt;” - &gt;其中$ curretnPage = 4):
$menu = array(); //array indexes are strings to make this clearer, it normally should be $menu[] = $sth; to preserve adding order which is important here
if($currentPage > 1) {
$menu['previous'] = $currentPage - 1;
}
$menu['first'] = 1;
if($currentPage > 3) {
$menu['dots'] = 0;
}
else if($currentPage == 3) {
$menu['oneBeforeCurrent'] = $currentPage - 1;
$menu['current'] = $currentPage;
}
else if($currentPage == 2) {
$menu['current'] = $currentPage;
}
$menu['oneAfterCurrent'] = $currentPage + 1;
......它变得有点复杂,也许有一种更好,更快的方式,但你可以看到它是如何完成的(不知道它应该如何“完成”)。它不完整,但是......你知道该怎么做:) 我希望你的意思是,你不能创建菜单......如果你有数字的数组只是循环并用数字创建html链接并把“...”放在0。
干杯