Create a table with PHP and populate from MySQL

I create a table to display on a web page, and this table is populated with data from a MySQL database. I'm trying to do a couple of things that make it difficult for me.

First I try to call the PHP code that exists in a separate file in HTML via JavaScript. I think that it works for me correctly, but I'm not 100% sure (because the table will not be displayed). I think it works correctly, because some code of the table (which is in the PHP file) is displayed in FireBug .

Secondly, I'm trying to make the rows alternate colors for easy viewing. So far, my PHP code is below. The table is not displayed 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, , "include ('filename_with_php_in_it')". , , . , , , FireBug , PHP, HTML PHP . PHP HTML , :

<script language="javascript" type="text/javascript" src="/Management/Employee_Management.php?action=Edit_Employee"></script>

@Matt S, , , - , FireBug , PHP ( ) HTML. , MySQL HTML- PHP. , employees , , . JavaScript, , , , HTML PHP , , , PHP- , JavaScript .

@, , (). HTML-.

+5
3

, :

  • mysql, php
  • html

jquery, , , .

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

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

, , , PHP Script. PHP include, PHP.

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

. : -

<?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>";
?>

"mysql_fetch_array" , . , , "while" "do-while" .

, - .

0

All Articles