doubi4435
doubi4435
2018-06-08 11:58

如何在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条回答

  • ds12093213 「已注销」 3年前
    <?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();
    
    ?>
    
    点赞 评论 复制链接分享
  • douzhuo8312 douzhuo8312 3年前

    The basic table format is this

    for example:

    <table>
        <tr>
            <td>row 1 item 1</td>
            <td>row 1 item 2</td>
        </tr>
        <tr>
            <td>row 2 item 1</td>
            <td>row 2 item 2</td>
        </tr>
    </table>
    

    so try:

    if ($result->num_rows > 0) {
        echo "<table>";
        while($row = $result->fetch_assoc()) {
            echo "<tr>";
            echo "<td>".$row["name"]."</td>";
            echo "<td>".$row["description"]."</td>";
            echo "<td>".$row["content"]."</td>";
            echo "</tr>";
        }
        echo "</table>";
    } else {
        echo "0 results";
    }
    
    点赞 评论 复制链接分享
  • drll85318 drll85318 3年前
    <?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) {
        echo '<table>';
        echo '<tr><td>Name</td><td>Description</td><td>Content</td></tr>'
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "<tr><td>".$row["name"]."</td><td>".$row["description"]."</td><td>".$row["content"] ."</td></tr>";
        }
        echo '</table>';
    } else {
        echo "0 results";
    }
    
    $conn->close();
    

    ?>

    点赞 评论 复制链接分享
  • doujiu9172 doujiu9172 3年前

    Try this approach:

    [... some HTML header and code ...]
    
    <table>
        <thead>
            <tr><th>Name</th><th>Description</th><th>Content</th></tr>
        </thead>
        <tbody>
        <?php
            [ ... extract something from the database ...]
    
            while($row = $result->fetch_assoc()) 
            {
                echo "<tr><td>$row[name]</td><td>$row[description]</td><td>Content</td></tr>
    ";
            }
        }
        ?>
        </tbody>
    </table>
    
    [... some HTML footer ...]
    

    Obviously, replace the [...] sections with your own code. The idea is here to use PHP to output the dynamic part of your HTML code, and is the power of PHP combined with HTML.

    I try to avoid "echoing" to much HTML in my PHP, but it does work (reference other answers).

    Detail: when setting up a <table> you should setup a header section <thead> <tfoot> (if applicable) and body section <tbody>. Ref What is the purpose for HTML's tbody?

    点赞 评论 复制链接分享
  • douwei1174 douwei1174 3年前

    Try this:

    <?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
            echo "<table>";
            echo "<thead>
              <tr>
                 <th>Name</th>
                 <th>Description</th>
              </tr>
             </thead>";
            echo "<tbody>";
            while($row = $result->fetch_assoc()) {
                echo "<tr>";
                echo "<td>".$row["name"]."</td>";
                echo "<td>".$row["description"]."</td>";
                echo "</tr>";            
            }
            echo "</table>";
        } else {
            echo "0 results";
        }
    
        $conn->close();
    ?>
    
    点赞 评论 复制链接分享