首页 > 解决方案 > Codeigniter Repotrs 页面的日期问题

问题描述

我正在使用 Codeigniter 框架开发一个项目,并且在报告页面中我需要一个具有数据表结构的表单,以便我可以按日、月和年生成报告。但我不知道如何使用数据表。我试图添加代码,例如它的编写位置 Ym 我添加了 dmy 但它不起作用

报告控制器:

defined('BASEPATH') OR exit('No direct script access allowed');

class Reports extends Admin_Controller 
{   
    public function __construct()
    {
        parent::__construct();
        $this->data['page_title'] = 'Stores';
        $this->load->model('model_reports');
    }

    /* 
    * It redirects to the report page
    * and based on the year, all the orders data are fetch from the database.
    */
    public function index()
    {
        if(!in_array('viewReports', $this->permission)) {
            redirect('dashboard', 'refresh');
        }

        $today_year = date('Y');

        if($this->input->post('select_year')) {
            $today_year = $this->input->post('select_year');
        }

        $parking_data = $this->model_reports->getOrderData($today_year);
        $this->data['report_years'] = $this->model_reports->getOrderYear();


        $final_parking_data = array();
        foreach ($parking_data as $k => $v) {

            if(count($v) > 1) {
                $total_amount_earned = array();
                foreach ($v as $k2 => $v2) {
                    if($v2) {
                        $total_amount_earned[] = $v2['net_amount'];                     
                    }
                }
                $final_parking_data[$k] = array_sum($total_amount_earned);  
            }
            else {
                $final_parking_data[$k] = 0;    
            }

        }

        $this->data['selected_year'] = $today_year;
        $this->data['company_currency'] = $this->company_currency();
        $this->data['results'] = $final_parking_data;

        $this->render_template('reports/index', $this->data);
    }
}   

**Reports Model:** 

<?php 

class Model_reports extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /*getting the total months*/
    private function months()
    {
        return array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
    }

    /* getting the year of the orders */
    public function getOrderYear()
    {
        $sql = "SELECT * FROM orders WHERE paid_status = ?";
        $query = $this->db->query($sql, array(1));
        $result = $query->result_array();

        $return_data = array();
        foreach ($result as $k => $v) {
            $date = date('Y', $v['date_time']);
            $return_data[] = $date;
        }

        $return_data = array_unique($return_data);

        return $return_data;
    }

    // getting the order reports based on the year and moths
    public function getOrderData($year)
    {   
        if($year) {
            $months = $this->months();

            $sql = "SELECT * FROM orders WHERE paid_status = ?";
            $query = $this->db->query($sql, array(1));
            $result = $query->result_array();

            $final_data = array();
            foreach ($months as $month_k => $month_y) {
                $get_mon_year = $year.'-'.$month_y; 

                $final_data[$get_mon_year][] = '';
                foreach ($result as $k => $v) {
                    $month_year = date('Y-m', $v['date_time']);

                    if($get_mon_year == $month_year) {
                        $final_data[$get_mon_year][] = $v;
                    }
                }
            }   


            return $final_data;

        }
    }
}

**View index** 



  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        Reports
      </h1>
      <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
        <li class="active">Reports</li>
      </ol>
    </section>

    <!-- Main content -->
    <section class="content">
      <!-- Small boxes (Stat box) -->
      <div class="row">

        <div class="col-md-12 col-xs-12">
          <form class="form-inline" action="<?php echo base_url('reports/') ?>" method="POST">
            <div class="form-group">
              <label for="date">Year</label>
              <select class="form-control" name="select_year" id="select_year">
                <?php foreach ($report_years as $key => $value): ?>
                  <option value="<?php echo $value ?>" <?php if($value == $selected_year) { echo "selected"; } ?>><?php echo $value; ?></option>
                <?php endforeach ?>
              </select>
            </div>
            <button type="submit" class="btn btn-default">Submit</button>
          </form>
        </div>

        <br /> <br />


        <div class="col-md-12 col-xs-12">

          <?php if($this->session->flashdata('success')): ?>
            <div class="alert alert-success alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <?php echo $this->session->flashdata('success'); ?>
            </div>
          <?php elseif($this->session->flashdata('error')): ?>
            <div class="alert alert-error alert-dismissible" role="alert">
              <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
              <?php echo $this->session->flashdata('error'); ?>
            </div>
          <?php endif; ?>

          <div class="box">
            <div class="box-header">
              <h3 class="box-title">Total - Report</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <div class="chart">
                <canvas id="barChart" style="height:250px"></canvas>
              </div>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
          <div class="box">
            <div class="box-header">
              <h3 class="box-title">Total Paid Orders - Report Data</h3>
            </div>
            <!-- /.box-header -->
            <div class="box-body">
              <table id="datatables" class="table table-bordered table-striped">
                <thead>
                <tr>
                  <th>Month - Year</th>
                  <th>Amount</th>
                </tr>
                </thead>
                <tbody>

                  <?php foreach ($results as $k => $v): ?>
                    <tr>
                      <td><?php echo $k; ?></td>
                      <td><?php 

                        echo $company_currency .' ' . $v;
                        //echo $v;

                      ?></td>
                    </tr>
                  <?php endforeach ?>

                </tbody>
                <tbody>
                  <tr>
                    <th>Total Amount</th>
                    <th>
                      <?php //echo $company_currency . ' ' . array_sum($parking_data); ?>
                      <?php echo array_sum($results); ?>
                    </th>
                  </tr>
                </tbody>
              </table>
            </div>
            <!-- /.box-body -->
          </div>
          <!-- /.box -->
        </div>
        <!-- col-md-12 -->
      </div>
      <!-- /.row -->


    </section>
    <!-- /.content -->
  </div>
  <!-- /.content-wrapper -->

  <script type="text/javascript">

    $(document).ready(function() {
      $("#reportNav").addClass('active');
    }); 

    var report_data = <?php echo '[' . implode(',', $results) . ']'; ?>;


    $(function () {
    /* ChartJS
     * -------
     * Here we will create a few charts using ChartJS
     */
     var areaChartData = {
      labels  : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
      datasets: [
        {
          label               : 'Electronics',
          fillColor           : 'rgba(210, 214, 222, 1)',
          strokeColor         : 'rgba(210, 214, 222, 1)',
          pointColor          : 'rgba(210, 214, 222, 1)',
          pointStrokeColor    : '#c1c7d1',
          pointHighlightFill  : '#fff',
          pointHighlightStroke: 'rgba(220,220,220,1)',
          data                : report_data
        }
      ]
    }

    //-------------
    //- BAR CHART -
    //-------------
    var barChartCanvas                   = $('#barChart').get(0).getContext('2d')
    var barChart                         = new Chart(barChartCanvas)
    var barChartData                     = areaChartData
    barChartData.datasets[0].fillColor   = '#00a65a';
    barChartData.datasets[0].strokeColor = '#00a65a';
    barChartData.datasets[0].pointColor  = '#00a65a';
    var barChartOptions                  = {
      //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value
      scaleBeginAtZero        : true,
      //Boolean - Whether grid lines are shown across the chart
      scaleShowGridLines      : true,
      //String - Colour of the grid lines
      scaleGridLineColor      : 'rgba(0,0,0,.05)',
      //Number - Width of the grid lines
      scaleGridLineWidth      : 1,
      //Boolean - Whether to show horizontal lines (except X axis)
      scaleShowHorizontalLines: true,
      //Boolean - Whether to show vertical lines (except Y axis)
      scaleShowVerticalLines  : true,
      //Boolean - If there is a stroke on each bar
      barShowStroke           : true,
      //Number - Pixel width of the bar stroke
      barStrokeWidth          : 2,
      //Number - Spacing between each of the X value sets
      barValueSpacing         : 5,
      //Number - Spacing between data sets within X values
      barDatasetSpacing       : 1,
      //String - A legend template
      legendTemplate          : '<ul class="<%=name.toLowerCase()%>-legend"><% for (var i=0; i<datasets.length; i++){%><li><span style="background-color:<%=datasets[i].fillColor%>"></span><%if(datasets[i].label){%><%=datasets[i].label%><%}%></li><%}%></ul>',
      //Boolean - whether to make the chart responsive
      responsive              : true,
      maintainAspectRatio     : true
    }

    barChartOptions.datasetFill = false
    barChart.Bar(barChartData, barChartOptions)
  })
  </script>

标签: codeigniterdatatables

解决方案


推荐阅读