Dees Posted October 17, 2007 Share Posted October 17, 2007 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 More sharing options...
Reg Edit Posted October 17, 2007 Share Posted October 17, 2007 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 More sharing options...
Dees Posted October 17, 2007 Author Share Posted October 17, 2007 ?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 More sharing options...
justsomeguy Posted October 17, 2007 Share Posted October 17, 2007 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 More sharing options...
Dees Posted October 17, 2007 Author Share Posted October 17, 2007 Where in the code would I put those echo statements? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 17, 2007 Share Posted October 17, 2007 Replace the while loop that is already there. Link to comment Share on other sites More sharing options...
Dees Posted October 18, 2007 Author Share Posted October 18, 2007 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 More sharing options...
Dees Posted October 18, 2007 Author Share Posted October 18, 2007 And how do I add the name of the column at the top of each column. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2007 Share Posted October 18, 2007 It's already there, did you even run it yet? There's no substitute for asking questions like actually trying it and seeing what happens. Link to comment Share on other sites More sharing options...
Dees Posted October 18, 2007 Author Share Posted October 18, 2007 I did run it and I have not the title of each column does not appear. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2007 Share Posted October 18, 2007 This line is displaying the headings:echo "<tr><td>First Name</td><td>Last Name</td><td>Age</td></tr>";You can style that out to make it stand out more or change the text. Link to comment Share on other sites More sharing options...
Dees Posted October 18, 2007 Author Share Posted October 18, 2007 Ill try applying the code again, maybe I did something wrong. If you apply the style that line of code will it show on all of the entries into the database. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 18, 2007 Share Posted October 18, 2007 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 More sharing options...
Dees Posted October 19, 2007 Author Share Posted October 19, 2007 Is it possible to apply a CSS tag that woud apply to the entire table? Link to comment Share on other sites More sharing options...
Dees Posted October 19, 2007 Author Share Posted October 19, 2007 I applied the code again and now it looks much better because everything is strait. But it is not showing the borders of the table. And again, can I create a CSS class for <table>? Link to comment Share on other sites More sharing options...
Reg Edit Posted October 19, 2007 Share Posted October 19, 2007 I applied the code again and now it looks much better because everything is strait. But it is not showing the borders of the table. And again, can I create a CSS class for <table>?Yes. Check out CSS Table in the CSS reference on W3Schools. Link to comment Share on other sites More sharing options...
Dees Posted October 19, 2007 Author Share Posted October 19, 2007 How would you control it with a separate style sheet so that I can just have one tag for the entire table? Link to comment Share on other sites More sharing options...
Dees Posted October 19, 2007 Author Share Posted October 19, 2007 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 More sharing options...
Synook Posted October 20, 2007 Share Posted October 20, 2007 Change the <table> echo line to echo "<table style=\"border:1px; \">"; Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.