alternate field header names in website
Posted: Sun Nov 22, 2009 10:52 pm
Here is a basic "print out my table" script:
How can I display an alternate field name for my columns? What do I need to change?
Like if I want to have the field 'name' displayed as "Name" in the table header row,
the field 'yesterday' displayed as "As of Yesterday"
'today' as "As of Today"
Code: Select all
<?php
session_start();
require_once('inc/constant.php');
require_once('inc/function.php'); //basic functions
db_on();
$table = 'mytable';
// sending query
$sql = "SELECT `name`, `yesterday`, `today`, `difference`, `quota`, `date` FROM {$table} WHERE `active` = 1 ";
$res=mysql_query($sql,_sql); //_sql is my link to the db definened in funtions.php
if (!$res) {
die("Query to show fields from table failed");
}
$fields_num = mysql_num_fields($res);
echo "<table border='1' align='center'><tr>";
// printing table headers
for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($res);
echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
$i=0;
while($row = mysql_fetch_row($res)){
echo "<tr>";
if (($i%2)==0){
// if (($temp) < 0){
foreach($row as $cell)
echo "<td><font color='FF0000'>$cell</font></td>";
}
else{
foreach($row as $cell)
echo "<td><font color='0000FF'>$cell</font></td>";
}
$i++;
echo "</tr>\n";
}
mysql_free_result($res);
?>
Like if I want to have the field 'name' displayed as "Name" in the table header row,
the field 'yesterday' displayed as "As of Yesterday"
'today' as "As of Today"