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.
You could try using :
Let me know if I misunderstood your question