473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Printing mysql_fetch_array results in multicolumn table

I'm not sure if this qualifies as a mysql or a php question so I'm
asking in both groups.

I am pulling the results of a mysql query from my database and want to
print the results into a two column table. I know how to get the results
into a single column table just fine using:

while($row = mysql_fetch_array($result)) {
print "<table border=2><tr><th>" . $row[name];
print "<tr><td>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td></tr>";
print "</table>\n";
print "<br><br>";
}

and it works fine. But my efforts to get the reults into a two column
setup have become frustrating. My latest attempt was:

while($row = mysql_fetch_array($result)) {
print "<table border=2 width='90%'>";
print "<tr>";
print "<td>";
print "<b>" . $row[name] . "</b><br>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td>";
print "<td>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td>";
print "</tr>";
print "</table>\n";
print "<br><br>";
}

This puts the same data from a single result in both <td> fields though.
I want it to put data from the first result in the left <td> and data
from the second result in the right side and continue on from there
until the end.

What am I overlooking on this to make it work as I need it to?
May 1 '06 #1
1 3885
JackM wrote:
I'm not sure if this qualifies as a mysql or a php question so I'm
asking in both groups.

I am pulling the results of a mysql query from my database and want to
print the results into a two column table. I know how to get the results
into a single column table just fine using:

while($row = mysql_fetch_array($result)) {
print "<table border=2><tr><th>" . $row[name];
print "<tr><td>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td></tr>";
print "</table>\n";
print "<br><br>";
}

and it works fine. But my efforts to get the reults into a two column
setup have become frustrating. My latest attempt was:

while($row = mysql_fetch_array($result)) {
print "<table border=2 width='90%'>";
print "<tr>";
print "<td>";
print "<b>" . $row[name] . "</b><br>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td>";
print "<td>";
print mysql_field_name($result, 0) . ": " . $row[ID]."<br>";
print mysql_field_name($result, 1) . ": " . $row[name]."<br>";
print mysql_field_name($result, 2) . ": " . $row[address]."<br>";
print mysql_field_name($result, 3) . ": " . $row[city]."<br>";
print mysql_field_name($result, 4) . ": " . $row[telephone]."<br>";
print "</td>";
print "</tr>";
print "</table>\n";
print "<br><br>";
}

This puts the same data from a single result in both <td> fields though.
I want it to put data from the first result in the left <td> and data
from the second result in the right side and continue on from there
until the end.

What am I overlooking on this to make it work as I need it to?


Not completely sure what you want exactly, but there are some weird
constructs in both cases. You create a complete table for each db row it
seems. The table tages should be outside the loop I suppose.

It is no surprise in the second example you get the same data in both
columns, since that is what you ordered. If what you aim for is a table
looking like this:

ID <ID value for row x>
name <name value for row x>

[etc]

ID <ID value for row y>
name <name value for row y>

[etc]

then replace all the <td> </td> contents of the first column with
appropriate contents.

Sh.
--
Sigh. I like to think it's just the Linux people who want to be on
the "leading edge" so bad they walk right off the precipice.
(Craig E. Groeschel)
May 2 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: kaptain kernel | last post by:
i'm trying to find a simple way of printing out the keys in an array generated by mysql_fetch_array which returns a single row. i.e. while ($row=mysql_fetch_array($result) ) { print out...
1
by: bill | last post by:
Hi, This looks like a simple problem, but I just can't seem to wrap my head around it. I'm trying to build a table dynamically, based on a result set from mysql. There will be 9 results for a...
1
by: billbealey | last post by:
I'm struggling how to format out in a nice html table after doing a query. Query is: $result = mysql_query("SELECT state_name, county_name, party_name, votes FROM regions, county, party,...
15
by: Good Man | last post by:
Hey there I have a dumb question.... Let's say i have a database full of 4000 people.... I select everything from the database by: $result = mysql_query("SELECT * FROM People");
4
by: Marcel Brekelmans | last post by:
Hello, I seem to get an extra empty field in every 'mysql_fetch_array' command I issue. For example: I have a simple table 'tblName': ID Name 1 Jane 2 Joe 2 Doe
5
by: Thomas F.O'Connell | last post by:
We've got a table that has a definition as follows: CREATE TABLE linking_table ( fk int8 REFERENCES source_table( pk1 ), value int8, PRIMARY KEY( fk1, value ) ); I would've thought that...
2
by: gthomas | last post by:
hi, i am having trouble printing the records form my database. each row has a topic and headline. the topic should print and then the headlines for that topic, then the next topic and headlines. ...
5
by: newbie | last post by:
table `user` --------------------------------------- | id | user_name | gender | --------------------------------------- I can gurantee that ids in table user are unique (so each number...
3
by: jmooney5115 | last post by:
Hey. I am new to php and am trying to learn. What I'm doing is querying(did I spell this right?) a mySQL database and putting the results into a table on a webpage. I have worked for hours on this...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.