Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 845411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:26:49+00:00 2026-05-15T06:26:49+00:00

I am creating a table to display on a web page and that table

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:26:50+00:00Added an answer on May 15, 2026 at 6:26 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a class which, takes a table from a database, and displays
I'm new to Django and I'm creating an app to create and display employee
(edited) I want to display a dynamic list of JPanels that hold textfield that
I am creating a site much like a wordpress blog whereby the front page
I am using the displaytag library to display a list of permission objects. The
I have to display the Rank of a user for which I plan to
I have a table in ms-sql obm_FeeTable FeeId int, FranchieId int, Amount Money, ChequeNo
I am creating an iPhone application which makes a iphone act as a pendrive
This is a question I had about working with the Kohana framework, though I
I'm new to ASP.NET, C# and the MVC framework but have managed to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.