Display MYSQL records in 2 html columns

I am attempting to fetch data from a database and display the records in two columns. I've given an example here:

-------------------------------------------------------------------------------------

Company Name                                  Company Name
Address                                                  Address
Phone                                                        Phone
Email                                                           Email

-------------------------------------------------------------------------------------

I have been able to get the data to display, but it displays the results one after the other down the page. Any help would be appreciated.

TIA
rjonetworksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
please show the code you have so far ...
dimajCommented:
Here's an example of how to read from mysql using php: http://www.homeandlearn.co.uk/php/php13p2.html
once you have your records, they'll be in an array and you can output your data into several columns like so:

<?PHP

$user_name = "root";
$password = "";
$database = "addressbook";
$server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);

if ($db_found) {

$SQL = "SELECT * FROM tb_address_book";
$result = mysql_query($SQL);
echo "<table>\n";
while ($db_field = mysql_fetch_assoc($result)) {
echo "<tr>"
echo "<td>" . $db_field['ID'] . ",</td>";
echo "<td>" . $db_field['ID'] . ",</td>";
echo "<td>" . $db_field['First_Name'] . ",</td>";
echo "<td>" . $db_field['Surname'] . ",</td>";
echo "<td>" . $db_field['Address'] . ",</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($db_handle);

}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}

?>

Open in new window

rjonetworksAuthor Commented:
Please remember that i am have already been able to display the records. i need to display each record in 2 column.

$result = mysql_query("SELECT * FROM directory");

while($row = mysql_fetch_array($result))
	{
	echo "<table>";
	echo "<tr>";
	echo "<td>" . "<b>" . $row['name'] . "</b>" . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . $row['street'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . $row['city'] . "," . " " . $row['state'] . " " . $row['zip'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . $row['contact'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . "Phone:" . " " . $row['phone'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . "Fax:" . " " . $row['fax'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . "Email:" . " " . $row['email'] . "</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td>" . "Web:" . " " . "<a href='http://$row['web'] . "</td>";
	echo "</tr>";
	echo "</table>";
	echo "<br />";
	}
	
mysql_close($link)

Open in new window

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

dimajCommented:
what should each column contain?
If I'm not mistaken, you're displaying each field on it's own row...

Just remove some of your <tr> </tr> statements
Guy Hengel [angelIII / a3]Billing EngineerCommented:
ok, let's modify that:
$result = mysql_query("SELECT * FROM directory");

echo "<table><tr>";

$i = 0;

while($row = mysql_fetch_array($result))
{
  $i++;
  if ($i == 2)
  {
    echo "</tr><td>";
    $i = 0;
  }

        echo "<td><b>" . $row['name'] . "</b><br/>";
        echo "" . $row['street'] . "<br/>";
        echo "" . $row['city'] . "," . " " . $row['state'] . " " . $row['zip'] . "<br/>";
        echo "" . $row['contact'] . "<br/>";
        echo "" . "Phone:" . " " . $row['phone'] . "<br/>";
        echo "" . "Fax:" . " " . $row['fax'] . "<br/>";
        echo "" . "Email:" . " " . $row['email'] . "<br/>";
        echo "" . "Web:" . " " . "<a href='http://$row['web'] . "<br/>";
        echo "</td>";
}

if ($i == 1)
{
  echo "<td></td>";
}

echo "</tr>";
echo "</table>";
        
mysql_close($link)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rjonetworksAuthor Commented:
This is what i'm trying to do. A client of ours wants to be able to add/edit/delete records through a php script that posts data to a directory containing information about each of their members. Each member has information to be displayed; i.e. name, address, phone, fax, email, web. when the directory page is visited by one of its members, a list will be shown using a fetch array, which will then display all its members in a two column table.
i am able to do this:
client 1
address1
phone1
email1
 
client2
address2
phone2
email2
\\--- but ---\\
i need the query to display the records as shown:
client1                                               client2
address1                                         address2
phone1                                             phone2
email1                                              email2
 
Thanks
dimajCommented:
ok...

how about this:

This code is not perfect and might have a few glitches (I'm rusty on html and php) but general idea is there.
$result = mysql_query("SELECT * FROM directory");

echo "<table><tr>";

$i = 0;
$arr = array();

while($row = mysql_fetch_array($result))
{
  $i++;
  $str = $row['name'] . "\n" . $row['street'] . "\n";
  $str .= $row['city] . "\n" . $row['city'] . "," . $row['state'] . " " . $row['zip'] . "\n";
  ....
  $arr[$i] = $str;
}
echo "<tr>";
for ($j = 0; $i < $i; $j++) {
 echo "<td>";
 echo $arr[$j];
 echo "<td>";
}
echo "</tr>";
echo "</table>";
        
mysql_close($link)

Open in new window

rjonetworksAuthor Commented:
@angelIII --- that seems to be the right direction. there is a little bug though. I've attached an image showing what is going on in the browser.
Oct.-19-13.21.gif
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.