Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

How to build a multi-column HTML table?

Posted on 2001-06-09
15
Medium Priority
?
396 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 200 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

571 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