dreamy1992 2014-06-16 20:02
浏览 56
已采纳

php函数返回列表中的下一个/上一个项目

I have a PHP file that loads three different pages: a user's followers, following, and their friends (represented below by a, b, and c). Each "page" has different privacy settings depending on who's viewing the website, so a, b, and c have true/false values. If the value is false, then that user cannot view the page. I want to create next and previous buttons, but in order to determine which page is next/previous, I need to factor in the user's privacy. For example, if the user only shows b, there would be no next/previous, but if they showed a, and c, then next/previous for a would be c, and for c it would be a. I wrote some code below that tries to accomplish this, but is there a simpler way to do this without being so repetitious? It's also important that it loop, so even if I'm on page c, the next button will bring me to page 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;
    }
}
  • 写回答

2条回答 默认 最新

  • douluanzhao6689 2014-06-16 20:35
    关注

    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 >
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?