Jump to content

[SOLVED] Displaying results in columns - Part II


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:

$num_results = number_of_$results;
$numcols = 3;
$numrows = trunc($numresults/numcols);
<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?

 

Revraz already directed me to http://www.phpfreaks.com/forums/index.php/topic,95426.0.html but that displayed the data from left to right then up to down like so:

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

 

It's a good temp solution, but if anyone is good with for loops, I'd appreciate the help, thx!

 

-Dave

Link to comment
Share on other sites

Can you help me with what the $max = 11 is for?

 

$i=1;
$max=11;
echo "<table style=\"display:inline\">";
while ($row = mysql_fetch_array($result) )
{
if (!($i%$max))
	echo "</table><table style=\"display:inline\">";
echo "<tr><td>" . $row['name'];
$i++;
}

 

I'll play with it to see what it does, but I don't understand what tools I'm working with.

Link to comment
Share on other sites

$max is the number of names to display before switching to the next column. I set it at one more than the intended number so that (0%$max) doesn't immediately start a new table.

 

What the code does is count every time that a name is printed and check if that number is a multiple of the maximum rows intended. If it is, it will start a new column by ending the table and creating a new one right next to it.

Link to comment
Share on other sites

I just tested it in IE and FF and it put the tables next to eachother for me. Did you leave the "display:inline" style in the table html?

 

I guess if the names were too long and your browser wasn't wide enough it would have to put the next table below it. Can I see the html output from your code?

Link to comment
Share on other sites

I already have deleted it because I found a new command to try: ceil().

 

You can help me if you could show me how to display the nth result of a query assuming standard $row formatting:

 

<?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());
$num_results = mysql_num_rows($result);
$num_cols = 3;
$num_rows_decimal = $num_results / $num_cols;
$num_rows = ceil($num_rows_decimal);

   echo "num_results: ". $num_results. "<br>"; 
   echo "num_cols: ". $num_cols. "<br>"; 
   echo "num_rows: ". $num_rows. "<br><hr>"; 

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

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

Link to comment
Share on other sites

Not fully tested but try

$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());

$rows = array();
$num_cols = 3;

$j = 1;
while($row = mysql_fetch_assoc($rslt))
{
    $data = $row['NameLast'].', '.$row['NamePrefix'].' '.$row['NameFirst'].' '.$row['NameMiddle'].$row['NameSuffix'].' ';
    @$rows[$j] .= '    <td>'.$data."</td>\n";

    if($j == $num_cols)
        $j = 0;

    $j++;
}

echo "<table>\n";

foreach($rows as $row)
{
    echo "  <tr>\n" . $row . "  </tr>\n";
}

echo '</table>';

Link to comment
Share on other sites

Not fully tested but try:

 

Hey, It displayed 3 rows, so I messed around with variables:

 

<?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());
$num_results = mysql_num_rows($result);
$num_cols = 3;
$num_rows_decimal = $num_results / $num_cols;
$num_rows = ceil($num_rows_decimal);

   echo "num_results: ". $num_results. "<br>"; 
   echo "num_cols: ". $num_cols. "<br>"; 
   echo "num_rows: ". $num_rows. "<br><hr>"; 


$rows = array();

$j = 1;
while($row = mysql_fetch_assoc($result))
{
$data = $row['NameLast'].', '.$row['NamePrefix'].' '.$row['NameFirst'].' '.$row['NameMiddle'].$row['NameSuffix'].' ';
@$rows[$j] .= ' <td>'.$data."</td>\n";

if($j == $num_rows)
$j = 0;

$j++;
}

echo "<table>\n";

foreach($rows as $row)
{
echo " <tr>\n" . $row . " </tr>\n";
}

echo '</table>'; 

?>

Link to comment
Share on other sites

  • 2 years later...

Hi,

 

I've implemented this on Firefox and it works perfectly but it does not render the table correctly on IE8 and Chrome 10 (missing blank tds and the td border so table looks incomplete).

 

Has anyone else come across this problem and found a solution ?

 

thanks in advance

 

a

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.