Mysql 数据到样式化的 HTML 表
我需要一些帮助。我是 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) 自动递增
名称 varchar(255) utf8_danish_ci
年龄 int(11)
问题 int(11)
描述文本 utf8_danish_ci
当查看代码时,我认为问题是 HTML 文件也不从 .php 文件导入样式表和数据?
.php 文件、.css 文件和 .html 文件位于同一文件夹中。
欢迎任何帮助。 抱歉,这可能只是一个简单的初学者错误。 (我们都需要从某个地方开始。)
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.
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;
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>
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 "
Notice: Undefined variable: i in C:\Program Files (x86)\EasyPHP-5.3.9\www\Tables\Datamodtagelse.php on line 25
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.
When I open my html file it doesn't display anything at all.
I will add my file :
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;
}
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
When looking at the code I think the problem is the HTML file nor importing the stylesheet and the data from the .php file?
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.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的操作:
在您的 HTML 文件中:
Try something like this:
And in your HTML file:
你就快到了(如果你向我们展示的代码运行的话)。
在你的 php 文件中,你需要为你的表指定 id“hor-zebra”,以便样式将应用于它以及其中的 th 和 td。
你还想添加一个计数器来正确处理 .odd 的事情:
你可能还想在你的 th 中设置scope =“col”以尽可能接近地匹配原始表
,最后但并非最不重要的一点:不要忘记echo 输出中的元素 thead 和 tbody 。
如果你完成了所有这些,你应该在最终的 html 中看到同一个表两次(检查源代码,ctrl+U)
you are almost there (if the code you showed us runs).
in your php file, you need to give your table the id "hor-zebra", so that the style will be applied to it and the th's and td's within it.
you also want to add a counter to get the .odd thing right:
you also might want to set the scope="col" in your th's to match the original table as closely as possible
AND last but not least: Don't forget the elements thead and tbody in your echo-output.
if you do all this, you should see the same table twice in your final html (check the source, ctrl+U)
请记住,您也可以在 div 标签中回显数据,用于
允许显示一行又一行的 sql 数据。我提到这一点是因为我发现 div 元素比表格更容易使用。
Remember you can echo your data in div tags too, use
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.