Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:44:50+00:00 2026-05-31T02:44:50+00:00

In my questions.php page in my Forum that i am creating, I am checking

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:44:51+00:00Added an answer on May 31, 2026 at 2:44 am
    // 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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php page that has a few HTML questions on it, each
I have a PHP page that needs to make a call to a external
I have asked several questions on the buddypress forum but I never get any
I have an includes.php page that I load at the start of every page
I'm building this website and I have basically two questions about this page: http://sites.publishyours.com.br/silviamecozzi/pt/obra/deserto_das_palmas.php
I'm developing online examination system in php-mysql. That exam will consist of multiple-choice questions;
How do I 'reroute' a 'Questions' category post page to a new/customized single.php and
I am creating a page that allows users access to a certain section of
I am creating a page that allows users access to a certain section of
Right, day 8 learning php and I feel that I have tried to do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.