按日/月分组帖子

时间:2015-10-22 09:45:17

标签: php codeigniter date foreach

我有故事列表,并希望按天分组,如下:

enter image description here

这是我的foreach

<?php if (!empty($stories)): ?>

   <?php foreach($stories as $sto): ?>

    <a href="<?php echo $sto['post_url']; ?>" target="_blank" class="story-link"><?php echo $sto['post_subject']; ?></a>
<time datetime="<?php echo $sto['post_date']; ?>"><?php echo $this->stories_model->calculartempo($sto['post_date'], date("Y-m-d H:i:s")); ?></time>

   <?php endforeach; ?>

<?php else: ?>  

   <span>No stories</span>

<?php endif; ?>

我正在尝试添加新的foreach $stories as $key => $sto

<?php if (!empty($stories)): ?>

<?php foreach($stories as $key => $sto): ?>

   ** here as o understand must be date ( how i can get this date? ) **

   <?php foreach($sto as $sstory): ?>

    <a href="<?php echo $sstory['post_url']; ?>" target="_blank" class="story-link"><?php echo $sstory['post_subject']; ?></a>
<time datetime="<?php echo $sstory['post_date']; ?>"><?php echo $this->stories_model->calculartempo($sstory['post_date'], date("Y-m-d H:i:s")); ?></time>

   <?php endforeach; ?>

<?php endforeach; ?>

<?php else: ?>  

   <span>No stories</span>

<?php endif; ?>

这是我从DB中获取的故事模型:

public function get_stories($offset = null, $search = "", $filter =  "Popular", $category = "") 
{

$ui = $this->session->userdata('userid');
if ($ui == "") $ui = 0;

$this->db->select('posts.*, users.*, COUNT(post_comments.posts_id) AS numbercomments, COUNT(posts_votes.vote_id) AS numbervotes, categories_posts.post_id as postid, categories.category_name, categories.id_category, favourites.posts_id as favourite'); 


$this->db->join('users', 'posts.post_by = users.user_id', 'left');
$this->db->join('post_comments', 'posts.post_id = post_comments.posts_id', 'left');
$this->db->join('posts_votes', 'posts_votes.vote_postid = posts.post_id', 'left');
//$this->db->order_by("post_date", "desc"); 
$this->db->join('categories_posts', 'categories_posts.post_id = posts.post_id', 'left');
$this->db->join('categories', 'categories.id_category = categories_posts.id_category', 'left');

$this->db->join('favourites', "posts.post_id = favourites.posts_id AND favourites.user_id=$ui", 'left');        

if ($category != "") {
    $this->db->where('categories_posts.id_category', $category);
}

$this->db->group_by("posts.post_id");       

if ($filter == "Recent") {
    $this->db->order_by("post_date", "desc");
} else if ($filter == "Most Comment") {
    $this->db->order_by("numbercomments", "desc");
} else {
    $this->db->order_by("numbervotes", "desc");         
}

if (strlen($search)>1) {
    $this->db->like('posts.post_subject', $search);         
}

$this->db->where('posts.approved', 1);
//$query = $this->db->get(); 
$query = $this->db->get('posts', 10, $offset);
//$this->output->enable_profiler(TRUE);     
return $query->result_array();

}

我的数据库查询SELECT post_id, post_date FROM posts

Posts
--------------------------------
post id:72 
post date: 2015-10-21 23:11:19 
--------------------------------
post id:73 
post date: 2015-10-22 11:51:54 
--------------------------------
post id:74 
post date: 2015-10-22 11:54:30 
--------------------------------
post id:75 
post date: 2015-10-22 13:10:54 
--------------------------------

但这不适合我,问题是什么?谢谢!

0 个答案:

没有答案