Welcome Guest, Not a member yet? Register   Sign In
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
#5

hi may i ask if do you already have a code of dynamic charts with filter date range php?
Reply
#6

(This post was last modified: 09-06-2023, 10:29 AM by ozornick. Edit Reason: Format code )

From the data recieved from the api, it contains a data with date '2017-02-28T00:00:00Z'; when you specify the start_date variable like this $sdate='2017-01-28T00:00:00Z'; and the end_date variable like this $edate='2017-02-28T00:00:00Z'; you get the data returned.

When you specify it like this $sdate='2017-01-28T00:00:00Z'; and the end date as this $edate='2017-03-28T00:00:00Z'; it bring out no data.


PHP Code:
$baseURL 'https://myip/DynamicsNAV90/WS/';
$pageURL $baseURL.rawurlencode($cur).'/Page/info';

$id='0101001';
$sdate='2017-01-28T00:00:00Z';
$edate='2017-03-28T00:00:00Z';
$error_msg=array("message"=>("Bill not found"));
//echo $sdate;
$params = array('filter' => array(
                                array('Field' => 'ID''Criteria' => $id),
                                array('Field' => 'Date''Criteria' => $sdate),
                                array('Field' => 'Date''Criteria' => $edate)
                                ),
                    'setSize' => 0);
$result $page->ReadMultiple($params);
$customers $result->ReadMultiple_Result->BillingInfo;

if ( 
$customers->Date >= $sdate && $customers->Date <=$edate)
{
    echo json_encode(array($customers));
}else{
    echo json_encode($error_msg);


what i'm trying to do is to search in between, i could have done this by writing a select statement on the database and specify the date ranges but since i'm only pulling from a web api its really difficult
Reply




Theme © iAndrew 2016 - Forum software by © MyBB