Solved

How to build a multi-column HTML table?

Posted on 2001-06-09
15
366 Views
Last Modified: 2006-11-17
I can't seem to figure out the logic to build a multiple column html table with the results from my query. There are a few catches as well. Here is what I mean.

Lets say my query returns 6 rows. Each row has two fields. We'll call them;

1a,1b,2a,2b,3a,3b,4a,4b,5a,5b,6a,6b

I would like them to be set up in a two column table with 12 rows, like this.

1a     |     4a
  1b     |       4b
2a     |     5a
  2b     |       5b
3a     |     6a
  3b     |       6b

I am just not sure how to do it.

Any ideas?
0
Comment
Question by:mfuse
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 2
  • +4
15 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6172090
Do you always get the same number of rows and fields returned?

Ant
0
 
LVL 3

Expert Comment

by:superschlonz
ID: 6172404
Is it an SQL query ?
If yes isn't it easier to let the database do the work for you ?
0
 
LVL 4

Expert Comment

by:ykf2000
ID: 6172449
hi try this:

$SQL = "SELECT your_data FROM our_table_name";
$result = mysql_query($SQL);
$num = 0;
echo "<table>";
while($row=mysql_fetch_array($result))
{
    if($num % 2 ==0)
       echo "<tr>";

    echo "<td>".$row[$num]."</td>";
   
    $num = $num + 1;

    if($num % 2 ==0)
       echo "</tr>";
}
echo "</table>";
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:ykf2000
ID: 6172456
ooooh and if the number is not even:


$SQL = "SELECT your_data FROM our_table_name";
$result = mysql_query($SQL);
$num = 0;
echo "<table>";
while($row=mysql_fetch_array($result))
{
   if($num % 2 ==0)
      echo "<tr>";

   echo "<td>".$row[$num]."</td>";
   
   $num = $num + 1;

   if($num % 2 ==0)
      echo "</tr>";
}

if($num % 2 != 0)
   echo "<td>&nbsp;</td></tr>";

echo "</table>";
0
 
LVL 3

Expert Comment

by:superschlonz
ID: 6173924
Hi ykf2000

This will create the following table (if you change $row[$num] to $row["your_data"]):

1a | 1b
2a | 2b
3a | 3b
4a | 4b
5a | 5b
6a | 6b

The following code should create the right output:

$SQL = "SELECT your_data FROM our_table_name";
$result = mysql_query($SQL);
$num = 0;
while($row=mysql_fetch_array($result))
{
  $tab[$num] = $row["your_data"];
  $num++;
}
if($num % 2 != 0)
{
  $tab[$num] = "";
  $num++;
}
echo "<table>";
$i1 = 0;
$i2 = $num/2;
while( $i2 < $num )
{
  echo "<tr><td>" . $tab[$i1] . "</td><td>" . $tab[$i2] . "</tr>";
  $i1++;
  $i2++;
}
echo "</table>";
0
 

Author Comment

by:mfuse
ID: 6175164
It is an SQL query, what do you mean, let the database do the work for me? I would prefer that, if possible.
0
 

Author Comment

by:mfuse
ID: 6175176
No I do not always get the same number of rows, (and  sometimes its even and sometimes is an odd number) but yes to the same number of fields every time.
0
 

Author Comment

by:mfuse
ID: 6175338
The select statment would be:

SELECT a,b FROM main

Also, the number of rows in the above example woud be 6, not 12, I made an error in stating 12.
0
 

Author Comment

by:mfuse
ID: 6175363
One other thing. I want the color of the every two rows to alternate.

white -1a | 4a
white -1b | 4b
grey   -2a | 5a
grey   -2b | 5b
white -3a | 6a
white -3b | 6b
0
 
LVL 3

Expert Comment

by:superschlonz
ID: 6176540
OK, you have a table with column a and column b. You probably have some more
columns for example a unique numer 'nr'. So you could use a statement like this:

select m1.a a1, m1.b b1, m2.a a2, m2.b b2
from main m1, main m2
where m1.nr + 3 = m2.nr
and m1.nr between 1 and 3

But this always gets an even number of values. For getting odd number of values
you must use an outer join:

select m1.a a1, m1.b b1, m2.a a2, m2.b b2
from main m1, outer main m2
where m1.nr + 3 = m2.nr
and m1.nr between 1 and 3
and m2.nr < 6

or with the oracle syntax:

select m1.a a1, m1.b b1, m2.a a2, m2.b b2
from main m1, main m2
where m1.nr + 3 = m2.nr(+)
and m1.nr between 1 and 3
and m2.nr(+) < 6

( the numbers I used in the where statement may be constant or not
depending on your table layout. perhaps they could also be created on
the fly with a subquery )

the php script would then look like this:

$SQL = <the statement>
$result = mysql_query($SQL);
$num = 0;
echo "<table>";
$rownr = 0;
while($row=mysql_fetch_array($result))
{
  if( $colnr % 2 == 0 )
  {
    $rowor = "#FFFFFF";
  }
  else
  {
    $rowor = "#CCCCCC";
  }
  echo "<tr bgcolor=\"" . $color . "\"><td>" . $row["a1"] . "</td>";
  echo "<td>" . $row["a2"] . "</td></tr>";
  echo "<tr bgcolor=\"" . $color . "\"><td>" . $row["b1"] . "</td>";
  echo "<td>" . $row["b2"] . "</td></tr>";
  $rownr++;
}
echo "</table>";
0
 

Accepted Solution

by:
swangy earned 50 total points
ID: 6197579
The easiest way to do this is as follows:

1. obtain your recordset as you've decribed - 1a,1b,2a,2b,3a,3b,4a,4b,5a,5b,6a,6b

2. let the HTML do the work - make two nested tables inside an outer table, as follows:
<?
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);
?>

<table>
<tr>
   <td>
   <table>
   <?
   for ($count = 1; $count < $num_rows/2; ++$count)
   {
       print "<tr><td bgcolor=". $num_rows%2?$grey:$white .">".mysql_fetch_row($result)."</td></tr>"
   }
   ?>
   </table>
   </td>
</tr>
<tr>
   <td>
   <?
   for (; $count <= $num_rows; ++$count)
   {
       print "<tr><td bgcolor=". $num_rows%2?$grey:$white .">".mysql_fetch_row($result)."</td></tr>"
   }
   ?>
   </td>
</tr>
</table>
0
 

Author Comment

by:mfuse
ID: 6198892
Great suggestion. I have alread obtained the answer that I am using from another forum, but this seems like it would work too.  My current solution, counts the number of results, finds the half point, assigns the results to an array, gives each field a row number attached as [x] and then draws the table using the fields[x] and and a simple calculation. Your way looks like a lot less logic, but I am not sure which is better more server side or more client side. Anyway, thanks for thinking outside the box with a simple answer.
0
 

Author Comment

by:mfuse
ID: 6198893
Great suggestion. I have alread obtained the answer that I am using from another forum, but this seems like it would work too.  My current solution, counts the number of results, finds the half point, assigns the results to an array, gives each field a row number attached as [x] and then draws the table using the fields[x] and and a simple calculation. Your way looks like a lot less logic, but I am not sure which is better more server side or more client side. Anyway, thanks for thinking outside the box with a simple answer.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6419389
This question was LOCKED with a PROPOSED ANSWER and awaits your decision today.  Once a question is LOCKED with a Proposed Answer, few new experts will step in to help on that question, since the assumption is, you've been helped.  If the Proposed Answer helped you, please accept it and award that expert.  If it did not help you, please reject it and add comments as to status and what else is needed.
 
If you wish to award multiple experts, just comment here with detail, I'll respond as soon as possible.  As it stands today, you asked the question, got help and not one expert was awarded for the contribution(s) made.  Your response is needed.  I'll monitor through month end, and if you've not returned to complete this, we'll need to decide.  Expert input is welcome (as always) to determine the outcome here if the Asker does not respond.
 
Your response in finalizing this (and ALL) your question(s) is appreciated.
 
Moondancer
Community Support Moderator @ Experts Exchange

0
 

Expert Comment

by:SpideyMod
ID: 8287154
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question