如何转移MySQL数据到样式HTML表?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (97)

我正在尝试将数据从我的数据库解析到一个样式化的HTMLTable,但是我找不到关于这个的任何教程。我确实完成了对用PHP创建的表进行解析的教程。我想使用我在这个文件中包含的表。

这是我尝试使用的PHP文件。我只能从教程中找到一封信。

<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("tegneserier") or die(mysql_error());

// Get all the data from the "årgang" table
$result = mysql_query("SELECT * FROM årgang") 
or die(mysql_error());  

echo "<table border='1'>";
echo "<tr> <th>Navn</th> <th>Årgang</th> <th>NR</th> <th>Navn</th> <th>Navn</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row['name'];
    echo "</td><td>"; 
    echo $row['age'];
    echo "</td><td>"; 
    echo $row['issue'];
    echo "</td><td>"; 
    echo $row['Description'];
    echo "</td><td>";
    echo $row['quality'];
    echo "</td></tr>"; 
} 

echo "</table>";
?>

这是我想使用的样式表:

/* ------------------
   styling for the tables 
   ------------------   */

body
{
    line-height: 1.6em;
}

#hor-zebra
{
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    margin: 60px;
    width: 480px;
    text-align: left;
    border-collapse: collapse;
}
#hor-zebra th
{
    font-size: 14px;
    font-weight: normal;
    padding: 10px 8px;
    color: #039;
}
#hor-zebra td
{
    padding: 8px;
    color: #669;
}
#hor-zebra .odd
{
    background: #e8edff; 

这是HTML文件,我希望数据库中的数据显示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
<table id="hor-zebra" summary="Datapass">
    <thead>
        <tr>
            <th scope="col">name</th> //Name off table in DB
            <th scope="col">age</th> //Name off table in DB
            <th scope="col">issue</th> //Name off table in DB
            <th scope="col">Description</th> //Name off table in DB
            <th scope="col">quality</th> //Name off table in DB
        </tr>
    </thead>
    <tbody>
        <tr class="odd">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr class="odd">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
        <tr class="odd">
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
            <td>5</td>
        </tr>
    </tbody>
</table>

</body>
</html>

添加代码后,我的PHP将返回结果。唯一的问题是,它没有显示我的样式表从我的style.css和我也得到了错误“

注意:未定义变量:I在第25行的C:\ProgramFiles(x86)\EasyPHP-5.3.9\www\Tables\Datamodtagelse.php中

在此下面,它返回我的输出:(这是php页面)。

name                    age issue   Description                           quality
Anders And & Co.    1949    1   Dette er en beskrivelse af en tegneserie. Very Fine.

当我打开html文件时,它根本不显示任何内容。

我将添加我的档案:

Datamodtagery.php

<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("tegneserier") or die(mysql_error());

    // Get all the data from the "årgang" table
    $result = mysql_query("SELECT * FROM årgang") 
    or die(mysql_error());  

    echo '<table id="hor-zebra" summary="Datapass">
<thead>
    <tr>
        <th scope="col">name</th>
        <th scope="col">age</th>
        <th scope="col">issue</th>
        <th scope="col">Description</th>
        <th scope="col">quality</th>
    </tr>
</thead>
<tbody>';


    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        if( $i++ % 2 == 0 ) {
            $class = " class='odd'";
        } else {
            $class = "";
        }
        // Print out the contents of each row into a table
        echo "<tr" . $class . "><td>"; 
        echo $row['name'];
        echo "</td><td>"; 
        echo $row['age'];
        echo "</td><td>"; 
        echo $row['issue'];
        echo "</td><td>"; 
        echo $row['Description'];
        echo "</td><td>";
        echo $row['quality'];
        echo "</td></tr>"; 
    } 

    echo "</tbody></table>";
?>

showcomic.html:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>

Style.css

body
{
    line-height: 1.6em;
}

#hor-zebra
{
    font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
    font-size: 12px;
    margin: 60px;
    width: 480px;
    text-align: left;
    border-collapse: collapse;
}
#hor-zebra th
{
    font-size: 14px;
    font-weight: normal;
    padding: 10px 8px;
    color: #039;
}
#hor-zebra td
{
    padding: 8px;
    color: #669;
}
#hor-zebra .odd
{
    background: #e8edff; 
}

我的数据库名是:tegneserier我在数据库中的表是:id int(11) AUTO_INCREMENT

名称varchar(255) utf8_danish_ci

INT年龄(11岁)

INT问题(11)

描述文本text utf8_danish_ci

在查看代码时,我认为问题在于HTML文件以及从.php文件导入样式表和数据的问题。

.php文件、.css文件和.html文件位于同一个文件夹中。

提问于
用户回答回答于

试试这样的东西:

<?php
    // Make a MySQL Connection
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("tegneserier") or die(mysql_error());

    // Get all the data from the "årgang" table
    $result = mysql_query("SELECT * FROM årgang") 
    or die(mysql_error());  

    echo '<table id="hor-zebra" summary="Datapass">
<thead>
    <tr>
        <th scope="col">name</th> //Name off table in DB
        <th scope="col">age</th> //Name off table in DB
        <th scope="col">issue</th> //Name off table in DB
        <th scope="col">Description</th> //Name off table in DB
        <th scope="col">quality</th> //Name off table in DB
    </tr>
</thead>
<tbody>';


    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $result )) {
        if( $i % 2 == 0 ) {
            $class = " class='odd'";
        } else {
            $class = "";
        }
        // Print out the contents of each row into a table
        echo "<tr" . $class . "><td>"; 
        echo $row['name'];
        echo "</td><td>"; 
        echo $row['age'];
        echo "</td><td>"; 
        echo $row['issue'];
        echo "</td><td>"; 
        echo $row['Description'];
        echo "</td><td>";
        echo $row['quality'];
        echo "</td></tr>"; 
    } 

    echo "</tbody></table>";
?>

在HTML文件中:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DataTable Output</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<?php include("datamodtagelse.php"); ?>
</body>
</html>
用户回答回答于

在php文件中,你需要给你的表提供id“hor-zebra”,这样就可以将样式应用于它,并且TH和TD也在其中。

你还需要添加一个计数器来正确地处理.odthing:

var $i = 0;
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr";
    if( $i++ % 2 == 0 ) echo(" class='odd'");
    echo "><td>"; 
    ...

你还可能希望在TH中设置作用域=“colo”,以便尽可能地与原始表匹配。

最后但并非最不重要的一点:不要忘记回声输出中的头和体元素。

如果你这样做,你应该在最后的html中看到相同的表两次(检查源,ctrl+U)

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励