php - Display the events for the current month first then the rest last desc
554
I have this controller which displays my events by event_date the date format is like 'Y-m-d'
Question Is there away that I could display the events for this month first in the list then the rest of the events after?
Model
public function getevents($filter = array()) {
$this->db->limit($filter['limit'], $filter['start']);
$this->db->order_by($filter['order'], $filter['sort']);
$query = $this->db->get('event');
return $query->result_array();
}
Controller
<?php
class Events extends Admin_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->model('admin/event/events_model');
$this->load->library("pagination");
}
public function index() {
$this->getlist();
}
public function getlist() {
$this->document->set_title('Events');
$data['heading_title'] = 'Events';
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => 'Dashboard',
'active' => '',
'href' => site_url('admin/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => 'Events',
'active' => 'active',
'href' => site_url('admin/events')
);
if ($this->input->get('order')) {
$order = $this->input->get('order');
} else {
$order = 'event_date';
}
if ($this->input->get('sort')) {
$sort = $this->input->get('sort');
} else {
$sort = 'desc';
}
if ($this->input->get('limit')) {
$limit = $this->input->get('limit');
} else {
$limit = 5;
}
if ($this->uri->segment(3)) {
$page = $this->uri->segment(3);
} else {
$page = 0;
}
$config["base_url"] = base_url('admin/events');
$config["total_rows"] = $this->events_model->total_count();
$config["per_page"] = $limit;
$config["uri_segment"] = 3;
$config['use_page_numbers'] = FALSE;
$config['full_tag_open'] = '<nav><ul >';
$config['full_tag_close'] = '</ul></nav>';
$config['num_tag_open'] = '<li ><span >';
$config['num_tag_close'] = '</span></li>';
$config['cur_tag_open'] = '<li ><span >';
$config['cur_tag_close'] = '<span >(current)</span></span></li>';
$config['next_tag_open'] = '<li ><span >';
$config['next_tagl_close'] = '<span aria-hidden="true">»</span></span></li>';
$config['prev_tag_open'] = '<li ><span >';
$config['prev_tagl_close'] = '</span></li>';
$config['first_tag_open'] = '<li ><span >';
$config['first_tagl_close'] = '</span></li>';
$config['last_tag_open'] = '<li ><span >';
$config['last_tagl_close'] = '</span></li>';
$this->pagination->initialize($config);
$filter = array(
'order' => $order,
'sort' => $sort,
'limit' => $limit,
'start' => $page
);
$data['events'] = array();
$events = $this->events_model->getevents($filter);
foreach ($events as $event) {
$button = array(
'type' => 'button',
'class' => 'btn btn-danger',
'content' => '<i aria-hidden="true"></i> Remove',
'data-toggle' => 'model',
'data-target' => '#event_model',
'data-id' => $event['event_id']
);
$data['events'][] = array(
'event_title' => $event['event_title'],
'event_date' => date('l dS F Y', strtotime($event['event_date'])),
'href' => anchor('admin/events/update/' . $event['event_id'], 'Edit', array('class' => 'btn btn-primary')),
'delete' => form_button($button)
);
}
$data["pagination_links"] = $this->pagination->create_links();
$data['header'] = Modules::run('admin/common/header/index');
$data['column_left'] = Modules::run('admin/common/column_left/index');
$data['footer'] = Modules::run('admin/common/footer/index');
$data['topnavbar'] = Modules::run('admin/common/topnavbar/index');
$this->load->view('template/event/getlist', $data);
}
}
Answer
Solution:
uasort()
You don't need dedicated year and month table columns for this. Using native MySQL and PHP functions on date format, you can achieve almost any real world example algorithm. Here is what you should do to avoid unnecessary or overwhelming DB calls.
I assume that in this line
you are getting
$events
that could be something like thisI didn't represent all fields but few needed for understanding principle
This should be the way of how you can sort elements by using child key in multidimensional array, and it is way better to use PHP for array operations than having multiple calls to database.
Also, PHP shows really great strength in work over arrays so you should consider using those functions and classes before making more DB calls than you actually need.
Edit:
Just got better resulst with this one:
Answer
Solution:
You could add
$this->db->where('event_date >=', date('Y-m-01'));
to yourgetevents()
method, I guess.Answer
Solution:
try replacing the following line of code:
by the following line:
And check if your event_date format is ok.
Answer
Solution:
Solution
I think just creating two arrays was the better option and then merge them. I had to create another column for a separate year and month
By adding the where() worked and or_where() then using array_merge