Create table with PHP and populate from MySQL

Posted by typoknig on Stack Overflow See other posts from Stack Overflow or by typoknig
Published on 2010-06-16T03:31:33Z Indexed on 2010/06/16 3:52 UTC
Read the original article Hit count: 156

Filed under:
|
|
|

Hi all, 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);

}

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql