doubi4435 2018-06-08 11:58
浏览 43
已采纳

如何在PHP中创建表并对其进行样式设置

I have managed to take information from my database of the website, however I have no idea how to present the results in a table and how to style it. I tried putting <table> outside of the whole PHP, clearly did not work. I tried echoing a <table> tag before the result echo and a closing </table> tag after it, but that did not do it. This is the code I am working with:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "onlib";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    //Takes all the results from the table with genre 5.
    $sql = "SELECT name, description, content FROM books WHERE genre='5'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<span style='color:white;'>"."<br> Name: ".$row["name"]."<br> Description: ".$row["description"]."<br> Content: ".$row["content"] ."<br>"."</p>";
        }
    } else {
        echo "0 results";
    }

    $conn->close();
?>

I am still new in PHP, trying to understand how the whole thing works. Thanks in advance!

  • 写回答

5条回答 默认 最新

  • 「已注销」 2018-06-08 12:24
    关注
    <?php
    
    function tableV1 ($row) {
        echo '<tr>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['description'] . '</td>';
        echo '<td>' . $row['content'] . '</td>';
        echo '</tr>';
    }
    
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "onlib";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } 
    ?>
    

    Always do Database Connection first, before Outputting anything, that way, you can create custom error message to show instead of a failed database conntection or no content at all.

    <style type="text/css">
    table {}
    tbody {}
    td {}
    th {}
    thead {}
    tr {}
    </style>
    

    Style is used inside the <head></head> to style the table, CSS it's called.

    <table>
        <thead>
            <th>Name</th>
            <th>Description</th>
            <th>Content</th>
        </thead>
        <tbody>
    <?php
    // Takes all the results from the table with genre 5.
    $sql = "SELECT name, description, content FROM books WHERE genre='5'";
    
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
    
        // Output data of each row
        while($row = $result->fetch_assoc()) {
            tableV1($row);
        }
    
    } else {
        echo '<tr><td colspan="3">0 results</td></tr>';
    }
    ?>
        </tbody>
    </table>
    

    Output contents from database.

    <?php
    
    $conn->close();
    
    ?>
    

    Close database connection in the end. All together:

    <?php
    
    function tableV1 ($row) {
        echo '<tr>';
        echo '<td>' . $row['name'] . '</td>';
        echo '<td>' . $row['description'] . '</td>';
        echo '<td>' . $row['content'] . '</td>';
        echo '</tr>';
    }
    
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "onlib";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } 
    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8" />
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <title>Page Title</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <style type="text/css">
            table {}
            tbody {}
            td {}
            th {}
            thead {}
            tr {}
            </style>
        </head>
        <body>
            <table>
                <thead>
                    <th>Name</th>
                    <th>Description</th>
                    <th>Content</th>
                </thead>
                <tbody>
    <?php
    // Takes all the results from the table with genre 5.
    $sql = "SELECT name, description, content FROM books WHERE genre='5'";
    
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
    
        // Output data of each row
        while($row = $result->fetch_assoc()) {
            tableV1($row);
        }
    
    } else {
        echo '<tr><td colspan="3">0 results</td></tr>';
    }
    ?>
                </tbody>
            </table>
        </body>
    </html>
    <?php
    
    $conn->close();
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题