Jump to content

Displaying Table in HTML Table With CSS


Dees
 Share

Recommended Posts

I have just successfully created a database that takes input from a form and records the data. I can view the data but it displays one long list on a white page. I have seen people who created a table and then had it being displayed in an HTML table where the names of each column are labeled with color and various other decorations. I was wondering how I do this. Thanks.

Link to comment
Share on other sites

I have just successfully created a database that takes input from a form and records the data. I can view the data but it displays one long list on a white page. I have seen people who created a table and then had it being displayed in an HTML table where the names of each column are labeled with color and various other decorations. I was wondering how I do this. Thanks.
It will probably be best if you can post your code as it is, that displays the one long list from the database, and someone can show how that can be changed to use a table.
Link to comment
Share on other sites

?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM person ORDER BY LastName");while($row = mysql_fetch_array($result)) { echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age']; echo "<br />"; }mysql_close($con);?>I am just using the standard code from w3schools.com.

Link to comment
Share on other sites

To create the basic table you can do this. Check the CSS reference on w3schools for information on how to style it.

echo "<table>";echo "<tr><td>First Name</td><td>Last Name</td><td>Age</td></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>{$row['FirstName']}</td>";echo "<td>{$row['LastName']}</td>";echo "<td>{$row['Age']}</td>";echo "</tr>";}echo "</table>";

Link to comment
Share on other sites

So like this?<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM person ORDER BY age");echo "<table>";echo "<tr><td>First Name</td><td>Last Name</td><td>Age</td></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>{$row['FirstName']}</td>";echo "<td>{$row['LastName']}</td>";echo "<td>{$row['Age']}</td>";echo "</tr>";}echo "</table>";mysql_close($con);?>

Link to comment
Share on other sites

The style you apply to that will only apply to that row. You can style either the tr or td tags inside the loop to style each individual row, like alternating the background color.echo "<tr style=\"background-color: #000000; color: #FFFFFF; font-size: 14px; font-weight: bold;\"><td>First Name</td><td>Last Name</td><td>Age</td></tr>";

Link to comment
Share on other sites

This is the code that I am using and it is not displaying the borders of the table:<?php$con = mysql_connect("db205.perfora.net","dbo199312593","D6BZFjHg");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("db199312593", $con);$sql="INSERT INTO person (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("db199312593", $con);$result = mysql_query("SELECT * FROM person ORDER BY age");echo "<table style="border:1">";echo "<tr><td>First Name</td><td>Last Name</td><td>Age</td></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>{$row['FirstName']}</td>";echo "<td>{$row['LastName']}</td>";echo "<td>{$row['Age']}</td>";echo "</tr>";}echo "</table>";mysql_close($con);?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...