How to use the between command in codeigniter to generate report

Asked

Viewed 226 times

1

I need to generate the following report:

The user selects the time interval (start date and end date) and the revenue report is generated by displaying the value of the added recipes.

Follow the view photo that calls the function in the Model class:

inserir a descrição da imagem aqui

View Code (photo above):

    <div class="col-md-12 col-xs-11"> 
    <div class="page-header">
        <h4 align="center"><strong>Relatório de Receitas</strong></h4>
    </div>                       
    <form action="<?= base_url() ?>receita/pagas" method="post">
        <div class="row">
            <div class="col-md-4 col-md-offset-5" >                                           
                <label>Data do recebimento entre:</label>
            </div>
        </div>
        <div class="row">
            <div class="col-md-2 col-md-offset-4" >                                           
                <div id="datepicker1" class="input-group date" data-date-format="dd-mm-yyyy">
                    <input id="dtrecinicio" class="form-control" type="text" name="dtrecinicio" required>
                    <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                </div>
                <script type="text/javascript">
                    $(function () {
                        $("#datepicker1").datepicker({
                            autoclose: true,
                            todayHighlight: true,
                            language: "pt-BR"
                        }).dtrecinicio('update', new Date());
                    });
                </script>                
            </div>
            <div class="col-md-1"> 
                <label>E</label>
            </div>
            <div class="col-md-2">                   
                <div id="datepicker2" class="input-group date" data-date-format="dd-mm-yyyy">
                    <input id="dtrecfim" class="form-control" type="text" name="dtrecfim" required>
                    <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
                </div>
                <script type="text/javascript">
                    $(function () {
                        $("#datepicker2").datepicker({
                            autoclose: true,
                            todayHighlight: true,
                            language: "pt-BR"
                        }).dtrecfim('update', new Date());
                    });
                </script>
            </div>
        </div>                
        <br>
        <br>
        <div class="row">            
            <div class="col-md-10 col-xs-12">
                <div class="col-md-3 col-md-offset-6">
                    <div class="form-group">
                        <button class="btn btn-md btn-primary btn-block" type="submit">GERAR</button>                
                    </div>
                </div>                               
            </div>
        </div>
        <div class="row">                 
            <div class="col-md-10 col-xs-12">
                <div class="col-md-3 col-md-offset-6">
                    <div class="form-group">
                        <a class="btn btn-md btn-default btn-block hidden-print" href="<?= base_url(); ?>">CANCELAR</a>
                    </div>
                </div>                                        
            </div>                                        
        </div>
    </form>
</div> 

Function of the Model Class:

    function get_receitas_like() {
        $inicio = $this->input->post('dtrecinicio');
        $fim = $this->input->post('dtrecfim');
//        $this->db->select('*');
        $this->db->select_sum("valor_receita");

        $this->db->where('dtrecebimento' >= $inicio);
        $this->db->where('dtrecebimento' <= $fim);
//        $this->db->where('dtrecebimento' BETWEEN  AND $dtrecebimento2');
//        $this->db->where('dtrecebimento', $termo);
        return $this->db->get('receitas')->result();
    }

View code that displays the added data:

<div class="row">
        <div class="col-md-12">
            <p align="center"><font  size="3" face="helvetica"><u><strong>RELATÓRIO - RECEITAS</strong></u></font></p>                                                           
        </div>
    </div>          
    <div class="row">
        <div class="col-md-5 col-md-offset-4 col-xs-12">
            <div class="table-responsive"> 
                <table class="table table-striped">
                    <tr class="row">                         
                        <td><strong>Valor total recebido no período selecionado:</strong> R$ <?php echo number_format($receitas[0]->valor_receita,2,",","."); ?></td>                                                                                             
                    </tr>                     
                    <tr class="row">                                   
                        <td></td> 
                        <td></td>                                                        
                    </tr>                     
                </table>
            </div>
        </div> 
    </div>

Photo from the Recipes table:

inserir a descrição da imagem aqui

1 answer

2


Some modifications:

If your date is in the correct format the date coming from the screen is not, it needs to be converted to Date and then in SQL passes the corresponding formatting, example:

$inicio = DateTime::createFromFormat('d-m-Y', $this->input->post('dtrecinicio'));
$fim = DateTime::createFromFormat('d-m-Y', $this->input->post('dtrecfim'));

and change:

$this->db->where('dtrecebimento' >= $inicio));
$this->db->where('dtrecebimento' <= $fim);

for

$this->db->where('dtrecebimento >=', $inicio->format('Y-m-d'));
$this->db->where('dtrecebimento <=', $fim->format('Y-m-d'));

the signals have to stay within the first parameter.

Reference: Custom Where

  • Thanks for the feedback @Virgilio Novic only that despite the correct syntax of "Where" the sum is not being done correctly, is generating a very high number, not consistent with the database data.

  • @Ramiro is the following syntax is that for between between values in the date case and your doubt was about it, if the sum is coming different is because something is missing before, you in your question could provide more information, like I have this set of information and the result of the sum is !!! for us can help more... this part of the date has been solved, now SQL before is not clear in the question if you can update the question we can help.

  • OK @Virgilio Novic I was able to solve my problem with the same 'like' because with the between I don’t know why the generated sum was coming wrong. Thanks again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.