Create table with PHP and populate from MySQL

后端 未结 3 580
陌清茗
陌清茗 2020-12-01 09:44

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

相关标签:
3条回答
  • 2020-12-01 10:33

    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>
    
    0 讨论(0)
  • 2020-12-01 10:36

    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.

    <?php
    include('./my_other_file.php');
    ?>
    
    0 讨论(0)
  • 2020-12-01 10:37

    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.

    Try using the above code, & see if any information crops up.

    0 讨论(0)
提交回复
热议问题