"Catchable fatal error" PHP error

Asked

Viewed 101 times

-2

I have a field search in which put a dynamic date, posting this date returns me the records of the bank below, but I stuck in this error that happens in foreach. Follows code:

DAO:

public function ListaPorTipoB($obj){
    $results = array();
    $stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DsTpVeiculo = \'Bitruck\' AND DtBase = ?');
    $stmt->bindValue(1, $obj->getdata());
        if($stmt) {
            while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                $prog = new Prog();
                $prog->setid($row->ID);
                $prog->setst($row->DsStatus);
                $prog->setplaca($row->NrPlaca);
                $prog->setmot(stripslashes($row->DsMotorista));
                $prog->setsaida(date('d/m/Y', strtotime($row->DtSaida)));
                $prog->setorig($row->DsOrigem);
                $prog->setdest($row->DsDestino);
                $prog->setprev(date('d/m/Y', strtotime($row->DtPrevChegDest)));
                $prog->setcarga($row->DsCarga);
                $prog->setadfin($row->DsAdFin);
                $prog->setagen($row->DsAgendas);
                $prog->setmal($row->DsMalote);
                $prog->setobs($row->DsObservacao);
                $results[] = $prog;
            }
        }
    return $results;
}

Controller:

class Comando{

  private $conn;

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

  public function ListaPorTipoB(Prog $obj){
    $dao = new ProgDAO($this->conn);
    return $dao -> ListaPorTipoB($obj);
  }
}

Precontrol:

include_once ('../connection_open.php');

include_once ('../model/prog.php');
include_once ('progControle.php');
include_once ('../DAO/progDAO.php');


$dataPost = $_POST['dataa'];

$objProg = new Prog();
$objProg->setdata($dataPost);

$controller = new Comando($conn);
$controller->ListaPorTipoB($objProg);


header ("location: ../view/programacao.php");

include_once ('../connection_close.php');

Field search and below returns the database records:

<form method="POST" action="../controller/buscaPrecontrole.php">
        <div class="large-12 columns">
            <div class="large-3 columns">
                <input type="date" id="dataa" name="dataa" value="" required>
            </div>
            <div class="large-9 columns">
                <input type="submit" class="tiny round button" value="Filtrar"/>
            </div>
        </div>
        <br><br><br>
        <div class="large-12 columns">
            <div class="TableCSS">
                <table>
                    <tr>
                        <td>ST</td>
                        <td>BITRUCK</td>
                        <td>Motorista</td>
                        <td>Data Saída</td>
                        <td>Origem</td>
                        <td>Destino</td>
                        <td>Previsão chegada</td>
                        <td>Carga/Manifesto</td>
                        <td>Adiantamento Fincanceiro</td>
                        <td>Agendas</td>
                        <td>Malotes</td>
                        <td colspan="2">Observação</td>
                    </tr>
                    <?php foreach ($controller->ListaPorTipoB() as $objProg) { ?>
                        <tr>
                        <td><?php echo $objProg->getplaca(); ?></td>
                        <td><?php echo $objProg->getmot(); ?></td>
                        <td><?php echo $objProg->getsaida(); ?></td>
                        <td><?php echo $objProg->getorig(); ?></td>
                        <td><?php echo $objProg->getdest(); ?></td>
                        <td><?php echo $objProg->getprev(); ?></td>
                        <td><?php echo $objProg->getcarga(); ?></td>
                        <td><?php echo $objProg->getadfin(); ?></td>
                        <td><?php echo $objProg->getagen(); ?></td>
                        <td><?php echo $objProg->getmal(); ?></td>
                        </tr>
                    <?php } ?>

                 </table>
               </div>
         </div>
  </form>

Catchable fatal error: Argument 1 passed to Comando::Listaportipob() must be an instance of Prog, None Given, called in C: xampp htdocs view programacao.php on line 76 and defined in C: xampp htdocs controller progControle.php on line 11

I can’t seem to fix this mistake, some hint ?

  • 2

    It’s the same mistake as last question, you need to pass an object Prog along those lines foreach ($controller->ListaPorTipoB() as $objProg

  • It is mandatory to pass the date to ListaPorTipoB() ?

  • No, if there is another way to do it, with the same purpose and is more feasible. And I could not pass the Prog object in this line above.

  • If the date doesn’t matter, just cut it out 1) of SQL. 2) Remove bind 3) Remove from method ListaPorTipoB($obj){ in DAO. 4) Remove from honeycomb ListaPorTipoB(Prog $obj){ in command.

  • 1

    But it does matter. I need it. How do I pass the object to the line you mentioned above ? Because I tried to pass it and it didn’t work, the same error continues.

  • Before the foreach do $objProg = new Prog();$objProg->setdata('2016-03-24');. afterward: foreach ($controller->ListaPorTipoB($objProg) as $objProg

  • 1

    Only that this date is dynamic as put in the field search on the foreach I can’t set it manually there.

Show 2 more comments

2 answers

1


I solved the error by modifying some codes below:

View:

<form method="POST" action="../controller/buscaPrecontrole.php">
        <div class="large-12 columns">
            <div class="large-3 columns">
                <input type="date" id="data" name="data" value="" required>
            </div>
            <div class="large-9 columns">
                <input type="submit" class="tiny round button" value="Filtrar"/>
            </div>
        </div>
        <div class="large-12 columns">
            <div class="TableCSS">
                <table>
                    <tr>
                        <td>ST</td>
                        <td>BITRUCK</td>
                        <td>Motorista</td>
                        <td>Data Saída</td>
                        <td>Origem</td>
                        <td>Destino</td>
                        <td>Previsão chegada</td>
                        <td>Carga/Manifesto</td>
                        <td>Adiantamento Fincanceiro</td>
                        <td>Agendas</td>
                        <td>Malotes</td>
                        <td colspan="2">Observação</td>
                    </tr>
                    <?php
                    if(isset($_GET['data'])){

                        $dataPost = $_GET['data'];

                        $objProg = new Prog();
                        $objProg->setdata($dataPost);

                    }else{

                        $dataPost = date('Y-m-d');

                        $objProg = new Prog();
                        $objProg->setdata($dataPost);


                    }


                     foreach ($controller->ListaPorTipoB($objProg) as $objProg) { ?>

Precontrol:

$dataPost = $_POST['data'];

$objProg = new Prog();
$objProg->setdata($dataPost);

$controller = new Comando($conn);
$controller->ListaPorTipoB($objProg);


header ("location: ../view/programacao.php?data=".$dataPost."");

0

I think this solves, because the question basically is that you are calling a method without passing value, in the class it is mandatory to pass a value, to solve you can create a second method that does the same, without having to pass an object, for that there are methods like Set and Get, however, for the example you are doing it would have to be something similar to this:

<form method="POST" action="../controller/buscaPrecontrole.php">
        <div class="large-12 columns">
            <div class="large-3 columns">
                <input type="date" id="dataa" name="dataa" value="" required>
            </div>
            <div class="large-9 columns">
                <input type="submit" class="tiny round button" value="Filtrar"/>
            </div>
        </div>
        <br><br><br>
        <div class="large-12 columns">
            <div class="TableCSS">
                <table>
                    <tr>
                        <td>ST</td>
                        <td>BITRUCK</td>
                        <td>Motorista</td>
                        <td>Data Saída</td>
                        <td>Origem</td>
                        <td>Destino</td>
                        <td>Previsão chegada</td>
                        <td>Carga/Manifesto</td>
                        <td>Adiantamento Fincanceiro</td>
                        <td>Agendas</td>
                        <td>Malotes</td>
                        <td colspan="2">Observação</td>
                    </tr>
                    <?php
                         if(!$_POST) {
                           $content = null;
                           } else {
                             $obj = new Prog();
                             $type = $obj->setdata($_POST['dataa']);
                             if ($controller instanceof Comando)):
                                $content = $controller->ListaPorTipoB($type);
                             }
                        if (!empty($content)):
                           foreach ($content as $objProg): ?>
                        <tr>
                        <td><?php echo $objProg->getplaca(); ?></td>
                        <td><?php echo $objProg->getmot(); ?></td>
                        <td><?php echo $objProg->getsaida(); ?></td>
                        <td><?php echo $objProg->getorig(); ?></td>
                        <td><?php echo $objProg->getdest(); ?></td>
                        <td><?php echo $objProg->getprev(); ?></td>
                        <td><?php echo $objProg->getcarga(); ?></td>
                        <td><?php echo $objProg->getadfin(); ?></td>
                        <td><?php echo $objProg->getagen(); ?></td>
                        <td><?php echo $objProg->getmal(); ?></td>
                        </tr>
                    <?php endforeach; ?>
                 <?php else:>
                        <tr>
                          <td colspan="10">Sem registros.</td>
                        </tr>
                 <?php endif; ?>
               <?php else:>
                        <tr>
                          <td colspan="10">Sem registros.</td>
                        </tr>
             <?php endif; ?>
                 </table>
               </div>
         </div>
  </form>

see if instead of doing all that I did above, it wouldn’t be better to do that:

include_once ('../connection_open.php');

include_once ('../model/prog.php');
include_once ('progControle.php');
include_once ('../DAO/progDAO.php');


$dataPost = $_POST['dataa'];

$objProg = new Prog();
$objProg->setdata($dataPost);

$controller = new Comando($conn);
//atribuindo a saída para uma variável
$data = $controller->ListaPorTipoB($objProg);

include_once ('../connection_close.php');

In view:

<form method="POST" action="../controller/buscaPrecontrole.php">
        <div class="large-12 columns">
            <div class="large-3 columns">
                <input type="date" id="dataa" name="dataa" value="" required>
            </div>
            <div class="large-9 columns">
                <input type="submit" class="tiny round button" value="Filtrar"/>
            </div>
        </div>
        <br><br><br>
        <div class="large-12 columns">
            <div class="TableCSS">
                <table>
                    <tr>
                        <td>ST</td>
                        <td>BITRUCK</td>
                        <td>Motorista</td>
                        <td>Data Saída</td>
                        <td>Origem</td>
                        <td>Destino</td>
                        <td>Previsão chegada</td>
                        <td>Carga/Manifesto</td>
                        <td>Adiantamento Fincanceiro</td>
                        <td>Agendas</td>
                        <td>Malotes</td>
                        <td colspan="2">Observação</td>
                    </tr>
                    <?php foreach ($data as $objProg) { ?>
                        <tr>
                        <td><?php echo $objProg->getplaca(); ?></td>
                        <td><?php echo $objProg->getmot(); ?></td>
                        <td><?php echo $objProg->getsaida(); ?></td>
                        <td><?php echo $objProg->getorig(); ?></td>
                        <td><?php echo $objProg->getdest(); ?></td>
                        <td><?php echo $objProg->getprev(); ?></td>
                        <td><?php echo $objProg->getcarga(); ?></td>
                        <td><?php echo $objProg->getadfin(); ?></td>
                        <td><?php echo $objProg->getagen(); ?></td>
                        <td><?php echo $objProg->getmal(); ?></td>
                        </tr>
                    <?php } ?>

                 </table>
               </div>
         </div>
  </form>

Just one remark about your class: Be careful with recursive methods, as this may affect performance if it is misspelled.

  • Returned me two error messages on foreach - Notice: Undefined variable: data in line 78 - Warning: Invalid argument supplied for foreach() in online 78

  • This error indicates that the variable $data is not passing anything to the foreach loop... you’ve already given a var_dump($controller->ListaPorTipoB($objProg)); die(); to see if there is any way out of this? Remembering that you are using recursive method... see if this is correct.

  • Where I run this var_dump($controller->ListaPorTipoB($objProg)); die(); ?

Browser other questions tagged

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