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!