gpt4 book ai didi

php - 我如何为我的排序 GET 变量改进这段代码?

转载 作者:行者123 更新时间:2023-11-29 01:44:55 25 4
gpt4 key购买 nike

在我正在创建的论坛的 questions.php 页面中,我正在检查 GET['sort'] 变量以了解要排序的内容,例如通过投票或观点等。我有一个下拉菜单可以按不同的事物进行排序。但是如何根据下拉列表的默认值对页面进行排序。到目前为止,这是我所拥有的,但它又长又乱。我相信有更专业的方法可以做到这一点。如果你能赐教,请赐教!

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>";
}

如果可以的话,请帮助我,并对其进行编辑,使其看起来更漂亮。我不是这方面的专家。

非常感谢!

最佳答案

// 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>";

这可能是我处理它的方式。

  1. 将选项放在您可以引用的数组中
  2. 根据该数组中的键验证 $_GET(如果它是无效或豁免条目则默认)
  3. 根据 $sort 值输出选项。

关于php - 我如何为我的排序 GET 变量改进这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9592006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com