dscc90150010 2017-02-02 23:00
浏览 54

php在数据库中创建未知数量的列的html表[重复]

This question already has an answer here:

i want to create an html table from a mariadb database. I dont know how many columns each Database table will end up with.

So as i dont know how many columns i have, i came up with this:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="styles/global.css">
</head>
<body>

    <?php

    $con=mysqli_connect("localhost","root","","test");
// Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
//query
    $sql="SELECT * FROM news ORDER BY author";
    $result=mysqli_query($con,$sql);



    $counter = 0;
    while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){

        echo $counter;
        echo nl2br("
");
        ?>  

        <table>
            <thead>
                <?php       
                if($counter==0){ ?>
                    <tr>
                        <th><?php echo implode("</th><th>", array_keys($row)); ?>
                        </th>
                    </tr>
                    }
            </thead>
            <tbody>
                <?php       
                if($counter>=0){ ?>
                    <tr>
                    <td><?php echo implode("</td><td>", $row); ?>
                    </td>
                    </tr>
                }
            </tbody>
        </table>

        <?php
        $counter ++;
    }

mysqli_free_result($result);
mysqli_close($con);
?>  
</body>
</html>

The problem is that the table wont fill with more than the first row.

Additionally i have get the error

Parse error: syntax error, unexpected end of file in E:\xampp\htdocs\test.php on line 58 Blockquote

The Error disappears when i enter two curly braces "}}" after the while loop, it will present the following:

execute:

I dont know why there are the curly braces in the browser as seen in the picture.

EDIT: I edit the thread because it was marked as duplicate to parsing and syntax errors, though my main problem is the creation of the table with unknown number of columns.

Any idea how to solve this? Thank you guys!

</div>
  • 写回答

1条回答 默认 最新

  • doucheng2210 2017-02-03 00:30
    关注

    To be honest, I'm not sure why it was giving that error, as all your braces were closed. However, try simplifying -- as RiggsFolly said, put only what you want repeated inside the loop. It makes your logic much simpler and neater. Try this:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <link rel="stylesheet" type="text/css" href="styles/global.css">
    </head>
    <body>
    
        <?php
    
        $con=mysqli_connect("localhost","root","","test");
    // Check connection
        if (mysqli_connect_errno()){
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
    //query
        $sql="SELECT * FROM news ORDER BY author";
        $result=mysqli_query($con,$sql);
        $row=mysqli_fetch_array($result, MYSQLI_ASSOC)
        ?>
    
        <table>
            <thead>
                    <tr>
                        <th><?php echo implode("</th><th>", array_keys($row)); ?>
                        </th>
                    </tr>
            </thead>
            <tbody>
                    <tr>
                    <td><?php echo implode("</td><td>", $row); ?>
                    </td>
                    </tr>
    
        <?php
        $counter = 0;
        while($row=mysqli_fetch_array($result, MYSQLI_ASSOC)){
    //        echo $counter;
    //        echo nl2br("
    ");
            ?>  
    
                    <tr>
                    <td><?php echo implode("</td><td>", $row); ?>
                    </td>
                    </tr>
    
            <?php
            $counter ++;
        }
    
    mysqli_free_result($result);
    mysqli_close($con);
    ?>  
                </tbody>
            </table>
    </body>
    </html>
    

    And then, check out any of the free template processors available, so you're not mixing content and code.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图