Turn DATA input into a PHP/JAVA variable

Asked

Viewed 137 times

0

I hope you understand the situation: Programming a web page, I created a field data where the user chooses the date that should filter the information contained on the website, that is, show only the information remanded to 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 appear all the information referring to all the dates? I still can’t think of a way to do this. I search for information from a MYSQL database.

  • Could post the code you implemented?

1 answer

1

I’m not sure 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 the bank to your view

1. HTML

First you will have to take 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

Later via javascript you will have to send this information that will be in json to your controller (Obs: assuming you are using MVC), Assuming that you will only treat the format in the controller class, which would be correct, your date would be up to then in this 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 date format by putting in the MYSQL date pattern, 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 model so that there is continued communication with bank. This is also where you will see if the user used the field to filter some date or left it empty, if it is null you call a model function that selects and returns all instances, otherwise you call the function where the query has a "... WHERE data = ? ..." and returns the instances with the date restriction.

4. Model (PHP/JAVA)

In model you will do the communication part with the bank, is where the query will be and is the part that would receive the response from the bank and through a Return you would return something as 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 the use of patterns facilitate the life of the programmer

Some frameworks, libraries, software architectures were made to facilitate the development, if you are having difficulty to model your problem I advise you to research a little about the tools well diffused in the market, In addition to finding more results when researching, you will see that the problems have become easier to model. I use codeigniter, Angularjs, React, npm... among others that made my life easier, I learned web a little while ago, but as I’m trying to learn by applying patterns things have become a little easier to absorb, and the best that was in a short time. I hope I’ve helped.

  • Gabriel, I could demonstrate a generic example of all this?

  • So I can, but you will use some specific framework?

  • I’ll make an example using angular with codeigniter

Browser other questions tagged

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