我有一个PHP文件,加载三个不同的页面:用户的追随者,追随者和他们的朋友(下面用a, b和c表示)。每个"页面"都有不同的隐私设置,这取决于谁在查看网站,所以a, b和c有真/假值。如果该值为false,则该用户无法查看该页。我想创建"下一步"answers"上一个"按钮,但是为了确定哪个页面是"下一步"/"上一个",我需要考虑用户的隐私。例如,如果用户只显示b,就不会有下一个/前一个,但如果他们显示a和c,那么a的下一个/前一个将是c, c的下一个/前一个将是a。我在下面写了一些代码来尝试实现这一点,但是有没有一种更简单的方法来做到这一点,而不是如此重复?循环也很重要,所以即使我在c页,下一个按钮也会把我带到a页。
$a = $_GET['a']; // true/false
$b = $_GET['b']; // true/false
$c = $_GET['c']; // true/false
$cur = // current page: a, b, or c
if($cur = $a) {
if($b) {
$next = $b;
}
else if($c) {
$next = $c;
}
else {
$next = $a;
}
if($c) {
$previous = $c;
}
else if($b) {
$previous = $b;
}
else {
$previous = $a;
}
}
if($cur = $b) {
if($c) {
$next = $c;
}
else if($a) {
$next = $a;
}
else {
$next = $b;
}
if($a) {
$previous = $a;
}
else if($c) {
$previous = $c;
}
else {
$previous = $b;
}
}
if($cur = $c) {
if($a) {
$next = $a;
}
else if($b) {
$next = $b;
}
else {
$next = $c;
}
if($b) {
$previous = $b;
}
else if($a) {
$previous = $a;
}
else {
$previous = $c;
}
}
未测试
$PRIVACY_PAGES[0] = "followers.php";
$PRIVACY_PAGES[1] = "following.php";
$PRIVACY_PAGES[2] = "friends.php";
function getNextPage($currPageName, $userPrivacy){
//$userPrivacy is array of boolean same order as $PRIVACY_PAGES
$currIdx = getPageIndex($currPageName);
$loopStart = 0;
if($currIdx > -1){
$loopStart = $currIdx+1;
}
if($currIdx == 2){//he is on last page
return "#";
}
for($v=$loopStart;$v<count($userPrivacy);$v++){
if($userPrivacy[$v]==true){
return $PRIVACY_PAGES[$v];
}
}//for loop
return "#";//he have no permission to view any page further.
}
function getPageIndex($currentPageName){
for($p=0;$p<count($PRIVACY_PAGES);p++){
if($PRIVACY_PAGES[0] == $currentPageName)
return $p;
}//for loop
return -1;//he is on another page, like index.php or gallery.php...
}
/////////////
//example :
$userPrivacy[0] = true;
$userPrivacy[1] = false;
$userPrivacy[2] = true;
$next = getNextPage("followers.php", $userPrivacy)
//$next should be friends.php
< html >
:
:
< a href="< ? php echo $next;?>">Next< /a >
面向对象的解决方案在这里是干净的,考虑以下伪代码:
class page {
private $nextpage;
private $prevpage;
private $permission;
function __construct($nextpage, $permission)
{
$this->nextpage = $nextpage;
$this->permission = $permission;
}
public function GetNextPage($first = $this)
{
if ($this->nextpage == null || $first == $this) {
return false;
} elseif ($this->nextpage->isAllowed()) {
return $this->nextpage;
} else {
return $this->nextpage->GetNextPage($first);
}
}
public function isAllowed()
{
// Some authorization voodoo here
return $_SESSION['userpermissions'] == $permission;
}
}
相同的代码可用于前一页。你甚至可以将其设置为动态的,以便为下一步和上一步节省多余的代码。
现在您所要做的就是在应用程序开始时实例化每个页面,您可以轻松地浏览它们。