soycharliente Posted October 28, 2008 Share Posted October 28, 2008 What's the best way to change the results from horizontally ordered to vertically ordered? I don't need a rewrite of code, but a point in the right direction. I don't really know how to start thinking about it. Currently the results come out like this ... 123 456 789 and I would like them to come out like this ... 147 258 369 <?php function listUndergrads() { dbconnect(); $query = "SELECT * FROM `profiles` WHERE `status`='U' OR `status`='P' ORDER BY `badge` ASC"; $result = mysql_query($query) OR DIE ("Error1: listUndergrads()"); if (mysql_num_rows($result) > 0) { $undergrads = "<table id=\"undergrads\" width=\"100%\" style=\"clear:both;\">\n"; $i = 0; $max_columns = 3; while ($r = mysql_fetch_array($result)) { $status = $r['status']; $badge = $r['badge']; $name = getName($badge); $undergrads .= ($i == 0) ? "<tr>\n" : ""; // open row if counter is zero $undergrads .= ($status == "U") ? "<td width=\"33%\">{$name}</td>\n" : "<td width=\"33%\">{$name}*</td>\n"; /*$undergrads .= ($status == "U") ? "<td><a href=\"showMember.php?id={$badge}\" title=\"{$name}\">{$name}</a></td>\n" : "<td><a href=\"showMember.php?id={$badge}\" title=\"{$name}\">{$name}</a>*</td>\n";*/ if(++$i == $max_columns) { $undergrads .= "</tr>\n"; $i = 0; } } if($i < $max_columns) { for($j = $i; $j < $max_columns; $j++) { $undergrads .= "<td> </td>\n"; } } $undergrads .= "</tr>\n"; $undergrads .= "</table>\n"; dbclose(); return $undergrads; } else { dbclose(); DIE ("Error2: listUndergrads()"); } } ?> Link to comment https://forums.phpfreaks.com/topic/130463-solved-changing-table-output-direction/ Share on other sites More sharing options...
soycharliente Posted October 28, 2008 Author Share Posted October 28, 2008 I figured out a "solution". It's basically just 3 cells, one for each column, instead of individual cells for each name. I calculated how many people should be in each column for it to stay balanced and just used that number to determine when to move to the next column. I don't know if it's very efficient. Any ideas? <?php function listUndergrads2() { dbconnect(); $query = "SELECT * FROM `profiles` WHERE `status`='U' OR `status`='P' ORDER BY `badge` ASC"; $result = mysql_query($query) OR DIE ("Error1: listUndergrads2()"); $num_results = mysql_num_rows($result); if ($num_results > 0) { $undergrads = "<table id=\"undergrads\" width=\"100%\" style=\"clear:both;\">\n<tr>\n"; $i = 0; $max_columns = 3; $percent = floor(100/$max_columns); $max_per_column = ceil($num_results/$max_columns); while ($r = mysql_fetch_array($result)) { $status = $r['status']; $badge = $r['badge']; $name = getName($badge); $undergrads .= ($i == 0) ? "<td width=\"{$percent}%\">\n" : ""; // open cell if counter is zero $undergrads .= ($status == "U") ? "{$name}" : "{$name}*"; /*$undergrads .= ($status == "U") ? "<a href=\"showMember.php?id={$badge}\" title=\"{$name}\">{$name}</a>" : "<a href=\"showMember.php?id={$badge}\" title=\"{$name}\">{$name}</a>*";*/ $undergrads .= ($i < $max_per_column - 1) ? "<br />" : ""; if(++$i == $max_per_column) { $undergrads .= "</td>\n"; $i = 0; } } if($i < $max_per_column) { for($j = $i; $j < $max_per_column; $j++) { $undergrads .= " "; $undergrads .= ($j < $max_per_column - 1) ? "<br />" : ""; } $undergrads .= "</td>\n"; } $undergrads .= "</tr>\n"; $undergrads .= "</table>\n"; dbclose(); return $undergrads; } else { dbclose(); DIE ("Error2: listUndergrads2()"); } } ?> Link to comment https://forums.phpfreaks.com/topic/130463-solved-changing-table-output-direction/#findComment-676874 Share on other sites More sharing options...
n3ightjay Posted October 28, 2008 Share Posted October 28, 2008 I created some logic while you posted your solution: your current display logic: <table> <? $result = array(1,2,3,4,5,6,7,8,9); for($i = 0; $i < 3; $i++){ echo "<tr>"; for($j = 0;$j < 3; $j++){ echo "<td>" . $result[($i*3)+$j] . "</td>"; } echo "</tr>"; } ?> </table> logic to switch: <table> <? $result = array(1,2,3,4,5,6,7,8,9); for($i = 0; $i < 3; $i++){ echo "<tr>"; for($j = 0;$j < 3; $j++){ echo "<td>" . $result[($j*3)+$i] . "</td>"; } echo "</tr>"; } ?> </table> this is assuming you always have a 3x3 grid though i hope this helps Link to comment https://forums.phpfreaks.com/topic/130463-solved-changing-table-output-direction/#findComment-676885 Share on other sites More sharing options...
soycharliente Posted October 28, 2008 Author Share Posted October 28, 2008 Yeah. I was looking at this article on Wikipedia (http://en.wikipedia.org/wiki/In-place_matrix_transposition#Algorithms) about doing something like that. I will never have and NxN matrix though. It will always be an Nx3 matrix. Thanks though. Link to comment https://forums.phpfreaks.com/topic/130463-solved-changing-table-output-direction/#findComment-676888 Share on other sites More sharing options...
n3ightjay Posted October 28, 2008 Share Posted October 28, 2008 I know you clicked solved but i just had to finish this ... this will work for any matix setup in the layout order you wanted: <table> <?php $result = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25); $numRows = 5; $numCols = 3; for($i = 0; $i < $numRows; $i++){ echo "<tr>"; for($j = 0;$j < $numCols; $j++){ echo "<td>" . $result[($j*$numRows)+$i] . "</td>"; } echo "</tr>"; } ?> </table> Link to comment https://forums.phpfreaks.com/topic/130463-solved-changing-table-output-direction/#findComment-676899 Share on other sites More sharing options...
soycharliente Posted October 28, 2008 Author Share Posted October 28, 2008 And I would just add each name to the list instead of adding it to the HTML output? Hmmm. Let's see how it works out. I'll have to do it when I get back later tonight. Link to comment https://forums.phpfreaks.com/topic/130463-solved-changing-table-output-direction/#findComment-676942 Share on other sites More sharing options...
soycharliente Posted October 29, 2008 Author Share Posted October 29, 2008 This is how I applied the advice. Works just fine. Thanks a lot. <?php function listUndergrads3() { dbconnect(); $query = "SELECT * FROM `profiles` WHERE `status`='U' OR `status`='P' ORDER BY `badge` ASC"; $result = mysql_query($query) OR DIE ("Error1: listUndergrads3()"); $num_results = mysql_num_rows($result); if ($num_results > 0) { $i = 0; $max_columns = 3; $percent = floor(100/$max_columns); $max_per_column = ceil($num_results/$max_columns); $list = array(); while ($r = mysql_fetch_array($result)) { $status = $r['status']; $badge = $r['badge']; $name = getName($badge); array_push($list, "{$name}...{$badge}"); if(++$i == $max_per_column) { $i = 0; } } if($i < $max_per_column) { for($j = $i; $j < $max_per_column; $j++) { array_push($list, " "); } } $undergrads = "<table id=\"undergrads\" style=\"width:100%;clear:both;\">\n"; for ($i = 0; $i < $max_per_column; $i++) { $undergrads .= "<tr>\n"; for ($j = 0; $j < $max_columns; $j++) { $undergrads .= "<td width=\"{$percent}%\">" . $list[($j*$max_per_column)+$i] . "</td>\n"; } $undergrads .= "</tr>\n"; } $undergrads .= "</table>\n"; dbclose(); return $undergrads; } else { dbclose(); DIE ("Error2: listUndergrads3()"); } } ?> Link to comment https://forums.phpfreaks.com/topic/130463-solved-changing-table-output-direction/#findComment-677716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.