简体   繁体   English

使用PHP创建表并从MySQL填充

[英]Create table with PHP and populate from MySQL

I am creating a table to display on a web page and that table is populated from data in a MySQL database. 我正在创建一个要在网页上显示的表,该表是从MySQL数据库中的数据填充的。 I am trying to do a couple of things that are making it difficult for me. 我正在尝试做一些让我很难的事情。

First I am trying to have call the PHP code that exists in a separate file in HTML via JavaScript. 首先,我试图通过JavaScript调用HTML中的单独文件中存在的PHP代码。 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文件中)显示在FireBug中。

Second I am trying to make it so the rows alternate colors for easy viewing too. 其次,我试图使它成为行替代颜色,以便于查看。 My PHP code so far is below. 到目前为止我的PHP代码如下。 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);

}

EDIT 编辑

To answer a few questions: 回答几个问题:

@controlfreak123, I am not sure what you mean by "include ('filename_with_php_in_it')". @controlfreak123,我不确定你的意思是“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. 我在原始问题中指出,我认为这是正确的,因为FireBug显示了表的代码,并且该代码位于单独的PHP文件中,因此必须在HTML文件和PHP文件之间进行通信。 Here is how I am calling the PHP file from the HTML file you if you care to know: 以下是我如何通过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. @Matt S,我没有太多的输出方式,事实上我不知道我得到任何东西,直到我看到FireBug并看到PHP代码(或其中一些)确实被传递给HTML文件。 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. 具体问题是如何从MySQL数据库中获取我想要的数据并通过PHP将其填充到HTML表中。 I can also confirm that employees does have data in it, two entries I put in for testing. 我还可以确认employees确实有数据,我输入了两个用于测试的条目。 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. 我可以尝试将代码放入自己的文件而不使用你建议的JavaScript,但这会打败我的目的,因为我希望我的HTML和PHP文件是分开的,但我可能只是为了看看PHP代码是否良好并确保JavaScript没有破坏它。

@Aaron, I am not sure what you are asking (sorry). @Aaron,我不确定你在问什么(对不起)。 The code is meant to populate create and populate a table on an HTML page. 该代码用于填充在HTML页面上创建和填充表格。

Here is a full example of what you're looking for: 以下是您正在寻找的完整示例:

  1. pull some data from mysql using php 使用php从mysql中提取一些数据
  2. put that data into an html table 将该数据放入html表中
  3. apply alternating colored rows to the table 将交替的彩色行应用于表格

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. 另外,请记住$ row [field]区分大小写。 So $row[id] != $row[ID]. 所以$ 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>

The reason your code is not executing is that you cannot include PHP with the Script tag. 您的代码未执行的原因是您不能将PHP包含在Script标记中。 You must use PHP's include function, and the original page must be parsed as PHP. 您必须使用PHP的include函数,并且必须将原始页面解析为PHP。

<?php
include('./my_other_file.php');
?>

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. 第一次在资源处理程序上使用“mysql_fetch_array”函数,之后它无法正常工作。 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. 这个答案可能看起来有点模糊,但我已经多次看过了,所以我总是使用“while”或“do-while”循环从DB中获取多行。

Try using the above code, & see if any information crops up. 尝试使用上面的代码,看看是否有任何信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM