使用PHP创建表并从MySQL填充 [英] Create table with PHP and populate from MySQL

查看:99
本文介绍了使用PHP创建表并从MySQL填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个表格以显示在网页上,并且该表格是从MySQL数据库中的数据填充的。我试图做一些让我感到困难的事情。



首先,我试图调用存在于HTML中单独文件中的PHP代码通过JavaScript。我认为我的工作权利,但我不是100%确定(因为表格不会显示)。我认为它正常工作,因为表中的一些代码(在PHP文件中)显示在FireBug中。

第二个I我正在尝试使它变得更容易观看。我的PHP代码到目前为止是在下面。该表在任何浏览器中都不显示。

  $ query =SELECT * FROM employees; 
$ result = mysql_query($ query);

$ num = mysql_num_rows($ result);

echo'< table>'; ($ 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>;
回显< td class =。 $ class。 > $ wrap_id< / TD> 中;
回显< td class =。 $ class。 > $ wrap_l_name< / TD> 中;
回显< td class =。 $ class。 > $ wrap_f_name< / TD> 中;
回显< td class =。 $ class。 > $ wrap_ssn< / TD> 中;
回声< / tr>;

}

echo'< / table>';

mysql_close($ link);


code

$ b

编辑



回答几个问题:

@ controlfreak123,我不确定你的意思是include('filename_with_php_in_it') 。至于页面没有被调用来解析,我认为它正在被调用并正在建立联系。我在我的原始问题中指出,我相信这是真的,因为FireBug显示表的代码,并且代码位于单独的PHP文件中,因此必须在HTML文件和PHP文件之间进行通信。这里是我如何从HTML文件中调用PHP文件,如果您想知道:

 < script language = javascripttype =text / javascriptsrc =/ Management / Employee_Management.php?action = Edit_Employee>< / script> 

@Matt S,我没有太多的输出方式,事实上我没有知道我得到了什么,直到我看到FireBug,并看到PHP代码(或其中的一部分)确实被传递给HTML文件。具体问题是如何从我的MySQL数据库获取我想要的数据,并通过PHP将其填充到HTML表格中。我还可以确认 employees 确实有数据,这是我放入测试的两个条目。我可以尝试将代码放入自己的文件中,而不使用JavaScript,正如你所建议的那样,但是这会破坏我的目的,因为我希望我的HTML和PHP文件是分开的,但我可能会试着看看PHP代码是否好并确保JavaScript不会破坏它。



@Aaron,我不确定你在问什么(抱歉)。该代码旨在填充在HTML页面上创建和填充表格。

解决方案

下面是一个完整的例子,寻找:


  1. 使用php从mysql中获取数据

  2. 将数据放入html table

  3. 将交替的彩色行应用于表格



  4. 使用jquery,我发现有点容易,那么你正在尝试做什么。

    另外,请记住$ row [field]区分大小写。所以$ row [id]!= $ row [ID]。



    希望这有助于:

     < HTML> 
    < head>
    < meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
    < script src =http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.jstype =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>名称< / td>
    < td>标题< / 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>;
    回声< / tr>;
    }

    ?>
    < / table>
    < / body>
    < / html>

    以下表格代码仅使用PHP来替换样式,就像您在示例中尝试的那样:

     < table class =striped> 
    < tr class =header>
    < td> Id< / td>
    < td>标题< / td>
    < td>日期< / 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>;
    回声< / tr>;
    $ i =($ i == 0)? 1:0;
    }

    ?>
    < / table>


    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.

    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.

    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);
    
    }
    

    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:

    <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 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, 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:

    1. pull some data from mysql using php
    2. put that data into an html table
    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.

    Also, remember $row[field] is case sensitive. So $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:

        <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>
    

    这篇关于使用PHP创建表并从MySQL填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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