Jump to content


Photo

3 or more multi column resultset


  • Please log in to reply
1 reply to this topic

#1 rkiss

rkiss
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 28 March 2006 - 12:03 AM

Hi,

I am looking for a solution for the following issue:

I am trying to split up my resultset into multiple colums, but I can't find anything that would fit my needs:

Sample Output:

A E I
B F J
C G K
D H L

Does anyone have a solution or know some good tutorial?

Thank You

RK

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 28 March 2006 - 11:56 AM

This will give

ABC
DEF
GHI

define ("NUMCOLS",3);

$res = mysql_query("SELECT area, locname FROM baagriddata");

$count = 0;
echo "<TABLE border=1>";
while (list($col1, $col2) = mysql_fetch_row($res)) {

    if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row

    echo "<TD>$col1<br>$col2</TD>\n";
    $count++;

    if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}
if ($count % NUMCOLS != 0) {
   # end row if not already ended
   while ($count++ % NUMCOLS) echo "<td>&nbsp;</td>";
   echo "</TR>\n";
}
echo "</TABLE>";

To do it as you asked will reqire storing tha data into an array , calculating how many rows are needed, R, then outputting record n, n+R, n+2R into each row.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users