我有一个PHP文件,可以加载三个不同的页面:用户的关注者,关注者和他们的朋友(下面用a,b和c表示)。每个"页面"根据谁查看网站有不同的隐私设置,因此a,b和c具有真/假值。如果值为false,则该用户无法查看该页面。我想创建下一个和上一个按钮,但为了确定下一个/上一个页面,我需要考虑用户的隐私。例如,如果用户只显示b,则没有下一个/上一个,但如果他们显示a和c,那么a的next / previous将是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;
}
}
答案 0 :(得分:1)
面向对象的解决方案在这里会很干净,请考虑这个伪代码:
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;
}
}
相同的代码可用于上一页。您甚至可以将其设置为动态,以便为下一个和上一个保存冗余代码。
现在,您所要做的就是在应用程序开始时对每个页面进行异议,您可以轻松浏览它们。
答案 1 :(得分:1)
<强> UNTESTED 强>
$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 >