首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将Mysql数据转换为带样式的HTML表

将Mysql数据转换为带样式的HTML表
EN

Stack Overflow用户
提问于 2012-02-22 06:22:05
回答 3查看 9.1K关注 0票数 2

我需要些帮助。我是PHP新手,最近几天一直在尝试解决这个问题。我试图从我的数据库解析数据到一个有样式的HTML表,但我找不到任何关于这方面的教程。我确实完成了解析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位于C:\Program Files (x86)\EasyPHP-5.3.9\www\Tables\Datamodtagelse.php的第25行

然后,它返回我的输出:(这是php页面)。

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

当我打开我的html文件时,它根本没有显示任何东西。

我将添加我的文件:

Datamodtagelse.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我在数据库中的表是:§rgang我在表中的属性是: id int(11) AUTO_INCREMENT

名称varchar(255) utf8_danish_ci

年龄int(11)

问题int(11)

描述文本utf8_danish_ci

在查看代码时,我认为问题出在HTML文件,也没有从.php文件导入样式表和数据?

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

欢迎任何帮助。抱歉,这可能只是一个简单的初学者错误。(我们都需要从某个地方开始。)

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9386221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档