Filter function php Pdo

Asked

Viewed 553 times

1

I’m with a php function that returns me in an html table all boards Nrplaca however I would need to put a filter in the php function to show only the boards in the table Dstpveiculo are bitruck, I would like some tips on how to do.Follows image of the database:Tabela banco de dados GTCLogist

Php function:

class ProgDAO{

  private $conn;

  public function __construct($connection) {
      $this->conn = $connection;
  }

  public function ListaTodos(){
          $results = array();
          $stmt = $this->conn->prepare(
              'SELECT * FROM GTCLogist'
          );
          $stmt->execute();
          if($stmt) {
              while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                  $prog = new Prog();
                  $prog->setplaca($row->NrPlaca);
                  $prog->setmot(stripslashes($row->DsMotorista));
                  $results[] = $prog;
              }
          }
      return $results;
  }

}

Table showing the data:

    <table  border="1" width="30%">
            <tr>
                <th>BITRUCK</th>
                <th>Motorista</th>
                <th>Data Saída</th>
                <th>Origem</th>
                <th>Destino</th>
                <th>Prev. Cheg. Dest</th>
                <th>Carga/Manifesto</th>
                <th>Adiantamento Fincanceiro</th>
                <th>Agendas</th>
                <th>Malote</th>
                <th>Obs</th>
            </tr>

            <?php
                    foreach ($controller->listarEmail() as $objProg) {
                        ?>
                            <tr>
                                <td><?php echo $objProg->getplaca(); ?></td>
                                <td><?php echo $objProg->getmot(); ?></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>
                <?php
                    }
                ?>

        </table>
  • Can it be in Sql no? It has to be done in php itself?

  • Yes, I would like it to be in php but if you have another option accepted too.

  • 1

    Do you really need to bring the entire database table? Otherwise, just change your SQL to SELECT * FROM GTCLogist where DsTpVeiculo ='Bitruck'.

  • You can create a new method, which makes a WHERE in the desired field or use the method with a parameter that informs the value to be searched for.

  • Or if you want to do it in php itself, you can put a if($row->DsTpVeiculo == 'Bitruck') within the while, but this is waste of the query to the bank, since you can bring filtered directly from it.

  • I tried to change select because it was wrong: Parse error: syntax error, Unexpected 'Bitruck' (T_STRING) in DAO progDAO.php on line 14

  • You missed the quotes? 'SELECT * FROM GTCLogist where DsTpVeiculo = \'Bitruck\''

  • Justo haha, I didn’t know this from Contra Barra. It helped me a lot.

Show 3 more comments

1 answer

2


You can create a new method to list the boards by type, the difference is that this method takes a parameter, which will be used in the clasp WHERE

public function ListaPorTipo($tipo){
    $results = array();
    $stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DsTpVeiculo = ?');
    $stmt->execute(array($tipo));
    if($stmt) {
        while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        $prog = new Prog();
        $prog->setplaca($row->NrPlaca);
        $prog->setmot(stripslashes($row->DsMotorista));
        $results[] = $prog;
    }else{
        print_r($stmt->errorInfo());
    }
    return $results;
}

And the call goes something like:

<?php
$prog = new ProgDAO ($conexao);
$itens = $prog->listaPorTipo('bitruck');
  • For the sake of organization and reusability, this form is really the most appropriate one. + 1

  • Thank you very much, you were a great help. That’s right.

  • Just a doubt, I am unable to call the function I am using MVC, I need to create a function within the class in the model to call ?

  • @Kevin. F, the function should be created in the class ProgDAO

  • @rray yes right, but this call should be put where ?

  • @Kevin. F puts in the controller that does this search, then it calls the DAO takes the values and passes to view(html).

  • @rray in the controller I am using this: class Comando {&#xA; &#xA; private $conn;&#xA; &#xA; public function __construct($connec) {&#xA; $this->conn = $connec;&#xA; }&#xA;&#xA; public function listarTodos(){&#xA; $dao = new ProgDAO($this->conn);&#xA; return $dao -> ListaTodos();&#xA; }&#xA;}&#xA;&#xA;&#xA;&#xA; a way Construct, in case would have to have a Function inside the class command listPorType nay ?

  • @Kevin. Oh, that’s right,

  • Now it was rs, VLW.

Show 4 more comments

Browser other questions tagged

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