Quantcast
Channel: CodeIgniter Forums - All Forums
Viewing all articles
Browse latest Browse all 14348

Display data week by week

$
0
0
On my model function it gets the total number of users that have joined up for this day in the week and then sets it on chart.

Sun, Mon, Tue, Wed, Thu, Fri, Sat

[Image: chart_dash.png]

On my database table I have two users that have joined up on the 19th of aug and one on 21

My model function currently prints code like.


Code:
{m: 'Sun', a: 0},

{m: 'Mon', a: 0},

{m: 'Tue', a: 1}, // 2016 - 08 - 21

{m: 'Wed', a: 0},

{m: 'Thu', a: 0},

{m: 'Fri', a: 2}, // 2016 - 08 - 19

{m: 'Sat', a: 0},

Because my weeks start on each Sunday it should only display something like

Code:
{m: 'Sun', a: 0},

{m: 'Mon', a: 0},

{m: 'Tue', a: 1}, // 2016 - 08 - 21

{m: 'Wed', a: 0},

{m: 'Thu', a: 0},

{m: 'Fri', a: 0},

{m: 'Sat', a: 0},


Question: On my model what would be best solution to be able to get and display correct result

PHP Code:
public function getUserTotalByWeek() {
    $date_start strtotime('-' date('w') . ' days');

    $user_data = array();

    $this->db->select('*, COUNT(*) AS total');
    $this->db->from($this->db->dbprefix 'user');
    $this->db->where('DATE(date_reg) >='$date_start);
    $this->db->group_by('DAYNAME(date_reg)');
    $query $this->db->get();

    for ($i 0$i 7$i++) {
        $date date('Y-m-d'$date_start + ($i 86400));
        $user_data[date('w'strtotime($date))] = array(
            'day'   => date('D'strtotime($date)),
            'total' => 0
        
);
    }

    foreach ($query->result_array() as $result) {
        $user_data[date('w'strtotime($result['date_reg']))] = array(
            'day'   => date('D'strtotime($result['date_reg'])),
            'total' => $result['total']
        );
    }

    return $user_data;



Controller index function


PHP Code:
public function index() {

    $data['results'] = array();

    if ($this->input->post('range')) {
        $range $this->input->post('range');
    } else {
        $range 'week';
    }

    switch ($range) {
        case 'week':
            $results $this->getUserTotalByWeek();

            foreach ($results as $result) {
                $data['results'][] = array(
                    'data' => "\n" "{m: " "'" $result['day'] . "'" ', ' 'a: '$result['total'] . '},' "\n"
                );
            }

            break;
        case 'month':
            # code...
            break;    
    
}

    return $this->load->view('template/dashboard/chart_view'$data);


Thank you for your time

.php   Chart.php (Size: 1.44 KB / Downloads: 5)

Viewing all articles
Browse latest Browse all 14348

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>