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 8433719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:27:56+00:00 2026-06-10T06:27:56+00:00

Using this function from a different to generate a table from a PHP query.

  • 0

Using this function from a different to generate a table from a PHP query. I’m trying to generate charts from using the filament jQuery visualize plugin. Nearly there however it seems that I need to tag each value in the first column as <th> in order for the charts to properly recognise it as a header. I think I need to put an if statement in in terms of ‘if the column number is 1, append <th> to it’ but unsure where this would go in the below code. Any ideas?

function SQLResultTable($Query)
{
    $host = "localhost";
$user = "root";
$pass = "";
$db = "Quality_Monitoring";
    $link = mysql_connect($host, $user, $pass) or die('Could not connect: ' . mysql_error());      //build MySQL Link
    mysql_select_db($db) or die('Could not select database');        //select database
    $Table = "";  //initialize table variable

    $Table.= "<table id='Table1' border='1' style=\"border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; \">"; //Open HTML Table

    $Result = mysql_query($Query); //Execute the query
    if(mysql_error())
    {
        $Table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {
        //Header Row with Field Names
        $NumFields = mysql_num_fields($Result);
            $Table.="<thead>";
        $Table.= "<tr style=\"background-color: #000066; text-align: center; color: #FFFFFF;\">";

        for ($i=0; $i < $NumFields; $i++)
        {   
        if($i==0){  
            $Table.= "<th>" . "</th>";}
            else {
            $Table.= "<th>" . mysql_field_name($Result, $i) . "</th>";
            }

        }

        $Table.= "</tr>";
    $Table.="</thead>";
        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt++ % 2 == 0) $Style = "background-color: #CCCCCC;";
            else $Style = "background-color: #FFFFFF;";

            $Table.= "<tr style=\"$Style\">";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $Table.= "<td>$value</td>";
            }
            $Table.= "</tr>";
        }
       // $Table.= "<tr style=\"background-color: #000066; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $Table.= "</table>";

    return $Table;

}

?>

I know I should be using MYSQLI or whatever, I will resolve that soon, just need to get this prototype up and running. Any help greatly appreciated.

The table it generates is this:

<table id="Table1" border="1" style="border-collapse: collapse; text-align: center; font-size: 10px; cellspacing: 5px; ">
<thead>
<tr style="background-color: #000066; text-align: center; color: #FFFFFF;">
</thead>
<tbody>
<tr style="background-color: #CCCCCC;">
<td>Alex O Halloran</td>
<td>2</td>
<td>5</td>
<td>2.5</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr style="background-color: #FFFFFF;">
<td>Christopher Fox</td>
<td>1</td>
<td>3</td>
<td>3.0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr style="background-color: #CCCCCC;">
<td>Dan McCrory</td>
<td>2</td>
<td>3</td>
<td>1.5</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr style="background-color: #FFFFFF;">
<td>Jacob Desborough</td>
<td>1</td>
<td>1</td>
<td>1.0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
<tr style="background-color: #CCCCCC;">
<td>Katy Hill</td>
<td>5</td>
<td>7</td>
<td>1.4</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
</tr>
</tbody>
</table>

In this example, I would need the names to be tagged as </th> – hope this helps clarify what I’m after.

  • 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-06-10T06:27:58+00:00Added an answer on June 10, 2026 at 6:27 am

    You could try using :

    $first = true;
    foreach($Row as $field => $value) {
        if ($first) {
            $Table .= "<th>$value</th>";
            $first = false;
        } else {
            $Table.= "<td>$value</td>";
        }
    }
    

    Let me know if I misunderstood your question

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm using autocomplete from jquery... and i have this: $('#name_search').autocomplete({ source: search/name.php, open: function(){
I am using this function to list items from table helps depending on parameters:
I'm using this function to extract files from .zip archive and store it on
hello i am using this Page.ClientScript.RegisterStartupScript() to call a javascript function from vb code
I am using this code to sum values from multiple radio buttons : $(document).ready(function(){
I am using following code to get bitmap from url. This function is used
I am using this function to redirect to a random post. I am trying
I'm using python's random.sample(population, k) function to generate a set of random values from
I'm using the jQuery Coundown plugin with Django to generate accurate and synced data
I'm using two functions one is from this site http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/ and its using function

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.