问题描述
|
在那里,我创建了一个带有选项卡按钮和页码的程序。所有功能几乎都能正常工作,直到我注意到一个小问题。众所周知,标签页始终会突出显示您当前所在的标签页。让我们说一下,如果您的标签页是AZ字母,并且#是首页或主页,而#是当前页,而主页则包含在您的数据库中注册的员工。因为我有页码(下一个和上一个),所以我限制了员工姓名/信息的数量或数量,由5表示仅5条记录应出现在屏幕上。
注意:我的代码可以正常工作,但有1个问题已解决。每次我单击下一个按钮以查看其他雇员列表时,“#”选项卡不会突出显示,因为您位于同一页面,因此它应该仍被突出显示。有谁知道这是什么原因以及如何解决?抱歉,如果不清楚,因为很难解释。
例子:(想象一下就是窗口),我们说限制为= 2
**#** A B C D E F G H I J K L M N O P Q R S T U V W X Y Z // //这是标签按钮,注意#突出显示了
员工编号:员工姓名:员工年龄
1 chel 26
2布兰登35
** PREV ** ** NEXT ** //这是页码
当我尝试单击“下一步”以查看主页中的下一位雇员时,该页面如下所示:
#A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ///请注意,单击下一步后,#号未突出显示
员工编号:员工姓名:员工年龄
3查理28
4萨沙24
**上一页** **下一页**
我希望我能通过这个简单的例子解决我的问题。我希望有人可以帮助我。谢谢
//this is my tabs codes
<?php
function toc_menu($current){
$return =\'<ol id=\"toc\">
\'.\"\\n\";
$return .= ($current==\'\') ? \'<li class=\"current\"><a href=\"index.php?namelist=%\"><span>#</span></a></li>\'.\"\\n\" : \'<li><a href=\"index.php\"><span>#</span></a></li>\'.\"\\n\";
foreach(range(\'a\',\'z\') as $link){
$return .= ($current==$link) ? \'<li class=\"current\"><a href=\"index.php?namelist=\'.$link.\'\"><span>\'.strtoupper($link).\'</span></a></li>\'.\"\\n\" : \'<li><a href=\"index.php?namelist=\'.$link.\'\"><span>\'.strtoupper($link).\'</span></a></li>\'.\"\\n\";
}
$return .=\"</ol>\\n\";
return $return;
}
if(isset($_GET[\'namelist\']))
{
$current=$_GET[\'namelist\'];
}
else
{$current=\'\';
}
//echo where you want the menu
if(isset($_GET[\'namelist\'])) {
echo toc_menu(strtolower($_GET[\'namelist\']));
$tocmenu = toc_menu(strtolower($_GET[\'namelist\']));
} else {
echo toc_menu(strtolower(\'\'));
}
//or hold it in a variable to display later on
?>
//and this is my page_number codes:
<?php if ($offset>=1)
{ // bypass PREV link if offset is 0
$prevoffset=$offset-$limit;
print \"<a href=\\\"\".htmlentities($_SERVER[\'PHP_SELF\']).\"?offset=$prevoffset&searchfile=$search&namelist=$listname\\\"/>Prev</a> \";
}
echo \'</td>
<td colspan =\"5\" height =\"20\" align=\"right\"\';
// check to see if last page
if (!($offset+$limit > $total))
{
// not last page so give NEXT link
if ($offset == 1)
{
$newoffset=$offset+($limit-1);
}
else
{
$newoffset=$offset+$limit;
}
print \"<a href=\\\"\".htmlentities($_SERVER[\'PHP_SELF\']).\"?offset=$newoffset&searchfile=$search&namelist=$listname\\\">Next</a> \";
}
?>
做记录:
我的变量名称列表用于A-Z变量
搜索文件用于我的搜索按钮
陈美诗
解决方法
在用了这么长的代码后,我看到的唯一可能导致问题的是“ 1”
如果listname不是字母,则您的代码将无效。
这是我的建议,因为您的代码不是按字母顺序排序的,所以您应该考虑使用数字代替。 A-Z将是1到10或
1 to total pages
,这更有意义,
这是我用来轻松处理分页的课程
class Pagination {
public $current_page;
public $per_page;
public $page_count;
public function __construct($page=1,$per_page= 15,$total_count=0) {
$this->current_page = $page;
$this->per_page = $per_page;
$this->total_count = $total_count;
}
public function offset() {
return ($this->current_page -1)* $this->per_page;
}
public function total_pages () {
return ceil($this->total_count/ $this->per_page);
}
public function previous_page () {
return $this->current_page -1;
}
public function next_page () {
return $this->current_page +1;
}
public function has_previous_page () {
return $this->previous_page() >= 1? true : false;
}
public function has_next_page () {
return $this->next_page() <= $this->total_pages()? true : false;
}
// edit this section to suit your needs
public function format_output ($link,$query) {
if ($this->total_pages() > 1) {
$output = \"<div id=\\\"pagination\\\"><p>\";
$output .= ($this->has_previous_page())? (\"<a href=\\\"/$link/\".$this->previous_page().\"/{$query}\\\" >Previous</a>\"): \"\";
$c = $this->current_page;
$t = $this->total_pages();
for ( $i = 1; $i <= $t; $i++ )
{
if ($this->current_page == $i) {
$output .= \" <a class=\\\"active\\\" href=\\\"#\\\" >{$i}</a> \";
}else {
$output .= \" <a href=\\\"/$link/{$i}/{$query}\\\" >{$i}</a> \";
//$output .= \" <a href=\\\"$link?q={$query}&page={$i}\\\" >{$i}</a> \";
}
}
$output .= (($t ==$this->total_pages())?\"\":\' ... \');
$output .= ($this->has_next_page())? (\"<a href=\\\"/$link/\".$this->next_page().\"/{$query}\\\" >Next</a>\"): \"\";
$output .= \"</p></div>\";
}else {
$output = \"<div id=\\\"pagination\\\"><p>Displaying all results.</p></div>\";
}
return $output;
}
}
?>
这就是我的使用方式。
// your listname in numbers
$currentPage = isset($_GET[\'currentPage\'])? $_GET[\'currentPage\']: 1; // page number
$limit =2; // you decided to limit 2 per page
$total = 10; // the total result available in all pages
$pagination = new Pagination($currentPage,$limit,$total);
// if you need the the value of how many to offset for the current page
$offset = $pagination->offset(); // example page 6 will return 12
// and to display the pagination simply echo this
echo $pagination->format_output(htmlentities($_SERVER[\'PHP_SELF\']),\'other values u want to pass\');
这将自动为您创建上一个和下一个按钮
我希望这可以帮助您解决问题