Forum Moderators: coopster

Message Too Old, No Replies

Formatting DB results (new to PHP)

         

gdogfunk

5:36 pm on Sep 23, 2004 (gmt 0)

10+ Year Member



Hello,

I have seen this topic on here, but none of the answers directly reflect what I am trying to do. Being new to PHP, I cannot apply the examples to my situation. What I am trying to do is probably very simple, but I need some help.

I have created a DB and have even created a form for my client to add new retailers to the DB. I created a map of the USA using an imagemap to link to the DB. So, if a customer is looking for a retailer in Ohio, they click Ohio, and a new page displays the results by alphabetical company name.

Right now, I have the results display like this:

company_name
address
city
state
zip
phone

I would like to be able to display each company in a horizontal table to look like:

company_name ¦ address ¦ city ¦ state ¦ zip ¦ phone

I cannot seem to find a good explanation of how to use html and table formatting within the PHP script.

Is it possible to even determine the color of the table cells as it writes the table?

The code I am currently using is this:

php

mysql_connect ("XX.XXX.XX.XX", "nobody", "nobody");

mysql_select_db (retailers);

if ($state == "")
{$state = '%';}

$result = mysql_query ("SELECT * FROM retailer_list
WHERE state LIKE '$state%'
ORDER BY company_name
");

if ($row = mysql_fetch_array($result)) {

do {
print $row["company_name"];
print ("<br>");
print $row["address"];
print ("<br>");
print $row["city"];
print ("<br>");
print $row["state"];
print ("<br>");
print $row["zip"];
print ("<br>");
print $row["country"];
print ("<br>");
print $row["phone"];
print ("<br>");
print ("--------------------------------------");
print ("<br>");
} while($row = mysql_fetch_array($result));

} else {print "Sorry, Products are not yet available in this state!";}

Getting the table format will be great for the US version, but I am also working on a European version of the site and have had a request for the results to display differently.

How can you populate results from a DB without using an external query? I mean how can I have this page load and automatically retrieve the data?

Any help with this is GREATLY appreciated.

Thanks!

Ryan

[edited by: jatar_k at 6:15 pm (utc) on Sep. 23, 2004]
[edit reason] removed url [/edit]

Birdman

5:50 pm on Sep 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WW, gdog!

What you want to do isn't too tough. What you do is: First, start the table before you loop and print the results.

if ($row = mysql_fetch_array($result)) {

print '<table>';

Then, during your loop, print the other tags(<tr> and <td>) along with the actual info.

do {
print '<tr>';
print '<td>' . $row['company_name'] . '</td>';
print '<td>' . $row['address"] . '</td>';
print '<td>' . $row['city']; . '</td>'
print '<td>' . $row['state'] . '</td>';
print '<td>' . $row['zip'] . '</td>';
print '<td>' . $row['country'] . '</td>';
print '<td>' . $row['phone'] . '</td>';
print '</tr>';
} while($row = mysql_fetch_array($result));

Then, finally, print the closing table tag:

print '</table>';

Regards,
Birdman

gdogfunk

6:46 pm on Sep 23, 2004 (gmt 0)

10+ Year Member



Thanks!

This works great, but now the text displays black and not white as before. I tried a few things, but I cannot seem to get the text to be white.

Any ideas?

Thanks!

Ryan

Birdman

7:11 pm on Sep 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure thing! Try adding a little CSS to change the color of the text.

print '<table style="color: #fff;">';

You're rollin' now! PHP and CSS in one day :)

Birdman