1

このスクリプトについて少し助けが必要です。それは機能していますが、少し単純化する方法が欲しいです。

現時点では、「if($ currentPageIndex == 4)」があります。プロジェクトリスト配列にアイテムを追加または削除するたびに、「4」を手動で更新する必要があります。必要なのは、「if($ currentPageIndex ==配列の最後の項目)」と言うことです。そうすれば、番号の更新も気にせずに項目を追加/削除できます。

これについてはどうすればよいですか?私はさまざまなオプションを読み、これまで運が悪かったことを試してきました。

また、可能であれば、ソリューションは前と次のリンクでも使用できますか?したがって、+ 4-4の代わりに、それぞれ最初と最後のアイテムにジャンプします。

どんな助けでも大歓迎です。

ここでの作業デモ:http ://www.ok-hybrid.com/projects/monkey/ ここでのコード:

<?php

$currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables!
$currentPath = $currentPath[0]; //grab just the path
$projectlist = array(
        '/projects/monkey/',
        '/projects/tiger/',
        '/projects/parrot/',
        '/projects/banana/',
        '/projects/aeroplane/',
);



if(! in_array($currentPath, $projectlist) ) {
    die('Not a valid page!'); //they didn't access a page in our master list, handle error here.
}

$currentPageIndex = array_search($currentPath, $projectlist);

if($currentPageIndex > 0) { 
    $prevlink = '<a href="'.$projectlist[$currentPageIndex-1].'">Prev</a>';
} else {
    $prevlink = '<a href="'.$projectlist[$currentPageIndex+4].'">Prev</a>';
    }

if($currentPageIndex == 4) { 
    $nextlink = '<a href="'.$projectlist[$currentPageIndex-4].'">Next</a>';
} else {
    $nextlink = '<a href="'.$projectlist[$currentPageIndex+1].'">Next</a>';
}

?>



<ul id="sub-nav">

<li>
<?php
print_r($prevlink);
?>
</li>

<li>
<?php
print_r($nextlink);
?>
</li>

</ul>
4

5 に答える 5

3

試す$projectlist[count($projectlist)-1]

于 2012-06-07T13:23:47.823 に答える
2

使用する

sizeof($projectList) - 1

希望の番号を取得するには

于 2012-06-07T13:23:03.993 に答える
2

で配列の要素数を取得しますcount()

if ($currentPageIndex == count($projectlist)-1) {
  // do something
}
于 2012-06-07T13:23:27.827 に答える
1
if( $currentPageIndex === end($projectlist) ) {
    //sup
}
于 2012-06-07T13:33:57.683 に答える
1

次のコードと友人の助けを借りて、なんとか完全に機能させることができました。助けてくれてありがとう。

ここで言及されているさまざまな手順を使用しているようです。リンクをチェックするために使用されている@Arthurによって言及されているように、'sizeof($ projectList)-1'を使用します。

<?php 

    $currentPath = explode('?', $_SERVER['REQUEST_URI']); //make sure we don't count any GET variables!
    $currentPath = $currentPath[0]; //grab just the path



    $projectlist = array(

    '/projects/monkey/',
    '/projects/tiger/',
    '/projects/parrot/',
    '/projects/banana/',
    '/projects/aeroplane/',
    '/projects/egg/',

    );





    if(! in_array($currentPath, $projectlist) ) {
        die('Not a valid page!'); //they didn't access a page in our master list, handle error here
    }

    $currentPageIndex = array_search($currentPath, $projectlist);

    //echo $currentPageIndex."<Br />";


    //items in array count
    $projectlist_count = count($projectlist);


    if($currentPageIndex > 0) { //make sure it's not the first page
        $prevlink = '<a href="'.$projectlist[$currentPageIndex-1].'">Prev</a>';
            } else {
        $prevlink = '<a href="'.$projectlist[$projectlist_count-1].'">Prev</a>';
            }



    if($nextlink < sizeof($projectlist)-1 ) { //make sure we're not the last page

    if($currentPageIndex+1 >= $projectlist_count)
    {
    //go back to start of array
        $nextlink = '<a href="'.$projectlist[0].'">Next</a>';
    } else {
        $nextlink = '<a href="'.$projectlist[$currentPageIndex+1].'">Next</a>';
    }
    }


    ?>

    <ul id="sub-nav">

    <li>
    <?php
    print_r($prevlink);
    ?>
    </li>

    <li>
    <?php
    print_r($nextlink);
    ?>
    </li>

    </ul>
于 2012-06-07T21:46:57.520 に答える