MySQL while switching from PDO foreach

0

I'm trying to migrate the files with the MySQL Functions to the PDO. But I can not loop the images with the code below.

<?php
    $sql = $pdo->prepare("SELECT * FROM banner");
    $sql->execute();
    foreach($sql->fetchAll() as $res){
    $tabela = '<tr>
    <td align="center"><img width="100" height="auto" src="../img_banner/'.$res['imagem'].'" title=""/></td>

    <td align="center">'.$res['texto'].'</td>

    <td align="center">'.$res['fonte'].'</td>

    <td align="center">'.$res['tam_fonte'].'</td>

    <td align="center"><div style="background-color:'.$res['cor_texto'].'; width:50%; height:20px;"></div></td>

    <td align="left">

    <a style="text-decoration:none;" href="edit_banner_inicial-upd.php?codigo='.$res['codigo'].'">
        <img width="25" src="../img/edit.png" title="Editar Banner '.$res['codigo'].'"/>
    </a>

    <a style="text-decoration:none;" href="inicial_del_banner.php?codigo='.$res['codigo'].'">
        <img width="25" src="../img/del.png" title="Excluir Banner '.$res['codigo'].'"/>
    </a>
    </td>
    </tr>';
    }   
?>

So it shows me only the last file registered in the Banner table. But when I include a period before the equals sign in the 4th line of the code, as shown below

$tabela .= '<tr>

It brings me all the files registered in the Banner table, however it shows me this message below.

Referring to line 42 which in the case is the 20th of the code published here, as shown below.

</tr>';

If friends can help me solve this problem, I'll be very grateful.

    
asked by anonymous 02.11.2017 / 04:49

2 answers

0

When you do, within the foreach loop, $tabela .= 'alguma coisa' , in the first run the variable table does not yet exist (something like $tabela = $tabela . 'alguma coisa' ). Since the variable $tabela was not declared this error appears on first run. To fix just do:

$tabela = '';
foreach($sql->fetchAll() as $res){
    $tabela .= '<tr>';
    ....
    ....
    
02.11.2017 / 18:17
0

The message displayed is:

  

Notice: Undefined variable: table

Translating

  

Warning: Variable undefined: table

This is because you did not instantiate / set some value for the variable $tabela and tried to concatenate a content to it.

In order to solve this problem, it is enough that before the loop repetition makes an assignment in the variable $tabela and the message will not appear again.

Example:

$tabela = '';
foreach($sql->fetchAll() as $res){
    $tabela .= '<tr> ...

To understand this type of message, look at the example below:

$x = $y + 10;

Running this script, what value should contain the variable $x ?

You must respond, impossible to say, I do not know what the value of the $y variable is. For PHP the answer is the same, for it is doing the following operation internally:

$x = valor indefinido + 10;

The correct way and do the following operation:

$y = 5;
$x = $y + 10;

Or:

$y = 0;
$x = $y + 10;

Every time an operation is performed with a variable with undefined value PHP displays the message

  

Notice: Undefined variable

    
02.11.2017 / 18:17