2015-09-11 147 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数据库更好/更快/更容易的所有更多的原因。