N
N
Nick_Adams2015-11-15 20:23:33
CodeIgniter
Nick_Adams, 2015-11-15 20:23:33

How to group posts by date for display?

Good evening, tell me, I want to group data for such a display:
32fed4368c3c4bfb8dbeb241b83c4cc3.png
I have such a 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; ?>

i am trying to rewrite foreach to display date:
<?php if (!empty($stories)): ?>

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

   ** сдесь должен быть див, который отображается перед каждой "группировкой" постов **

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

and this is my post model (stories)
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("posts.post_date");
$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();

I understand that I need to show posts by date by uncommenting this:
$this->db->order_by("posts.post_date");
But how should my foreach? thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav, 2015-11-15
@Nick_Adams

can be on the first iteration of foreach to write down date in a variable. then compare the current iteration date with the one in the variable
then a simple if date !equal to date then show divs and update the variable
group by date in the model query is definitely impossible

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question