如果你有網頁名稱的數組,如下面所示:記住下一個和以前的頁面使用堆棧
$array = ["home.php", "about.php", "contact.php"];
用戶將降落在「home.php」,並有一個按鈕去下一頁將是「about.php」。然後,關於頁面會有一個前一個按鈕,它將轉到「home.php」,下一個按鈕將轉到「contact.php」。
我想通過推送和彈出值來使用堆棧,但我沒有運氣。我很感激,如果有人建議替代品,但我想使用一個堆棧。
如果你有網頁名稱的數組,如下面所示:記住下一個和以前的頁面使用堆棧
$array = ["home.php", "about.php", "contact.php"];
用戶將降落在「home.php」,並有一個按鈕去下一頁將是「about.php」。然後,關於頁面會有一個前一個按鈕,它將轉到「home.php」,下一個按鈕將轉到「contact.php」。
我想通過推送和彈出值來使用堆棧,但我沒有運氣。我很感激,如果有人建議替代品,但我想使用一個堆棧。
您可以使用array_search找到數組中的當前頁的位置。然後檢查是否有任何鄰國,像這樣:
$array = ["home.php", "about.php", "contact.php"];
// Get the location of the current page in $array
$currentPageKey = array_search(basename($_SERVER['SCRIPT_NAME']), $array);
// See if there is a key prior to this. If so, get it's value
$previousPage = array_key_exists($currentPageKey - 1, $array)
? $array[$currentPageKey - 1]
: null;
// See if there is a key after this. If so, get it's value
$nextPage = array_key_exists($currentPageKey + 1, $array)
? $array[$currentPageKey + 1]
: null;
然後,你可以這樣做
if (!is_null($previousPage)) {
echo '<a href="' . $previousPage . '">Previous</a>';
}
if (!is_null($nextPage)) {
echo '<a href="' . $nextPage . '">Next</a>';
}
在'about.php'頁面上設置這個設置的實例:https://3v4l.org/BYhVa – Oldskool
只是檢查當前頁面的數組索引並建立相應的上/下一個環節:
$array = ["home.php", "about.php", "contact.php"];
$cp = basename($_SERVER['PHP_SELF']);
$ci = array_search($cp, $array);
if($ci > 0){
echo '<a href="'.$array[$ci-1].'">Prev page</a>';
}
if($ci < count($array)-1){
echo '<a href="'.$array[$ci+1].'">Next page</a>';
}
*我試圖通過推動和彈出值用於該堆棧,但我有沒有運氣* - 你能告訴我們你的代碼嗎? – Ben
在會話變量中分配數組。 – Fil