• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic chart Date range to filter records with PHP MySQL

#1
Good Morning,

I have been filtering for a dynamic chart for more than two weeks.
Everything works very well.
After choosing the dates you hardly print anything.
Please leave the code here.


CONTROLLER

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Incidencias_ministerio extends CI_Controller {

    public function __construct()
    {
        parent::__construct();    
        $this->load->model('incidencias_ministerio_model');
        
    }

    public function index()
    {
        //ifPermissions('incidencias_ministerio');
        $page_data['incidencias_ministerio'] = $this->incidencias_ministerio_model->get_nombre();
        $page_data['view'] = 'chart/incidencias_ministerio';
        $this->load->view('includes/layout', $page_data);
    }

    public function select_by_date_range() {
        $date1 = $this->input->post('date_from');
        $date2 = $this->input->post('date_to');
        $data = array(
        'date1' => $date1,
        'date2' => $date2
        );
        if ($date1 == "" || $date2 == "") {
        $data['date_range_error_message'] = "Both date fields are required";
        } else {
        $result = $this->incidencias_ministerio_model->show_data_by_date_range($data);
        if ($result != false) {
        $data['incidencias_ministerio'] = $result;
        } else {
        $data['incidencias_ministerio'] = "No record found !";
        }
        }
        $data['view'] = 'chart/incidencias_ministerio';
        $this->load->view('includes/layout', $data);
    }
}

MODEL

Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Incidencias_ministerio_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    
    function get_nombre() {

        $this->db->group_by('name_ministerio');
        $this->db->select('name_ministerio');
        $this->db->join('ministerios', 'ministerios.id = graficas.ministerio_id','inner');
        $this->db->select("count(*) as total");
        return $this->db->from('graficas')
          ->get()
          ->result();
    }

    public function show_data_by_date_range($data) {
        $condition = "add_date BETWEEN " . "'" . $data['date1'] . "'" . " AND " . "'" . $data['date2'] . "'";
        $this->db->group_by('name_ministerio');
        $this->db->select('name_ministerio');
        $this->db->join('ministerios', 'ministerios.id = graficas.ministerio_id');
        $this->db->join('tecnicos', 'tecnicos.id = graficas.tecnico_id');
        $this->db->from('graficas');
        $this->db->where($condition);
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return false;
        }
    }
    
}

VIEW
I'm sure the error comes from the view

Code:
<!-- Load file plugin Chart.js -->     
<script src="<?= base_url()?>/assets/chart/Chart.js"></script>

<div id="show_form" style="margin-bottom: 69px;">
    <div class="col-md-12">
        <?php
            echo form_open('incidencias_ministerio/select_by_date_range');
                echo'<div class="col-md-2">';
                    echo form_label('Buscar De : ');
                echo'</div>';

                echo'<div class="col-md-3">';
                    $data = array(
                    'type' => 'date',
                    'name' => 'date_from',
                    'placeholder' => 'yyyy-mm-dd'
                    );
                    echo form_input($data);
                echo'</div>';
                
                echo'<div class="col-md-1">';
                    echo " A : ";
                echo'</div>';
                echo'<div class="col-md-3">';
                    $data = array(
                    'type' => 'date',
                    'name' => 'date_to',
                    'placeholder' => 'yyyy-mm-dd'
                    );
                    echo form_input($data);
                echo'</div>';

                echo'<div class="col-md-3">';
                    echo form_submit('submit', 'Buscar');
                    echo form_close();
            echo'</div>';
        ?>
    </div>
</div>
    
<!-- /.box-body -->

    <?php
    
    $data_1= "";
    $data_2=null;
    
    foreach ($incidencias_ministerio as $item)
    {
        $data_3=$item->name_ministerio;
        $data_1 .= "'$data_3'". ", ";
        $data_4=$item->total;
        $data_2 .= "$data_4". ", ";
        
    }
    ?>
    <div class="chart">
    <canvas id="myChart"></canvas>
    </div>

<script>
    var ctx = document.getElementById('myChart').getContext('2d');
    var chart = new Chart(ctx, {
       
        type: 'bar',
        
        data: {
            labels: [<?php echo $data_1; ?>],
            datasets: [{
                label:'Incidencia por Ministerio ',
                backgroundColor: [    'rgb(255, 99, 132)',
                                    'rgba(56, 86, 255, 0.87)',
                                    'rgb(60, 179, 113)',
                                    'rgb(175, 238, 239)',
                                    'rgb(255, 99, 132)',
                                    'rgba(56, 86, 255, 0.87)',
                                    'rgb(60, 179, 113)',
                                    'rgb(175, 238, 239)',
                                    'rgb(255, 99, 132)',
                                    'rgba(56, 86, 255, 0.87)',
                                    'rgb(60, 179, 113)',
                                    'rgb(175, 238, 239)'
                                ],
                borderColor: ['rgb(255, 99, 132)'],
                //grafica
                data: [<?php echo $data_2; ?>]
                
            }]
            
        },
        // Configuration options go here
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero:true
                    }
                }]
            }
        }
    });
</script>
Reply

#2
Would be nice if you stated what the ERROR was that you are receiving.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#3
(12-02-2019, 08:58 AM)InsiteFX Wrote: Would be nice if you stated what the ERROR was that you are receiving.

After making the date filter, just print the names but without the diagram bars.

I would like to upload an image but I don't know how to do it in the forum.
Reply

#4
(12-02-2019, 08:58 AM)InsiteFX Wrote: Would be nice if you stated what the ERROR was that you are receiving.

Here is the link: http://cniapge.gq/soporte_cniapge/login

User : test@admin.com
Password: admin

By the search: November 1 - November 30
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.