Mysql数据到样式化的HTML表 [英] Mysql data to a styled HTML Table

查看:96
本文介绍了Mysql数据到样式化的HTML表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助。我是PHP新手,最近几天试图解决这个问题。我试图将数据从我的数据库解析为样式化的HTML表格,我无法找到任何关于此的教程。我做了解析用PHP创建的表的教程。我想使用我在此文件中包含的表格。如果有人愿意告诉我如何做到这一点并解释它我将非常高兴。

I need some Help. I'm new to PHP and the last couple of days trying to solve this issue. I am trying to parse data from my database to a styled HTML Table and I am not able to find any tutorials on this. I did do the tutorial for parsing to a table that is created with PHP. I would like to use the tables I did include in this file. If anyone would be so kind to show me how to do this and also explain it I would be very happy.

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

This is the PHP file I did try to work with. Only one close I could find from tutorials.

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

这是我想要使用的样式表:

This is the stylesheet I would like to use:

/* ------------------
   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文件:

And this is the HTML file where I would like the data from my database to show:

<!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中的样式表,我也得到错误

After adding the code my PHP do return the result. The only problem is it it not showing my styled tables from my style.css and I also get the error "


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

Notice: Undefined variable: i in C:\Program Files (x86)\EasyPHP-5.3.9\www\Tables\Datamodtagelse.php on line 25

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

And under that it returns my output: (This is the php page.)

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

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

When I open my html file it doesn't display anything at all.

我将添加我的文件:

Datamodtagelse.php

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:

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

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

name varchar(255)utf8_danish_ci

age int(11)

issue int(11)

描述文本utf8_danish_ci

My database name is: tegneserier My table in the database is: årgang My attributes in the table is: id int(11) AUTO_INCREMENT
name varchar(255) utf8_danish_ci
age int(11)
issue int(11)
Description text utf8_danish_ci

查看代码时我认为问题是HTML文件还是导入样式表和.php文件中的数据?

When looking at the code I think the problem is the HTML file nor importing the stylesheet and the data from the .php file?

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

The .php file and the .css file and the .html file is located in the same folder.

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

Any help is welcome. And sorry this is probably just a easy beginner mistake. (We all need to start somewhere.)

推荐答案

请记住,您也可以在div标签中回显您的数据,使用

Remember you can echo your data in div tags too, use

    <li></li> 

允许显示一行一行的sql数据。我提到这个是因为我发现div元素比表格更容易使用。

to allow row after row of sql data to get displayed. I mention this because I find the div elements easier to work with than tables.

这篇关于Mysql数据到样式化的HTML表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆