Solved

How to build a multi-column HTML table?

Posted on 2001-06-09
15
357 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

863 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

18 Experts available now in Live!

Get 1:1 Help Now