Sydcomebak Posted August 14, 2008 Share Posted August 14, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/ Share on other sites More sharing options...
lemmin Posted August 14, 2008 Share Posted August 14, 2008 Did you try the method that I suggested on your last topic? Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616651 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616659 Share on other sites More sharing options...
lemmin Posted August 14, 2008 Share Posted August 14, 2008 $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. Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616702 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 Lemmin, it printed tables one after the other but they weren't next to each other. Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616738 Share on other sites More sharing options...
lemmin Posted August 14, 2008 Share Posted August 14, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616772 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 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]. " "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616776 Share on other sites More sharing options...
wildteen88 Posted August 14, 2008 Share Posted August 14, 2008 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616851 Share on other sites More sharing options...
Sydcomebak Posted August 14, 2008 Author Share Posted August 14, 2008 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-616895 Share on other sites More sharing options...
tmfl Posted April 29, 2011 Share Posted April 29, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-1208362 Share on other sites More sharing options...
wildteen88 Posted April 29, 2011 Share Posted April 29, 2011 Start a new thread. This thread is 3 years old. Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-1208371 Share on other sites More sharing options...
tmfl Posted April 29, 2011 Share Posted April 29, 2011 I did....and didnt really get any replys for a solution...... if you have one could you post here then http://www.phpfreaks.com/forums/index.php?topic=331778.0 thanks a Quote Link to comment https://forums.phpfreaks.com/topic/119693-solved-displaying-results-in-columns-part-ii/#findComment-1208381 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.