2015-09-11 150 views
4

我正在爲我的論壇創建一個「固定」功能,我正在尋找一種方法將固定的主題放在數組的開頭,以便它們「卡」到頁面的頂部。按值排序數組

如果該主題未被固定,則topic_pinned=NULL如果被固定則爲topic_pinned=0

該數組是排序方式topic_updated。固定的主題需要保持排序topic_updated,同時停留在頁面的頂部,然後在固定主題下是非固定主題,這些主題也按topic_updated排序。

主題陣列($forum_topic_results):

Array 
(
    [0] => Array 
     (
      [topic_id] => 4 
      [topic_subject] => Test Subject #4 
      [topic_date] => 2015-09-10 18:34:18 
      [topic_by] => 1 
      [topic_pinned] => 
      [topic_updated] => 2015-09-10 20:37:22 
     ) 

    [1] => Array 
     (
      [topic_id] => 3 
      [topic_subject] => Test Subject #3 
      [topic_date] => 2015-08-22 09:24:40 
      [topic_by] => 1 
      [topic_pinned] => 0 
      [topic_updated] => 2015-09-04 22:02:31 
     ) 

    [2] => Array 
     (
      [topic_id] => 2 
      [topic_subject] => Test Subject #2 
      [topic_date] => 2015-08-15 10:56:00 
      [topic_by] => 1 
      [topic_pinned] => 
      [topic_updated] => 2015-09-04 19:45:32 
     ) 

    [3] => Array 
     (
      [topic_id] => 1 
      [topic_subject] => Test Subject #1 
      [topic_date] => 2015-08-30 19:48:17 
      [topic_by] => 1 
      [topic_pinned] => 0 
      [topic_updated] => 2015-09-03 00:44:38 
     ) 
) 

PHP:

/** 
* getAllTopics 
* 
* Retreives the topics of the chosen category from the `forum_topics` table. 
* 
* @param $cat_id 
* @access public 
*/ 
public function getAllTopics($cat_id=NULL) 
{ 
    $database=$this->database; 

    $database->query('SELECT topic_id, topic_subject, topic_date, topic_by, topic_pinned, topic_locked FROM forum_topics WHERE topic_cat = :catid ORDER BY topic_updated DESC', array(':catid' => $cat_id)); 
    $result = $database->statement->fetchAll(PDO::FETCH_ASSOC); 

    return $result; 
} 

# Get topics 
$forum_topic_results = $this->getAllTopics($_GET['cat']); 
foreach($forum_topic_results as $forum_topic_row) 
{ 
    # Get user's username. 
    $topic_by=SearchUser($forum_topic_row['topic_by']); 

    $data.='<tr>'. 
     '<td>'. 
      '<h3><a href="'.$_SERVER['PHP_SELF'].'?action=forum_posts&topic='.$forum_topic_row['topic_id'].'">'.$forum_topic_row['topic_subject'].'</a></h3>'. 
      'by '.$topic_by['username'].' on '.date('D M d, Y g:i a', strtotime($forum_topic_row['topic_date'])). 
     '</td>'. 
    '</tr>'; 
} 

結果我想:

Array 
(
    [0] => Array 
     (
      [topic_id] => 3 
      [topic_subject] => Test Subject #3 
      [topic_date] => 2015-08-22 09:24:40 
      [topic_by] => 1 
      [topic_pinned] => 0 
      [topic_updated] => 2015-09-04 22:02:31 
     ) 

    [1] => Array 
     (
      [topic_id] => 1 
      [topic_subject] => Test Subject #1 
      [topic_date] => 2015-08-30 19:48:17 
      [topic_by] => 1 
      [topic_pinned] => 0 
      [topic_updated] => 2015-09-03 00:44:38 
     ) 

    [2] => Array 
     (
      [topic_id] => 4 
      [topic_subject] => Test Subject #4 
      [topic_date] => 2015-09-10 18:34:18 
      [topic_by] => 1 
      [topic_pinned] => 
      [topic_updated] => 2015-09-10 20:37:22 
     ) 

    [3] => Array 
     (
      [topic_id] => 2 
      [topic_subject] => Test Subject #2 
      [topic_date] => 2015-08-15 10:56:00 
      [topic_by] => 1 
      [topic_pinned] => 
      [topic_updated] => 2015-09-04 19:45:32 
     ) 
) 
+2

你想要什麼,預期結果是什麼?你也試過這麼做嗎?請粘貼您的代碼努力也。 –

+0

你想根據'topic_updated'對數組進行排序嗎? –

+1

[參考:在PHP中對數組和數據進行排序的所有基本方法]的可能重複(http://stackoverflow.com/questions/17364127/reference-all-basic-ways-to-sort-arrays-and-data-in -php) –

回答

4

做antother方式。

馬克在你的數據庫表PINNED爲1

並非針對not pinned爲0

之後,只需添加(替換ORDER BY)到您的查詢,其中u選擇主題:

ORDER BY `topic_pinned` DESC, `topic_updated` DESC 
+0

不計算日期! –

+0

真的嗎?關鍵是要保持固定的主題。你只讀了關於如何排序數組。但我提供的解決方案是通過將邏輯傳遞給sql來避免這種情況(使用一行代碼)。 – M0rtiis

+0

沒有他的觀點,你至少需要ORDER BY topic_pinned,topic_updated – gview

0

這應該工作:

<?php 
$finalArr = $forum_topic_results; 
foreach ($finalArr as $key => $row) { 
    $topic_pinned[$key] = $row['topic_pinned']; 
} 
array_multisort($topic_pinned, SORT_DESC, $finalArr); 
return $finalArr; 
?> 

有關array_multisort的更多信息:array_multisort

+0

我不知道'array_multisort()',謝謝。 – Draven

+0

對我來說,這是缺少topic_updated的比較。如果數據預先應該按日期排序,那麼這可能是好的。 – gview

+0

@gview是的..這也應該工作..但如果可能的話,我們應該只使用查詢.. –

1

只是爲了完整性,如果你實際上仍然希望或需要通過PHP來做到這一點,我可能會選擇使用usort。它不需要製造柱狀陣列。

usort允許您編寫函數或提供執行比較邏輯的匿名函數。既然你有數組的數組,比較需要使用幾個數組元素做的,你可以很容易地編寫一個簡單的比較功能,只要你記住:

比較函數必須返回一個整數如果第一個參數被認爲是分別小於,等於或大於第二個的 ,則小於,等於或大於零。

像這樣的東西(即興,未測試的代碼)

function cmp($a, $b) { 
    if ($a['topic_pinned'] == $b['topic_pinned']) { 
     // Have to compare Dates 
     $adate = new DateTime($a['topic_updated']); 
     $bdate = new DateTime($b['topic_updated']); 
     return ($adate < $bdate) ? -1 : 1; 
    } elseif ($a['topic_pinned'] == null) { 
     return 1; 
    } else { 
     return -1; 
    } 
} 

usort($array, 'cmp'); 

正如你可以看到這個變得複雜快,由於需要將日期時間字符串轉換成datetime對象,這樣就可以實際做比較。

爲什麼在這種情況下使用sql數據庫更好/更快/更容易的所有更多的原因。