Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 400
  • Last Modified:

How to build a multi-column HTML table?

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
mfuse
Asked:
mfuse
  • 6
  • 3
  • 2
  • +4
1 Solution
 
a.marshCommented:
Do you always get the same number of rows and fields returned?

Ant
0
 
superschlonzCommented:
Is it an SQL query ?
If yes isn't it easier to let the database do the work for you ?
0
 
ykf2000Commented:
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
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ykf2000Commented:
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
 
superschlonzCommented:
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
 
mfuseAuthor Commented:
It is an SQL query, what do you mean, let the database do the work for me? I would prefer that, if possible.
0
 
mfuseAuthor Commented:
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
 
mfuseAuthor Commented:
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
 
mfuseAuthor Commented:
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
 
superschlonzCommented:
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
 
swangyCommented:
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
 
mfuseAuthor Commented:
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
 
mfuseAuthor Commented:
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
 
MoondancerCommented:
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
 
SpideyModCommented:
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 6
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now