【问题标题】:Problems with current tab and page numbers in phpphp中当前选项卡和页码的问题
【发布时间】:2011-05-24 05:39:52
【问题描述】:

我已经创建了一个带有标签按钮和页码的程序。在我注意到一个小问题之前,所有功能几乎都可以正常工作。众所周知,标签总是突出显示您所在的当前标签。假设您的标签是 A-Z 字母和 # 表示主页或主页,您的 # 是当前页面,主页由列表组成在您的数据库中注册的员工。因为我有页码(下一个和上一个),所以我将员工姓名/信息的数量或数量限制为 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 //这是选项卡按钮,注意 # 突出显示 员工ID:员工姓名:员工年龄 1 切尔 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 //注意点击下一步后#没有高亮显示 员工ID:员工姓名:员工年龄 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> &nbsp;";
    }
    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> &nbsp;";
    }   
    ?>

注意: 我的变量 namelist 用于 A-Z 变量 searchfile 用于我的搜索按钮 陈美莎

【问题讨论】:

  • 据我了解,您将在 prev 或 next ?page=1 或 2 等中添加一个参数,然后您将使用 $_GET 获取正确的页面
  • @Ibu : 好的,我试试看 :)
  • @lbu :好吧,我对我应该怎么做有点困惑。你看,我的代码中已经有类似的东西了。我会把它贴在这里。你介意检查一下吗?谢谢
  • 发布处理分页的代码
  • @lbu 我已经发布了我的标签和页码的代码:)

标签: php tabs page-numbering


【解决方案1】:

在处理这么长的代码之后,我看到的唯一可能导致问题的是$listname

如果 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');

这会自动为你创建上一个和下一个按钮

希望能帮到你解决问题

【讨论】:

  • 还有其他方法吗?你看我的名单名称是按字母顺序排列的。每当用户单击字母选项卡时,例如 A,所有名称以 A 开头的员工都会在页面上回显:(所以我无法更改它。
  • 您可以修改类以满足您的需要,例如,添加一个包含 [A-Z] 的数组,下一个将是 currentarrayIndex +1,上一个将是 currentarrayIndex -1,这将具有相同的效果
  • 先生,我发现了错误,在我的 A-Z 代码中,如果你能注意到那里的 isset,我将它转移到 $return ='
      ' 下面。"\n ";并添加了另一个代码 $GET[namelist]!="%" 除了那个 isset,无论如何我认为您的代码对于可能遇到与我相同问题的其他人仍然非常有用。谢谢你,先生:)
  • 很高兴您找到了解决方案 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 2014-02-23
  • 1970-01-01
  • 2013-04-24
  • 2014-07-06
  • 1970-01-01
  • 2021-08-09
相关资源
最近更新 更多