Jump to content

[SOLVED] Displaying results in columns


Sydcomebak

Recommended Posts

<?php 
$result = mysql_query("SELECT * 
FROM FamilyTbl
INNER JOIN PeopleTbl ON (FamilyTbl.Name_ID = PeopleTbl.NameID) 
WHERE FamilyTbl.House_ID = '$address'
ORDER BY NameLast, NameFirst
") OR die(mysql_error());

WHILE ($row = mysql_fetch_array($result) )  { 

   echo $row[NameLast]. ", ". $row[NamePrefix]. " ". $row[NameFirst]. " ". $row[NameMiddle]. $row[NameSuffix]. " "; 
}
?>

 

OK, some of these queries return A LOT of names.  I'd like to be able to display them in columns in a table like so:

 

Charne, Mr. Michael   Glanger, Mrs. Karin      Kling, Mr. Wayne  
Charne, Mrs. Suzette  Glanger, Mr. Trevor      Lazarow, Mrs. Fiona  
Charney, Mrs. Linda   Jochelson, Mrs. Barbara  Lazarow, Mr. Mark  
Charney, Mr. Norman   Jochelson, Mr. Neil      Norton, Mr. Charles  
Cohen, Mr. Brendan    Karlan, Mr. Dennis       Norton, Mrs. Jodi  
Cohen, Mrs. Joanna    Karlan, Mrs. Helen       Roy, Mr. Michael  
Flekser, Mrs. Jean    Kling, Mrs. Danielle     Roy, Mrs. Nicki  
Frysh, Dr. Howard     Kling, Mrs. Melanie      Tsafrir, Mrs. Lauren  
Frysh, Mrs. Sandra    Kling, Mr. Nevil         Tsafrir, Mr. Thomer  

 

That way it reads top to bottom THEN left to right.

 

math-wise, it's simple to set up:

<table>
for row_loop=1 to numrows
<tr>
for col_loop = 1 to numcols
<td>
echo result(col_loop-1)*(numrows)+row_loop
</td>
next col_loop
</tr>
next row_loop
</table>

 

Anyone want to give this a shot?

Link to comment
https://forums.phpfreaks.com/topic/119668-solved-displaying-results-in-columns/
Share on other sites

Here's the solution based on revraz's link:

 

<?php

$result = mysql_query("SELECT * 
FROM FamilyTbl
INNER JOIN PeopleTbl ON (FamilyTbl.Name_ID = PeopleTbl.NameID) 
WHERE FamilyTbl.House_ID = '$address'
ORDER BY NameLast, NameFirst
") OR die(mysql_error());

   echo "<table>"; 

if($result && mysql_num_rows($result) > 0)
{
$i = 0;
$max_columns = 3;

WHILE ($row = mysql_fetch_array($result) )  { 
// make the variables easy to deal with
extract($row);
// open row if counter is zero
if($i == 0)
   echo "<tr>"; 
// make sure we have a valid product
if($row[FamilyID] != "" && $row[FamilyID] != null)
   echo "<td>"; 
   echo $row[NameLast]. ", ". $row[NamePrefix]. " ". $row[NameFirst]. " ". $row[NameMiddle]. $row[NameSuffix]. " "; 
   echo "</td>"; 

// increment counter - if counter = max columns, reset counter and close row
if(++$i == $max_columns) 
{
   echo "</tr>"; 
$i=0;
} // end if 
} // end while
} // end if results

   echo "</table>"; 

?>

 

Thanks revraz!!!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.