2012-03-06 55 views
2

在我創建的論壇的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>"; 
     } 

請幫助我,如果你可以,並編輯它,使其看起來更好。我不是這方面的專家。

非常感謝!

+0

帶環的數組 – 2012-03-06 21:14:47

+0

使用開關模型[http://php.net/manual/en/control-structures.switch。php] – AlexC 2012-03-06 21:16:51

+0

你可能已經爲我所做的所有好事寫下了'箍ha'。不管怎麼說,還是要謝謝你! – yehuda 2012-03-06 21:17:10

回答

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

大概是我該如何解決它。

  1. 陣列中的地點選項,你可以基於該數組中的鍵引用
  2. 驗證$_GET(和默認值,如果它是一個無效或免稅進入)基於該$sort
  3. 輸出選項。
+0

哎呀,剛剛注意到靜態'問::調用更改,現在修復它... – 2012-03-06 21:24:12

+0

要玩一會兒,歡呼聲,儘快回覆。 – yehuda 2012-03-06 21:24:20

+0

'option'標記'selected'的html屬性將使默認選擇選項被選中......這樣你就可以維護順序並因此獲得一致的用戶體驗。重組訂單隻會使下拉不一致。 – Brendan 2012-03-06 21:26:27

0

我會將每個選項作爲頂部數組中的字符串添加,基於$ _GET - 選擇要顯示的數組的哪一部分。這樣,您可以在頂部的數組上進行一次更改,它將修復和更新顯示它的所有部件。要清理if語句,你需要了解switch case。

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>"; 
?> 
+0

只是玩弄代碼,謝謝,很快見到你。 – yehuda 2012-03-06 21:26:29

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 
} 
+0

我想知道,這是最佳做法嗎?似乎是一個好主意。 – yehuda 2012-03-06 22:04:53

+0

非常感謝,希望未來能實現這一點。不勝感激! – yehuda 2012-03-06 22:16:39

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... --> 
+0

真的很不錯的代碼,現在只需檢查它。乾杯 – yehuda 2012-03-06 22:13:02

0

感謝大家的全力幫助,我從每個人身上取走了一張bi,這是我的最終代碼。 而不必爲我所有的不同類型的類型5個不同的功能,我已經實現了傑米的想法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; 
    } 

然後我採取了布拉德的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>"; 

再次感謝大家誰幫助過我!