用 PHP 创建表并从 MySQL 填充
我正在创建一个表格以显示在网页上,该表格是从 MySQL 数据库中的数据填充的.我正在尝试做一些让我感到困难的事情.
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.
首先,我试图通过 JavaScript 调用 HTML 中单独文件中存在的 PHP 代码.我想我可以正常工作,但我不是 100% 确定(因为表格不会显示).我认为它运行正常,因为某些表格代码(在 PHP 文件中)显示在 FireBug 中.
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 some of the code for the table (which is in the PHP file) displays in FireBug.
其次,我试图使行交替颜色以便于查看.到目前为止,我的 PHP 代码如下.该表格根本不会在任何浏览器中显示.
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.
$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);
}
编辑
回答几个问题:
@controlfreak123,我不确定您所说的include ('filename_with_php_in_it')"是什么意思.至于未被调用的页面被解析,我认为它正在被调用并且正在联系.我在最初的问题中指出,我相信这是真的,因为 FireBug 显示了表格的代码,并且该代码位于单独的 PHP 文件中,因此必须在 HTML 文件和 PHP 文件之间进行通信.如果您想知道,以下是我从 HTML 文件调用 PHP 文件的方法:
@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:
<script language="javascript" type="text/javascript" src="/Management/Employee_Management.php?action=Edit_Employee"></script>
@Matt S,我对输出的影响不大,事实上我根本不知道我得到了什么,直到我查看 FireBug 并看到 PHP 代码(或其中一些)确实是被传递到 HTML 文件.具体问题是如何从我的 MySQL 数据库中获取我想要的数据并通过 PHP 将其填充到 HTML 表中.我还可以确认 employees
中确实有数据,我输入了两个条目进行测试.我可以尝试按照您的建议将代码放入它自己的文件中而不使用 JavaScript,但这会违背我的目的,因为我希望我的 HTML 和 PHP 文件是分开的,但我可以尝试一下,看看 PHP 代码是否好并确保 JavaScript 不会破坏它.
@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 employees
does 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.
@Aaron,我不确定你在问什么(抱歉).该代码用于在 HTML 页面上填充创建和填充表格.
@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.
推荐答案
以下是您正在寻找的完整示例:
Here is a full example of what you're looking for:
- 使用php从mysql中提取一些数据
- 将该数据放入一个 html 表格中
- 将交替的彩色行应用于表格
对于样式,我有点作弊并使用 jquery,我发现它比您尝试做的要容易一些.
For the styling I cheat a little and use jquery which I find a bit easier then what you're trying to do.
另外,请记住 $row[field] 区分大小写.所以 $row[id] != $row[ID].
希望这有帮助:
<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>
这是仅使用 PHP 来替换样式的表格代码,就像您在示例中尝试做的那样:
Here's the table code only using PHP to alternate the styles like you're trying to do in your example:
<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>
相关文章