duankan8739 2012-03-06 21:13
浏览 40
已采纳

如何为我的排序GET变量改进此代码?

In my questions.php page in my Forum that i am creating, I am checking the GET['sort'] variable to know what to sort by, e.g. by votes, or, views etc etc. I have a drop down offering to sort by different things. But how do I make the sort that the page is on by the default value of the drop down list. This is what I have so far, but it is terribly long and messy. I'm sure there is a more professional way of doing it. If you can enlighten me, please do!

if(isset($_GET['sort']) && $_GET['sort']=='answers'){
        $questions = Question::find_most_answered();
        $page_title = 'Showing most answered questions! - '.SITE_NAME;
        $sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">
                            <option value=\"questions.php?sort=answers\">Sorted By Most Answers</option>
                            <option value=\"questions.php?sort=votes\">Sorted By Most Voted</option>
                            <option value=\"questions.php?sort=recent\">Sorted By Most Recent</option>
                            <option value=\"questions.php?sort=oldest\">Sorted By Oldest</option>
                            <option value=\"questions.php?sort=views\">Sorted By Most Viewed</option>
                        </select>";
    }else if(isset($_GET['sort']) && $_GET['sort']=='oldest'){
        $questions = Question::find_oldest_questions();
        $page_title = 'Showing oldest questions! - '.SITE_NAME;
        $sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">
                            <option value=\"questions.php?sort=oldest\">Sorted By Oldest</option>
                            <option value=\"questions.php?sort=answers\">Sorted By Most Answers</option>
                            <option value=\"questions.php?sort=votes\">Sorted By Most Voted</option>
                            <option value=\"questions.php?sort=recent\">Sorted By Most Recent</option>
                            <option value=\"questions.php?sort=views\">Sorted By Most Viewed</option>
                        </select>";
    }else if(isset($_GET['sort']) && $_GET['sort']=='recent'){
        $questions = Question::find_recent_questions();
        $page_title = 'Showing most recent questions! - '.SITE_NAME;
        $sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">
                            <option value=\"questions.php?sort=recent\">Sorted By Most Recent</option>
                            <option value=\"questions.php?sort=answers\">Sorted By Most Answers</option>
                            <option value=\"questions.php?sort=votes\">Sorted By Most Voted</option>
                            <option value=\"questions.php?sort=oldest\">Sorted By Oldest</option>
                            <option value=\"questions.php?sort=views\">Sorted By Most Viewed</option>
                        </select>";
    }else if(isset($_GET['sort']) && $_GET['sort']=='views'){
        $questions = Question::find_most_viewed();
        $page_title = 'Showing most viewed questions! - '.SITE_NAME;
        $sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">
                            <option value=\"questions.php?sort=views\">Sorted By Most Viewed</option>
                            <option value=\"questions.php?sort=votes\">Sorted By Most Voted</option>
                            <option value=\"questions.php?sort=answers\">Sorted By Most Answers</option>
                            <option value=\"questions.php?sort=recent\">Sorted By Most Recent</option>
                            <option value=\"questions.php?sort=oldest\">Sorted By Oldest</option>
                        </select>";
        }else{
            $questions = Question::find_most_voted();
            $page_title = 'Showing most voted questions! - '.SITE_NAME;
            $sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">
                            <option value=\"questions.php?sort=votes\">Sorted By Most Voted</option>
                            <option value=\"questions.php?sort=answers\">Sorted By Most Answers</option>
                            <option value=\"questions.php?sort=recent\">Sorted By Most Recent</option>
                            <option value=\"questions.php?sort=oldest\">Sorted By Oldest</option>
                            <option value=\"questions.php?sort=views\">Sorted By Most Viewed</option>
                        </select>";
        }

Please help me if you can, and edit this to make it look nicer. I am not as expert at that.

Thanks a lot!

  • 写回答

6条回答 默认 最新

  • duanhongyi2964 2012-03-06 21:20
    关注
    // establish a list of possible drop-down list options
    // left-hand is the value, right hand is the "english" equivalent
    $options = array(
      'views' => 'Most Viewed',
      'votes' => 'Most Voted',
      'answers' => 'Most Ansers',
      'recent' => 'Most Recent',
      'oldest' => 'Oldest'
    );
    
    // determine the sort value
    $sort = (isset($_GET['sort']) && array_key_exists($_GET['sort'],$options)
          ? $_GET['sort']      // Input was valid, accept it
          : 'views');          // setup default sort here
    
    // populate the questions list based on the sort
    switch ($sort)
    {
      case 'views': $questions = Question::find_most_viewed(); break;
      case 'votes': $questions = Question::find_most_voted(); break;
      case 'answers': $questions = Question::find_most_answered(); break;
      case 'recent': $questions = Question::find_recent_questions(); break;
      case 'oldest': default: $questions = Question::find_oldest_questions(); break;
    }
    
    // Setup the title based on the $options value
    $page_title = 'Showing '.$options[$sort].' questions! - '.SITE_NAME;
    
    // populate the sortResults based on the value of $sort, and iterate over
    // it to reduce redundancy
    $sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">";
    foreach ($options as $k => $v){
      // it matches the current sort, mark it as selected
      // (I assume this is what you were going for by moving it to the
      // top of the list?)
      $selected = '';
      if ($sort == $k) $selected = ' selected="selected"';
    
      // it's not the current filter, so append it to $sortResults
      $sortResults .= "<option value=\"questions.php?sort={$k}\"{$selected}>Sorted By {$v}</option>";
    }
    $sortResults .= "</select>";
    

    Is probably how I'd tackle it.

    1. Place options in an array you can reference
    2. Validate $_GET based on keys in that array (and default if it's an invalid or exempt entry)
    3. Output options based on that $sort value.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?