【问题标题】:How to correct my PHP code to make simple pagination without Database of menu list displaying?如何在不显示菜单列表数据库的情况下更正我的 PHP 代码以进行简单的分页?
【发布时间】:2022-08-24 22:59:03
【问题描述】:

我有一个更改我的 USSD 项目的菜单显示的小新错误(我称之为:\"分页\") 但是,在我对项目的另一部分进行更改之前,它起作用了。正如您在以下屏幕截图中看到的那样:

SCREEN OF DISPLAYING OF THE MENU LIST

function toString(){
        $objectString=\'\';
        $items=$this->children;
        $bufferLimit=(count($items)==0)?1:$this->getBufferLimit()+1;

        echo \"<pre>\";
        echo $this->getBufferLimit()+1;
        echo \"</pre>\";

        do{
            $bufferLimit-=1;
            $objectString=$this->recurseMenu($items,$bufferLimit);
        }while(strlen($objectString>160));
        $this->index=$bufferLimit;

        /* echo \"<pre>\";
        echo $this->index;
        echo \"</pre>\"; */

        /* echo \"<pre>\";
        echo $objectString;
        echo \"</pre>\"; */
        return $objectString;
    }

    function getBufferLimit() {
        $len=count($this->children);
        $margin=$len-$this->index;

        if($margin<5)
            return $this->index+$margin;
        else
            return $this->index+5; //Permet de définir le Nombre de Menu à afficher au niveau de l\'index \"/ussd/receiver.php\" dont le Menu est \"/ussd/MyTree.php\".
    }

    function recurseMenu($items,$bufferLimit) {
        $objectString=\"<strong>\". $this->getTitle() . \"</strong>\" . PHP_EOL;
        $lastMenu=false;
        if(count($items)>0) {
            foreach ($items as $i => $item) {
                if ($i >= $bufferLimit){
                    break;
                }
                $num = $i + 1;
                //get node by name
                $userSessions = $_SESSION[\'userSessions\'];
                $currUserSession = $userSessions[$this->address];
                $node = $currUserSession->getNode($item);
                $title = $node->getTitle();
                $objectString .= PHP_EOL . $num . \'. \' . $title;
            }
        } else {
            $objectString=$objectString.PHP_EOL . \'NO DATA AVAILABLE, TRY AGAIN LATER\';
        }
        $lastMenu=$bufferLimit==count($items);
        $objectString=$objectString . PHP_EOL . PHP_EOL . \"<strong>0. Exit</strong>\";
        if($this->getParent() != \'0\'){
            $objectString=$objectString . PHP_EOL . \"<strong>#. Back</strong>\";

        }
        if($lastMenu===false){
            $rem=count($items)-$this->index;
            $objectString=$objectString . PHP_EOL . \"<strong>99. Next (\".$rem.\")</strong>\";
        }
        return $objectString;
    }

正如您在我刚刚发送给您的屏幕截图中看到的那样,在此屏幕截图中以蓝色选择的下一页(712)菜单显示在索引下方,而当我们发送消息“99”时这会激活 \"Next\" 选项,默认索引的第一个菜单列表(16)应该消失并让位于下一个菜单列表(712)。

当我尝试在上面的 \"toString\" 函数中调试 \"$this-&gt;getBufferLimit()+1\" 时,如下所示:

echo \"<pre>\"; 
echo $this->getBufferLimit()+1; 
echo \"</pre>\";
  • 第一\"99. Next\" 的条目返回给我6
  • 第二\"99. Next\" 的输入返回给我11
  • 第三\"99. Next\" 的条目返回给我13

我指定当前有 12 个菜单,我每次按 \"99. Next\" 时尝试分批显示 5 个菜单。

那么如何修改我的\"getBufferLimit\"函数中的\"return $this-&gt;index+5\"这样即使我们单击“99. Next”操作,它也只显示接下来的 6 个菜单列表,而不是在上面的屏幕截图中当前看到的之前的列表下方显示它们,因为知道它也用于“@” 987654343@\"函数下面???

谢谢请帮帮我。

    标签: php


    【解决方案1】:

    recurseMenu 函数中是这样的:

    function recurseMenu($items,$bufferLimit) {
            $objectString="<strong>". $this->getTitle() . "</strong>" . PHP_EOL;
            $lastMenu=false;
            if(count($items)>0) {
                $nbrElementPerPage = 5;
                $currentPage = 1;
                $nbrPage = ceil(count($items) / $nbrElementPerPage);
                // On empêche d'être à une page nagative
                if ($currentPage < 1) $currentPage = 1;
                // On empêche d'être au delà du nombre de page
                if ($currentPage > $nbrElementPerPage) $currentPage = $nbrElementPerPage;
                $itemSlice = array_slice($items, ($currentPage - 1) * $nbrElementPerPage, $nbrElementPerPage);
                foreach ($itemSlice as $i => $item) {
                    if ($i >= $bufferLimit){
                        break;
                    }
                    $num = $i + 1;
                    //get node by name
                    $userSessions = $_SESSION['userSessions'];
                    $currUserSession = $userSessions[$this->address];
                    $node = $currUserSession->getNode($item);
                    $title = $node->getTitle();
                    $objectString .= PHP_EOL . $num . '. ' . $title;
                }
            } else {
                $objectString=$objectString.PHP_EOL . 'NO DATA AVAILABLE, TRY AGAIN LATER';
            }
            $lastMenu=$bufferLimit==count($items);
            $objectString=$objectString . PHP_EOL . PHP_EOL . "<strong>0. Exit</strong>";
            if($this->getParent() != '0'){
                $objectString=$objectString . PHP_EOL . "<strong>#. Back</strong>";
    
            }
            if($lastMenu===false){
                $rem=count($items)-$this->index;
                $objectString=$objectString . PHP_EOL . "<strong>99. Next (".$rem.")</strong>";
            }
            return $objectString;
        }
    

    也许 ???

    但是当我点击99. Next 时,它并没有移动到第二页。它在第一页上保持不变。而且,我想这是因为我的$currentPage 变量是1

    那么如何激活变量$currentPage,以便每次单击99. Next 时它都会演变和更改值$currentPage = 2 等等???

    如果没有,请纠正我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-28
      • 2017-02-16
      • 2014-04-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-21
      • 2021-07-22
      • 1970-01-01
      相关资源
      最近更新 更多