Transform the DATA entry into a PHP / JAVA variable

0

I hope you understand the situation: By programming a web page, I have created a data field where the user chooses the date that to filter the information contained in the site, ie only display information that reflects the chosen day or period. Well, how can I turn this date entry into a variable and use it later in the rest of the codes, knowing that if the user does not choose a specific date, should I see all the information for all the dates? I still can not think of a way to do that. I'm looking for information from a MYSQL database.

    
asked by anonymous 20.03.2018 / 20:15

1 answer

1

I do not know if I understand your question, but come on. (In the examples I used angularjs and codeigniter together, so it does not include files and codes referring to routing or configuration)

  • Path from your view to the bank and bank to your view

1. HTML

First you will have to get the information via input in the right HTML form?

                            <div class="form-group">
                                <label>Ano de referência teste:</label>

                                <input type="date"  ng-model="ano_teste.value"
                                       placeholder="yyyy-MM-dd" class="form-control width-auto" ng-required="false"/>
                            </div>

                                <button type="submit" ng-click="enviarData(ano_teste.value);" class="btn btn-primary col-md-offset-5 col-xs-offset-5">Gerar Relatório</button>

2. JS

Subsequently via javascript you will have to send this information that will be in json to your controller ( obs: assuming you are using MVC ), assuming you will handle the format only in the controller class , that would be the correct one, its date would be until then in that format: "2010-01-01T02: 00: 00.000Z"

  $scope.enviarData =  function enviarData(ano_teste){

        //busca dados
        var rows = [];   
        var data = { ano_teste: ano_teste };
        $http.post('get_relatorio_ano_teste', data).then(function(resposta){
            if (resposta.data.status === "error"){
                toastr["error"]('', resposta.data.message);
            }

            for (var i in resposta.data){
                rows.push({
                    c: [resposta.data[i]]
                });
            }

3. Controller (PHP / JAVA)

In controller you will treat the format of the date, putting it in the default date of MYSQL, as the example below:

EXAMPLE:

public function get_relatorio_ano_teste(){
            if (!is_logged_in() || !$this->session->has_userdata('admin')){
                $message = array("status"=>"error","message"=>"Faça login novamente");
                echo json_encode ($message);
                return;
            }

            $request_body = file_get_contents('php://input');
            $data = json_decode($request_body);
            //valida os dados enviados
            if (!array_key_exists('ano_teste', $data) ||
                strlen(trim($data->ano_teste)) == 0 ||
                $data->ano_teste == null ){

                $data_teste = date('Y-m-d', strtotime($data->ano_teste));

                $this->load->model('secretaria/relatorio/RelatorioM');
                $row = $this->RelatorioM->gerar_relatorio_generico($data_teste);
            }else{

            $data_teste = date('Y-m-d', strtotime($data->ano_teste));

            $this->load->model('secretaria/relatorio/RelatorioM');
            $row = $this->RelatorioM->gerar_relatorio_com_data($data_teste);
            }
        echo json_encode ($row);
        return;
        }

And then send up the model so that communication with the bank can proceed there. Here is also where you will see if the user used the field to filter some date or left empty, if it is null you call a model function that selects and returns all instances, otherwise you call function where the query has a "... WHERE data =? ..." and returns the instances with the date constraint.

4. Model (PHP / JAVA)

In model you will do the communication part with the bank, it will be where the query will be and it will be the part that would receive the answer from the bank and through a return you would return something according to your need.

  public function gerar_relatorio_com_data($data_teste){

        $sql = "
          SELECT *
          FROM exemplo as e 
          WHERE e.data_teste = ?
          ORDER BY e.nome
            ";
        $query = $this->db->query($sql,array($data_teste));
        $rows = $query->result();
        if (empty($rows)){
            return array("status"=>"error","message"=>"Não existem resultados pra esse período");
        }else{
            return $rows;
        }
    }
  • Remembering that using patterns makes life easier for the programmer

Some frameworks, libraries, software architectures were made to facilitate development, if you are having trouble modeling your problem, I advise you to do some research on the well-known tools in the market, and you will find more results when searching, you will see that the problems have become easier to model. I use codeigniter , angularjs , react , npm ... among others that made my life a little easier I learned the web but as I'm trying to learn by applying standards things got a little easier to absorb, and the better that was in a short time. I hope I have helped.

    
20.03.2018 / 21:00