在我正在创建的论坛的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>";
}
如果可以,请帮助我,并编辑它以使其看起来更好。我不是那么专家。
非常感谢!
答案 0 :(得分:1)
// 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>";
可能是我要解决的问题。
$_GET
(默认情况下,如果它是无效或免除条目)$sort
值的输出选项。答案 1 :(得分:1)
你可以这样做,它未经测试但应该有用。
<?php
$options[] = "<option value=\"questions.php?sort=answers\">Sorted By Most Answers</option>";
$options[] = "<option value=\"questions.php?sort=votes\">Sorted By Most Voted</option>";
$options[] = "<option value=\"questions.php?sort=recent\">Sorted By Most Recent</option>";
$options[] = "<option value=\"questions.php?sort=oldest\">Sorted By Oldest</option>";
$options[] = "<option value=\"questions.php?sort=views\">Sorted By Most Viewed</option>";
$option_ontop = 0;
if(!empty($_GET['sort'])) {
switch($_GET['sort']) {
case 'answers':
$option_ontop = 0;
$questions = Question::find_most_answered();
$page_title = 'Showing most answered questions! - '.SITE_NAME;
break;
case 'votes':
$option_ontop = 1;
$questions = Question::find_most_voted();
$page_title = 'Showing most voted questions! - '.SITE_NAME;
break;
case 'recent':
$option_ontop = 2;
$questions = Question::find_recent_questions();
$page_title = 'Showing most recent questions! - '.SITE_NAME;
break;
case 'oldest':
$option_ontop = 3;
$questions = Question::find_oldest_questions();
$page_title = 'Showing oldest questions! - '.SITE_NAME;
break;
case 'views':
$option_ontop = 4;
$questions = Question::find_most_viewed();
$page_title = 'Showing most viewed questions! - '.SITE_NAME;
break;
}
}
if($option_ontop > 0) {
$new_option_sort = array($options[$option_ontop]);
unset($options[$option_ontop]);
$new_option_sort = array_merge($new_option_sort, $options);
}
$sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">"
foreach($new_option_sort AS $option) {
$sortResults .= $option;
}
$sortResults .= "</select>";
?>
答案 2 :(得分:1)
我使用通用函数并将您需要的排序条件作为变量提供,而不是定义一个全新的函数:
$questions = Question::find($criteria);
switch($_GET['sort'])
{
case 'answers':
$text = 'most answered';
break;
case 'oldest':
$text = 'oldest';
break;
case 'recent':
$text = 'most recent';
break;
case 'views':
$text = 'most viewed';
break;
default:
$text = 'most voted';
break;
}
$page_title = "Showing $text 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>";
在你的班上:
public function find($criteria)
{
switch($_GET['sort'])
{
case 'answers':
$order = 'answers DESC';
break;
case 'oldest':
$order = 'post_date ASC';
break;
case 'recent':
$order = 'post_date DESC';
break;
case 'views':
$order = 'views DESC';
break;
default:
$order = 'votes DESC';
break;
}
// Use your $order variable for MySQL call
}
答案 3 :(得分:1)
我试图用最干净的方式来做到这一点,我知道如何做到这一点。让我知道你的想法。
<?php
// set $sort to $_GET var or nothing if it's not set
$sort = isset($_GET['sort']) ? $_GET['sort'] : '';
// Options Array
$options = array(
'answers' => array('Most Answers',0),
'oldest' => array('Oldest',0),
'recent' => array('Most Recent',0),
'views' => array('Most Viewed',0),
'votes' => array('Most Voted',0)
);
switch($sort) {
case 'answers':
$questions = Question::find_most_answered();
$options[$sort][1] = 1;
break;
case 'oldest':
$questions = Question::find_oldest_questions();
$options[$sort][1] = 1;
break;
case 'recent':
$questions = Question::find_recent_questions();
$options[$sort][1] = 1;
break;
case 'views':
$questions = Question::find_most_viewed();
$options[$sort][1] = 1;
break;
case 'votes':
$questions = Question::find_most_voted();
$options[$sort][1] = 1;
break;
default:
$questions = Question::find_recent_questions();
break;
}
?>
<select class="sortResults" name="sortResult" id="sortResult">
<?php foreach($options as $key => $val) { ?>
<option value="questions.php?sort=<?php echo $key; ?>" <?php if($val[1] == 1) { echo "selected=\"selected\""; } ?>>Sorted By <?php echo $val[0]; ?></option>
<?php } ?>
</select>
<!-- more code... -->
答案 4 :(得分:0)
我会将每个选项作为字符串添加到顶部的数组中,基于$ _GET - 选择要显示的数组部分。这样你就可以对顶部的数组进行一次更改,它将修复并更新显示它的所有部分。要清理if语句地狱,你需要了解切换案例。
答案 5 :(得分:0)
感谢大家的帮助,我从大家那里得到了一个bi,这是我的最终代码。 我没有为我所有不同的排序类型提供5种不同的功能,而是将Jamie的想法answer实现如下......
switch($sort){
case 'newest':
$order = "ORDER BY created DESC, votes DESC, total_answers DESC ,views DESC ";
break;
case 'oldest':
$order = "ORDER BY created ASC, votes DESC, total_answers DESC ,views DESC ";
break;
case 'answers':
$order = "ORDER BY total_answers DESC, votes DESC, views DESC ";
break;
case 'votes':
$order = "ORDER BY votes DESC, total_answers DESC, views DESC ";
break;
case 'views':
$order = "ORDER BY views DESC, votes DESC, total_answers DESC ";
break;
default:
$order = "ORDER BY votes DESC, total_answers DESC, views DESC "; // Perhaps display 404
break;
}
然后我接受了Brad的idea并创建了一个数组,如下所示......
$sorts = array(
'votes'=>'most voted',
'answers'=>'most answered',
'views'=>'most viewed',
'newest'=>'newest',
'oldest'=>'oldest'
);
然后我最后实现了以下代码......
$sort = isset($_GET['sort']) && !empty($_GET['sort']) ? $_GET['sort'] : 'votes';
$questions = Question::find_by($sort);
$page_title = "Showing {$sorts[$sort]} questions! - ".SITE_NAME;
$sortResults = "<select class=\"sortResults\" name=\"sortResult\" id=\"sortResult\">";
foreach($sorts as $value=>$display){
if($value == $sort){
$sortResults .= "<option value=\"questions.php?sort={$value}\" selected=\"selected\" \">Sorted by {$value}</option>";
}else{
$sortResults .= "<option value=\"questions.php?sort={$value}\">Sort by {$value}</option>";
}
}
$sortResults .= "</select>";
再次感谢所有帮助过我的人!