...

View Full Version : Column Titles



idewar
03-12-2013, 05:14 PM
Hi all,
I'm only now breaking myself into the coding world but am STUMPED on what to do here. Basically all I want to do is add the column titles to the table that is generated by the following code... any help would be gratefully recieved!

Thanks

Lloyd. :)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Display Contracts</title>
<link rel="stylesheet" type="text/css" href="contractapp.css" />
</head>
<body>
<div id="container" class="containerDiv">
<div class="headerDiv"><h1>Display Contracts</h1></div>
<div class="menuDiv"><a href="home.html">Main menu</a></div>
<div class="contentDiv">

<!-- *********************** START OF MAIN CONTENT *********************** -->

<?php
include("dbinfo.inc.php");
$comm=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db($database) or die( "Unable to select database");
$sql="SELECT * FROM contract";
$result = mysql_query($sql) or die("SELECT Error: ".mysql_error());
echo "<table class=\"table\">\n";
while ($get_info = mysql_fetch_row($result))
{
echo "<tr>\n";
foreach ($get_info as $field)
echo "<td class=\"table\">&nbsp$field&nbsp&nbsp</td>\n";
echo "</tr>\n";
}
echo "</table>\n";
mysql_close();
?>
</div>

<!-- ************************ END OF MAIN CONTENT ************************ -->

<div class="footerDiv">Footer Text</div>
</div>
</body>
</html>

Fou-Lu
03-12-2013, 07:41 PM
Its a relatively simple task to do this. You can grab the name using the mysql_field_name function:


print('<tr>');
for ($i = 0; $i < mysql_num_fields($sql); ++$i)
{
printf('<td>%s</td>', mysql_field_name($sql, $i));
}
print('</tr>');


Also, in the future please wrap your code in
or
tags to preserve the formatting.

idewar
03-12-2013, 08:01 PM
Great thanks, sorry I didn't know about the tags.

Also where in relation to my code do I add the code you gave me, sorry for being such a major novice!

thanks again!

Fou-Lu
03-12-2013, 08:09 PM
I'd put it before the while loop, then the while loop will populate the recordset with the data.

idewar
03-12-2013, 08:48 PM
Hey thanks for the reply this is helping me out a lot! I popped you'r code into the SQL but when it displays the results its as if that code doesn't exist ie. no title :)

any thoughts thanks again

lloyd

Old Pedant
03-12-2013, 08:59 PM
But I would like to suggest an alternate answer.

When you do SELECT * FROM contract that means you are getting *ALL* the columns from that contract table.

Very seldom will you do this in real life.

Suppose that contract had the following columns:
contractid -- INT AUTO_INCREMENT, just for internal control
contractdate
company
address
city
state
zip
phone
contactperson
contractlength
contractdeatils LONGTEXT (e.g., the full text of the contract)

*SURELY* you don't want to dump out *ALL* those fields. Especially the contractdetails and the contractid, say.

IN GENERAL, you should *AVOID* using SELECT * and, instead, select *ONLY* those fields you really need and want to show in your report. On top of that, you often want the fields to be in a different order in the report than they are in the DB. And, finally, you often want the column titles to be more human readable than the names in the db. So you might do something like


SELECT company, contactperson, phone, contractdate, contractlength
FROM contract
ORDER BY company

And now you don't *NEED* to get the names from the query using mysql_field_name.

Instead, just use HTML and code it by hand:


<html>
<body>
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Phone</th>
<th>Start date</th>
<th>Length</th>
</tr>
<?PHP
$sql = "SELECT company, contactperson, phone, contractdate, contractlength
FROM contract
ORDER BY company";
... etc ...

Old Pedant
03-12-2013, 09:02 PM
Fou-Lu used the wrong variable, $sql. To match your code, he needed to use $result.



$result = mysql_query($sql) or die("SELECT Error: ".mysql_error());
...
print('<tr>');
for ($i = 0; $i < mysql_num_fields($result); ++$i)
{
printf('<td>%s</td>', mysql_field_name($result, $i));
}
print('</tr>');

(And I don't even use PHP. <grin/>)

idewar
03-12-2013, 09:05 PM
Hi there i appreciate the answer, however i do need to the report to show all columns, and there are a few tables I've already done that display without the headings, it would be great to have a quick fix if one exists,
again i appreciate the response.

Lloyd

idewar
03-12-2013, 09:08 PM
old pendant perfect thanks!!! however they display like this....

contractidnamecustomernumbertelephone

rather than

contract id | name | customer number | telephone |

any thoughts

thanks!

Fou-Lu
03-12-2013, 09:19 PM
rofls yep I sure did use the wrong one.
Check the source code, does it show its blocked in the tr and td tags? If so, just make sure it's blocked by the table tags.
I also agree to use explicit naming. I rarely name my properties exactly how I want them to display (ie: pid and aid don't really make any sense in a display name).

Old Pedant
03-12-2013, 09:32 PM
Here. Let's write it out for you. You surely just have it in the wrong place.


<?php
include("dbinfo.inc.php");
$comm=@mysql_connect(localhost,$username,$password);
$rs=@mysql_select_db($database) or die( "Unable to select database");
$sql="SELECT * FROM contract";
$result = mysql_query($sql) or die("SELECT Error: ".mysql_error());

echo "<table class=\"table\">\n";

echo "<tr>\n";
for ($i = 0; $i < mysql_num_fields($result); ++$i)
{
printf("<td>%s</td>\n", mysql_field_name($result, $i));
}
echo "</tr>\n";

while ($get_info = mysql_fetch_row($result))
{
echo "<tr>\n";
foreach ($get_info as $field)
{
echo "<td class=\"table\">&nbsp$field&nbsp&nbsp</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
mysql_close();
?>

It seems VERY VERY STRANGE, to me, that you would give your <table> the same class that you give a <td>. *SURELY* <td class="table"> is an error?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum