在我創建的論壇的questions.php頁面中,我正在檢查GET ['sort']變量以知道要排序的內容,例如,通過投票,或意見等等等等我有一個下降提供排序不同的東西。但是,如何通過下拉列表的默認值對頁面進行排序。這是我到目前爲止,但它是非常漫長和混亂。我確信有一個更專業的方式來做到這一點。如果你能啓發我,請做!如何改進我的排序GET變量的代碼?
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>";
}
請幫助我,如果你可以,並編輯它,使其看起來更好。我不是這方面的專家。
非常感謝!
帶環的數組 – 2012-03-06 21:14:47
使用開關模型[http://php.net/manual/en/control-structures.switch。php] – AlexC 2012-03-06 21:16:51
你可能已經爲我所做的所有好事寫下了'箍ha'。不管怎麼說,還是要謝謝你! – yehuda 2012-03-06 21:17:10