Answer the question
In order to leave comments, you need to log in
How to group posts by date for display?
Good evening, tell me, I want to group data for such a display:
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; ?>
<?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; ?>
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();
$this->db->order_by("posts.post_date");
foreach
? thank you!
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question