2

そこで、タブボタンとページ番号を持つプログラムを作成しました。小さな問題に気付くまで、すべての機能はほぼ正常に動作しています。ご存知のように、タブは常に現在のタブを強調表示します。タブが A ~ Z の文字と、ホームまたはメインページを意味する # であり、# が現在のページであり、メイン ページが次のリストで構成されているとします。データベースに登録されている従業員。ページ番号 (次と前) があるので、従業員の名前/情報の量または数を 5 に制限し、画面に 5 つのレコードのみを表示する必要があることを示しています。

注: 私のコードは機能していますが、1 つの問題が発生しました。次のボタンをクリックして従業員の他のリストを表示するたびに、#タブは強調表示されませんが、同じページにいるため、まだ強調表示されているはずです。これの原因と修正方法を知っている人はいますか?説明するのがとても難しいので、それが明確でない場合は申し訳ありません。

例:(これをウィンドウとして想像してください) 制限が = 2 であるとしましょう

**#** ABCDEFGHIJKLMNOPQRSTU VWXYZ //これはタブ ボタンです。# が強調表示されていることに注意してください
従業員 ID : 従業員名 : 従業員年齢
     1 チェル 26
     2 ブランドン 35
**PREV** **NEXT** //これはページ番号です

メイン ページで次の従業員を表示するために [次へ] をクリックしようとすると、ページは次のようになります。

# ABCDEFGHIJKLMNOPQRSTU VWXYZ //[次へ] をクリックした後、# が強調表示されていないことに注意してください
従業員 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;";
    }   
    ?>

注意: 私の変数名リストは AZ 変数に使用され、 searchfileは私の検索ボタンに使用されます MisaChan

4

1 に答える 1

0

この長いコードに苦労した後、問題を引き起こしている可能性がある唯一のことは$listname

listname が文字でない場合、コードは機能しません。

コードはアルファベット順にソートされていないため、代わりに数字の使用を検討する必要があります。AZ は 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');

これにより、前と次のボタンが自動的に作成されます

これが問題の解決に役立つことを願っています

于 2011-05-24T06:19:39.077 に答える