Solved

How to build a multi-column HTML table?

Posted on 2001-06-09
15
354 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses four methods for overlaying images in a container on a web page
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now