Jump to content

I could use some help with my code! I'm pretty new to this.


vetman

Recommended Posts

I could use some help with my code! I'm pretty new to this. I have been able to write data to a database, access the database, show the data in a table on my webpage, but only in the vertical direction. I would like to place 2 or more tables in a row, down the length of the page.

Right now i getting:

 

table

table

table

 

I would like to have:

 

table  table

table  table

table  table

 

I would appreciate any help you could offer, either it can be done or it can't be done.

Thanks in advance.

 

My code below!

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<title></title>
<head>

<link rel="stylesheet" href="./stylesheet/stylesheet1.css" media="screen">
<style type="text/css" media="screen">@import url("./stylesheet/stylesheet2.css");</style>

</head>
<body>

<?php

// Make a MySQL Connection
include 'config.php';


$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db("vetmanpc") or die(mysql_error());

// echo "Connected to Database <br>";

// Retrieve all the data from the "lakestmill" table
$result = mysql_query("SELECT * FROM lakestmill") or die(mysql_error());

// store the record of the "lakestmill" table into $row
$current = '';

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
$id = $row['id'];
if (!$current) {
echo "<br><br>";
echo "<div><table border='1' width='300'>";
$current = $id;
} elseif ($current != $id){
echo "</table></div><br><br><div><table border='1' width='300'>";
$current = $id;
}
// Print out the contents of each row into a table
echo "<tr><th width='80' height='3'> Unit No.</th><td>{$row['unit']}</td></tr>";
echo "<tr><th height='3'> Company</th><td>{$row['company']}</td></tr>";
echo "<tr><th height='3'> FirstName</th><td>{$row['firstname']}</td></tr>";
echo "<tr><th height='3'> LastName</th><td>{$row['lastname']}</td></tr>";
$email = $row['email'];
echo "<tr><th height='3'> Email</th><td><a href='mailto:$email'>$email</a></td></tr>"; 
echo "<tr><th height='3'> Address</th><td>{$row['address']}</td></tr>";
echo "<tr><th height='3'> City</th><td>{$row['city']}</td></tr>";
echo "<tr><th height='3'> State</th><td>{$row['state']}</td></tr>";
echo "<tr><th height='3'> Zip Code</th><td>{$row['zip']}</td></tr>";
echo "<tr><th height='3'> Phone</th><td>{$row['phone']}</td></tr></div>";


}
echo "</table></div>";

?>

</body>
</html>




 

 

From my understanding your tables are all listed in one row. Like

 

Unit No.

Company

FirstName

LastName

Email

Address

City

State

Zip

Phone

 

But you want it to look like:

 

Unit No. - Company - FirstName - LastName - Email - Address - City - State - Zip - Phone

 

 

If I understand correctly you could try/work with this

 

<?php


// Make a MySQL Connection
include 'config.php';

$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db("vetmanpc") or die(mysql_error());

// echo "Connected to Database <br>";
// Retrieve all the data from the "lakestmill" table
$result = mysql_query("SELECT * FROM lakestmill") or die(mysql_error());

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<title></title>
<head>

<link rel="stylesheet" href="./stylesheet/stylesheet1.css" media="screen">
<style type="text/css" media="screen">@import url("./stylesheet/stylesheet2.css");</style>

</head>
<body>


<table border='1' width='300'>
<tr>
	<th>Unit No.</th>
	<th>Company</th>
	<th>FirstName</th>
	<th>LastName</th>
	<th>Email</th>
	<th>Address</th>
	<th>City</th>
	<th>State</th>
	<th>Zip</th>
	<th>Phone</th>
</tr>

<?php
while($row = mysql_fetch_array( $result )) {
	// Print out the contents of each row into a table
	// I use <?= ... which is a cleaner way of saying <? echo ...
?>
<tr>
	<td><?= $row['unit']; ?></td>
	<td><?= $row['company']; ?></td>
	<td><?= $row['firstname']; ?></td>
	<td><?= $row['lastname']; ?></td>
	<td><a href="mailto:<?= $row['email']; ?>"><?= $row['email']; ?></a></td>
	<td><?= $row['address']; ?></td>
	<td><?= $row['city']; ?></td>
	<td><?= $row['state']; ?></td>
	<td><?= $row['zip']; ?></td>
	<td><?= $row['phone']; ?></td>
</tr>
<?php
} // END WHILE LOOP

?>

</table>

</body>
</html>

 

What I noticed is that you were using <TR> for every field. I don't know how much you know about HTML or PHP but <TR> creates a new Table Row, so by stripping out a few <TR> tags you should get the result you're looking for?

 

Hope this helps.

I could go your way, but I really wanted to do it with 2 tables in a row.

 

Unit No.                                  Unit No.

Company                                Company

FirstName                                FirstName

LastName                                LastName

Email                                      Email

Address                                  Address

City                                      City

State                                    State

Zip                                        Zip

Phone                                    Phone

 

And so on!

 

I'll keep trying.

Thanks for your time.

 

Ah ok. The way you want to do it is a little more complex because you're going to have to "jump around".

 

I'm scratching my head on this one. You know the feeling on how you know how to solve something but it's hard to explain. I'll see if I can work up an example.

 

And if any one else reads this, I'm thinking of a foreach loop and dumping the results to an array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.