Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

output table

Status
Not open for further replies.

NNNNN

MIS
Dec 2, 2002
90
GB
Hi I want to output a table with headings

the following code prints only the data

I have tried outputting the $col_name in the second while statement but I get the heading with each line of date

I have played around with it but can't get the rsult I want
whis would be something like

ID Name Title
1 John Mr
2 Jim Dr
3 Jane Mrs

I either get
1 John Mr
2 Jim Dr
3 Jane Mrs
or
ID Name Title
1 John Mr
ID Name Title
2 Jim Dr
ID Name Title
3 Jane Mrs
or
ID Name Title
ID Name Title
ID Name Title

or nothing at all

code >>>

<?php
$link = mysql_connect("mysql_host", "mysql_login", "mysql_password")
or die ("Could not connect");
print ("Connected successfully");
mysql_select_db ("my_database")
or die ("Could not select database");

$query = "SELECT * FROM my_table";
$result = mysql_query ($query)
or die ("Query failed");

// printing HTML result

print "<table>\n";
while ($line = mysql_fetch_array($result)) {
print "\t<tr>\n";
while(list($col_name, $col_value) = each($line)) {
print "\t\t<td>$col_value</td>\n";
}
print "\t</tr>\n";
}
print "</table>\n";

mysql_close($link);
?>


thanks
 
I don't have the exact code off the top of my head,
but here is what you do.

You get the column names and store them in variables first

Make arrays for each heading.

Then you get the data and put them into the array corresponding to each heading.

After all that.

You output the html header row with the column named variables.

Then the rows with the corresponding array indices.




Thanks
Tricia
 
Just place the Headings outside thw Whie Loop. Where you start the table:
Code:
...
  // printing HTML result

    print "<table>\n";
    while...

add the headings below it like:
Code:
print "<table>\n";
[red]print "<tr><td>ID</td><td>NAME</td><td>Title</td></tr>\n";[/red]
while...

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i'm assuming you want a generic answer that you can apply to any table you want.

the code below does what you want (slightly adapted from where you had got to):
Code:
<?php
    $link = mysql_connect("mysql_host", "mysql_login", "mysql_password")
        or die ("Could not connect");
    print ("Connected successfully");
    mysql_select_db ("my_database")
        or die ("Could not select database");
    
    $query = "SELECT * FROM my_table";
    $result = mysql_query ($query)
        or die ("Query failed");

    // printing HTML result

    print "<table>\n";
	$cnt = 0;
    while ($line = mysql_fetch_assoc($result)):
        print "\t<tr>\n";
        if ($cnt === 0):
			foreach ($line  as $key=>$val):
				echo "<td>$key</td>";
			endforeach;
			echo "</tr><tr>";
			$cnt++;
		endif;		
		foreach ($line as $val):
            print "\t\t<td>$val</td>\n";
        endforeach;
        print "\t</tr>\n";
    endwhile;
    print "</table>\n";
    
    mysql_close($link);
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top