用 PHP 创建表并从 MySQL 填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3050558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create table with PHP and populate from MySQL
提问by ubiquibacon
I am creating a table to display on a web page and that table is populated from data in a MySQL database. I am trying to do a couple of things that are making it difficult for me.
我正在创建一个表以显示在网页上,该表是从 MySQL 数据库中的数据填充的。我正在尝试做一些让我感到困难的事情。
First I am trying to have call the PHP code that exists in a separate file in HTML via JavaScript. I think I have that working right but I am not 100% sure (because the table will not display). I think it is working right because someof the code for the table (which is in the PHP file) displays in FireBug.
首先,我试图通过 JavaScript 调用 HTML 中单独文件中存在的 PHP 代码。我想我可以正常工作,但我不是 100% 确定(因为表格不会显示)。我认为它工作正常,因为表格的一些代码(在 PHP 文件中)显示在 FireBug 中。
Second I am trying to make it so the rows alternate colors for easy viewing too. My PHP code so far is below. The table does not display at all in any browser.
其次,我试图使行交替颜色以便于查看。到目前为止,我的 PHP 代码如下。该表根本不会在任何浏览器中显示。
$query = "SELECT * FROM employees";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo '<table>';
for ($i = 0; $i < $num; $i++){
$row = mysql_fetch_array($result);
$id = $row['id'];
$l_name = $row['l_name'];
$f_name = $row['f_name'];
$ssn = $row['ssn'];
$class = (($i % 2) == 0) ? "table_odd_row" : "table_even_row";
echo "<tr>";
echo "<td class=" . $class . ">$wrap_id</td>";
echo "<td class=" . $class . ">$wrap_l_name</td>";
echo "<td class=" . $class . ">$wrap_f_name</td>";
echo "<td class=" . $class . ">$wrap_ssn</td>";
echo "</tr>";
}
echo '</table>';
mysql_close($link);
}
EDIT
编辑
To answer a few questions:
回答几个问题:
@controlfreak123, I am not sure what you mean by "include ('filename_with_php_in_it')". As far as the page not being called to be parsed, I think it is being called and contact is being made. I pointed out in my original question that I believe this is true because FireBug shows the code for the table, and that code is in separate PHP file, thus communication between the HTML file and the PHP file must be taking place. Here is how I am calling the PHP file from the HTML file you if you care to know:
@ controlfreak123,我不确定您所说的“包含('filename_with_php_in_it')”是什么意思。至于未被调用的页面被解析,我认为它正在被调用并且正在联系。我在最初的问题中指出,我相信这是真的,因为 FireBug 显示了表格的代码,并且该代码位于单独的 PHP 文件中,因此必须在 HTML 文件和 PHP 文件之间进行通信。如果您想知道,以下是我从 HTML 文件调用 PHP 文件的方法:
<script language="javascript" type="text/javascript" src="/Management/Employee_Management.php?action=Edit_Employee"></script>
@Matt S, I am not getting much in the way of output, in fact I didn't know I was getting anything at all until I looked at FireBug and saw that the PHP code (or some of it) was indeed being passed to the HTML file. The specific question is how do I get the data I want from my MySQL database and populate it into an HTML table via PHP. I can also confirm that employeesdoes have data in it, two entries I put in for testing. I can try to put the code into its own file without the JavaScript as you suggested, but that would defeat my purpose since I want my HTML and PHP files to be separate, but I may try it just to see if the PHP code is good and to make sure the JavaScript isn't breaking it.
@Matt S,我在输出方面没有得到太多,事实上我根本不知道我得到了什么,直到我查看 FireBug 并看到 PHP 代码(或其中一些)确实被传递给HTML 文件。具体问题是如何从我的 MySQL 数据库中获取我想要的数据并通过 PHP 将其填充到 HTML 表中。我还可以确认其中employees确实有数据,我输入了两个条目进行测试。我可以尝试按照您的建议将代码放入它自己的文件中而不使用 JavaScript,但这会违背我的目的,因为我希望我的 HTML 和 PHP 文件是分开的,但我可以尝试一下,看看 PHP 代码是否好并确保 JavaScript 不会破坏它。
@Aaron, I am not sure what you are asking (sorry). The code is meant to populate create and populate a table on an HTML page.
@Aaron,我不确定你在问什么(抱歉)。该代码旨在填充创建并填充 HTML 页面上的表格。
回答by brendan
Here is a full example of what you're looking for:
这是您要查找的内容的完整示例:
- pull some data from mysql using php
- put that data into an html table
- apply alternating colored rows to the table
- 使用php从mysql中提取一些数据
- 将该数据放入 html 表中
- 将交替的彩色行应用于表格
For the styling I cheat a little and use jquery which I find a bit easier then what you're trying to do.
对于样式,我有点作弊并使用 jquery,我发现它比您尝试做的要容易一些。
Also, remember $row[field] is case sensitive. So $row[id] != $row[ID].
另外,请记住 $row[field] 区分大小写。所以 $row[id] != $row[ID]。
Hope this helps:
希望这可以帮助:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<style type="text/css">
tr.header
{
font-weight:bold;
}
tr.alt
{
background-color: #777777;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('.striped tr:even').addClass('alt');
});
</script>
<title></title>
</head>
<body>
<?php
$server = mysql_connect("localhost","root", "");
$db = mysql_select_db("MyDatabase",$server);
$query = mysql_query("select * from employees");
?>
<table class="striped">
<tr class="header">
<td>Id</td>
<td>Name</td>
<td>Title</td>
</tr>
<?php
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>".$row[ID]."</td>";
echo "<td>".$row[Name]."</td>";
echo "<td>".$row[Title]."</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>
Here's the table code only using PHP to alternate the styles like you're trying to do in your example:
这是仅使用 PHP 来替换样式的表格代码,就像您在示例中尝试做的那样:
<table class="striped">
<tr class="header">
<td>Id</td>
<td>Title</td>
<td>Date</td>
</tr>
<?php
$i = 0;
while ($row = mysql_fetch_array($query)) {
$class = ($i == 0) ? "" : "alt";
echo "<tr class=\"".$class."\">";
echo "<td>".$row[ID]."</td>";
echo "<td>".$row[Name]."</td>";
echo "<td>".$row[Title]."</td>";
echo "</tr>";
$i = ($i==0) ? 1:0;
}
?>
</table>
回答by Fosco
The reason your code is not executing is that you cannot include PHP with the Script tag. You must use PHP's include function, and the original page must be parsed as PHP.
您的代码未执行的原因是您不能在 Script 标签中包含 PHP。必须使用PHP 的include 函数,并且必须将原始页面解析为PHP。
<?php
include('./my_other_file.php');
?>
回答by Knowledge Craving
The starting of the coding is a little bit wrong. It should be:-
编码的开头有点错误。它应该是:-
<?php
$query = "SELECT * FROM employees";
$result = mysql_query($query);
$num = mysql_num_rows($result);
echo '<table>';
if($num) {
while( $row = mysql_fetch_array($result) ) {
// all logic for each of the rows comes here
}
}
else {
// no rows fetched, so display proper message in a row
}
echo "</table>";
?>
The first time "mysql_fetch_array" function is used on a Resource Handler, after that it does not work properly. This answer may seem a bit vague, but I have seen it many times, so I always use a "while" or "do-while" loop for fetching multiple rows from DB.
第一次在资源处理程序上使用“mysql_fetch_array”函数,之后它无法正常工作。这个答案可能看起来有点模糊,但我已经看过很多次了,所以我总是使用“while”或“do-while”循环来从数据库中获取多行。
Try using the above code, & see if any information crops up.
尝试使用上面的代码,看看是否有任何信息出现。

