【问题标题】:How to create a table and style it in PHP如何在 PHP 中创建表格并为其设置样式
【发布时间】:2018-06-08 23:24:04
【问题描述】:

我已经设法从我的网站数据库中获取信息,但是我不知道如何在表格中呈现结果以及如何设置它的样式。我尝试将<table> 放在整个 PHP 之外,显然没有用。我尝试在结果回显之前回显一个<table> 标记,并在它之后回显一个关闭</table> 标记,但这并没有做到。这是我正在使用的代码:

<?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();
?>

我还是 PHP 新手,试图了解整个事情是如何工作的。提前致谢!

【问题讨论】:

  • 向我们展示你实际尝试过的东西,而不是仅仅告诉我们关于哪些东西不起作用的模糊故事...... “我还是 PHP 新手” - 这没什么可做的用特定的语言做,或多或少只是逻辑思维的一般问题……我想要创建的结构是什么,以及在哪里做必要的标签为发生这种情况而输出。
  • 您是否从脚本中获得了当前的输出?
  • 我搜索了一些东西,但无法正常工作。现在的代码按原样显示来自数据库的信息。名称:爱丽丝梦游仙境 描述:一本好书 内容:这是全部内容。如果我添加另一本书,它会显示在这本书下面,并在它们之间用一条线显示。
  • if ($result->num_rows > 0) { // 输出每一行的数据 echo ""; while($row = $result->fetch_assoc()) { echo ""."
    名称:".$row["name"]."
    说明: ".$row["description"]."
    内容: ".$row["content"] ."
    "."";回声“
    ”; } } else { echo "0 个结果";如果我尝试这样做,这就是全部,但没有创建表。
  • 基本上 PHP 只是创建纯 HTML。我建议创建一个没有 PHP 的纯 HTML,然后用 PHP 代码替换动态部分(每个 roe 应该重复 gor,通常是 ... 部分)。

标签: php html css


【解决方案1】:
<?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); } 
?>

始终先进行数据库连接,然后再输出任何内容,这样,您可以创建自定义错误消息来显示,而不是显示数据库连接失败或根本没有内容。

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

&lt;head&gt;&lt;/head&gt; 中使用样式来设置表格的样式,它被称为 CSS。

<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>

从数据库中输出内容。

<?php

$conn->close();

?>

最后关闭数据库连接。一起来:

<?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();

?>

【讨论】:

    【解决方案2】:

    试试这个:

    <?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();
    ?>
    

    【讨论】:

    • 谢谢。这确实在表格中设置样式!我忘记包含有关每一行的信息。谢谢。
    【解决方案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();
    

    ?>

    【讨论】:

    • 谢谢。这确实在表格中设置样式!我忘记包含有关每一行的信息。谢谢。
    【解决方案4】:

    基本的表格格式是这样的

    例如:

    <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>
    

    那就试试吧:

    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";
    }
    

    【讨论】:

    • 我在尝试这个时只收到一行信息。
    • @TodorYakov 你确定你是这样做的吗?因为我已经测试过了,它对我来说很好用
    • 对此表示歉意,它确实在表格中完成,我只有背景为黑色,字母为灰色。谢谢!
    【解决方案5】:

    试试这个方法:

    [... 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>\n";
            }
        }
        ?>
        </tbody>
    </table>
    
    [... some HTML footer ...]
    

    显然,用您自己的代码替换 [...] 部分。这里的想法是使用 PHP 来输出 HTML 代码的动态部分,是 PHP 与 HTML 结合的power

    我尽量避免在我的 PHP 中“回响”很多 HTML,但它确实有效(参考其他答案)。

    详细信息:设置&lt;table&gt; 时,您应该设置标题部分&lt;thead&gt; &lt;tfoot&gt;(如果适用)和正文部分&lt;tbody&gt;。参考What is the purpose for HTML's tbody?

    【讨论】:

    • 您在回声中的变量存在非常量假设问题。
    • 如果您要添加说明 HTML 表格需要的不仅仅是 &lt;table&gt;&lt;/table&gt;,包括 &lt;tr&gt;&lt;/tr&gt;&lt;td&gt;&lt;/td&gt;,这将是一个非常好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-25
    • 2010-11-12
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多