douyuliu9527 2016-09-23 17:44
浏览 30
已采纳

PHP变量表大小,列数受限制

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条回答 默认 最新

  • dongyong1942 2016-09-23 17:48
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64