1

I'm trying to make an image preview in a table, just for checking the submitted files. The table can't have more than 3 cols, and the number of rows and cels is variable, because I skip the "not found" images in DB. I did the code below, but couldn't solve the logic by myself. The table shows the same image for row, and jumps 2 results for the next one.

    <?php
        $dados      = mysql_fetch_array (mysql_query("SELECT id,placa,usuario FROM dados WHERE id='".$_SESSION['novaOS']."'"));
        $itens      = mysql_result (mysql_query("SELECT itens_acessorios FROM vist_aval WHERE idp='".$_SESSION['novaOS']."'"),0);

        $sqlFotos   = "SELECT * FROM imagens WHERE idp='".$_SESSION['novaOS']."'";
        $qrFotos    = mysql_query($sqlFotos) or die();
        $rowFotos   = mysql_fetch_array($qrFotos) or die();
    ?>

    (...)

    <div>
        <table>
            <?php
                $dir_img = "../uploads/fotos/";
                for($f=2;$f<=33;){
                    $foto = $rowFotos[$f];
                    $td = '<td><img src="'.$dir_img.''.$foto.'" style="margin:0;width:100%;height:auto"></td>';
                    $tr = '<tr id="gridpreview"></tr>';
                    if ($foto != false){
                        for ($r=0;$r<=3;$r++){
                            if ($r>0){
                                echo $td;
                                $f++;
                            }
                            else{
                                echo $tr;
                            }
                        }
                    }
                }
            ?>
        </table>
    </div>

Generated Table

In addiction, the message "Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\app\Finalizar.php on line 88" appears on loading. The line 88 refers to if ($foto != false).

2
  • WARNING: If you're just learning PHP, please, do not use the mysql_query interface. It’s so awful and dangerous that it was removed in PHP 7. A replacement like PDO is not hard to learn and a guide like PHP The Right Way explains best practices. Your user parameters are not properly escaped and there are SQL injection bugs that can be exploited. Commented Sep 23, 2016 at 18:43
  • @tadman Yes, I'm dealing with PHP not long ago. I'm just using it this way because of the rush for something functional. After that, I'll have more time for reading and applicate best practices. So I'll probably change a lot of things. Thank you very much for your warning. Commented Sep 23, 2016 at 18:52

2 Answers 2

1

That's not how you do a columnar output. It should be more like

$col = 0;
echo '<tr>';
while($row = ... fetch row from result ...) {
    echo "<td>$row[whatever]</td>";
    $col++;
    if ($col > 2) {
       echo '</tr><tr>';
       $col = 0;
    }
}
echo '</tr>';

You don't need two loops, just the one loop, which keeps repeating until there's more no results left in the DB.

Sign up to request clarification or add additional context in comments.

2 Comments

In this case, isn't it creating <tr>'s one inside another?
I made same changes in this code and it works. Thanks for your help!
0

Just to register the functional code:

<table>
<?php
$dir_img = "../uploads/fotos/";
$col=0;
$f=2;
echo "<tr>";
while ($f<=33){
$foto = $rowFotos[$f];
if ($foto != false){
if ($col>2){
echo "</tr><tr>";
$col = 0;
}
else{
echo "<td><img src='".$dir_img."".$foto."' style='margin:0;width:100%;height:auto'></td>";
$col++;
$f++;
}
}
else{
    $f++;
}
}
echo '<td>';
?>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.