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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

changing a table display format

Status
Not open for further replies.

biscuitboy

Technical User
Dec 11, 2004
22
0
0
GB
Hiya,

The script below returns all of records stored in a mysql database and displays them in a table. The current table is displayed like the following:

Surname First Name Flat No etc etc etc
-----------------------------------------------------
record1data data data etc etc etc
record2data data data etc etc etc

However I would like to display it like this:

Surname: Record1data
First name: data
flat no: data
etc
etc

Surname: Record2data
First name: data
Flat no: data
etc
etc

Does anyone have any ideas how I can change the existing code so that the table is formed like the above for each record?

Best Regards


Code:

<?php # Scrtipt 6.6 - viewresidents.php


$page_title = 'View Residents';

require_once ('connect.php'); // Connect to the db.


//Make the Query
$query = "SELECT surname, first_name, flat_no, building_name, tel_no, organisations, Disabilities, Reasons FROM Residents ORDER BY surname";
$result = mysql_query ($query); // run the query
$num = mysql_num_rows ($result); // how many users are there
if ($num > 0) { // if it ran ok display the records

echo "<p><big><b> There are currently $num residents registered.</b></big></p>";

echo '<table align="centre"
cellspacing="2" cellpadding="4">
<tr><b><td align="left"><b>Surname</b>
</td><td align="left"><b>First Name</b>
</td><td align="left"><b>Flat No</b>
</td><td align="left"><b>Building Name</b>
</td><td align="left"><b>Tel No</b>
</td><td align="left"><b>Organisations</b>
</td><td align="left"><b>Disabilities</b>
</td><td align="left"><b>Reasons</b>
</td></tr>
';
//fetch and print all the records.
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
echo "<tr><td align=\"left\">" .
stripslashes($row[0]) . "</td>
<td align=\"left\">$row[1]</td>
<td align=\"left\">$row[2]</td>
<td align=\"left\">$row[3]</td>
<td align=\"left\">$row[4]</td>
<td align=\"left\">$row[5]</td>
<td align=\"left\">$row[6]</td>
<td align=\"left\">$row[7]</td>
</tr>\n";
}

echo '</table>';
mysql_free_result ($result); //Free up the resources.

} else { // if it did not run ok
echo '<p>There are currently no registered residents.</p>';
}

mysql_close(); // close the database connection

?>
 
Code:
echo "<table align=\"centre\" cellspacing=\"2\" cellpadding=\"4\">";
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
echo "
<tr>
  <td align="left"><b>Surname</b></td>
  <td align=\"left\">" . stripslashes($row[0]) . "</td>
</tr>
<tr>
  <td align="left"><b>First Name</b></td>
  <td align=\"left\">$row[1]</td>
</tr>
<tr>
  <td align=\"left\">Flat No</td>
  <td align=\"left\">$row[2]</td>
</tr>
<tr>
  <td align="left"><b>Building Name</b></td>
  <td align=\"left\">$row[3]</td>
</tr>
<tr>
  <td align=\"left\">Tel No</td>
  <td align=\"left\">$row[4]</td>
</tr>
<tr>
  <td align="left"><b>Organisation</b></td>
  <td align=\"left\">$row[5]</td>
</tr>
<tr>
  <td align=\"left\">Disabilities</td>
  <td align=\"left\">$row[6]</td>
</tr>
<tr>
  <td align=\"left\">Reasons</td>
  <td align=\"left\">$row[7]</td>
</tr>
\n";
}
echo "</table>";
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top