EngineeringGuy Posted December 7, 2007 Share Posted December 7, 2007 I have a table with four columns that are five to ten characters wide. There are approximately fifty rows in the the table (though it needs to dynamically be able to get longer and shorter, depending on results from a mysql query) I have been having a heck of a time trying to take the results and display the first half on the left and the second half on the right of the page. I am trying to take a long narrow table: Result 11Result 12Result 13Result 14 Result 21Result 22Result 23Result 24 Result 31Result 32Result 33Result 34 Result 41Result 42Result 43Result 44 and turn it into this: Result 11 Result 12 Result 13 Result 14 | Result 31 Result 32 Result 33 Result 34 Result 21Result 22Result 23Result 24|Result 41Result 42Result 43Result 44 Quote Link to comment Share on other sites More sharing options...
cunoodle2 Posted December 7, 2007 Share Posted December 7, 2007 Do some research on putting your mysql query result into an array, sort it and then reference it via index numbers. Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 7, 2007 Share Posted December 7, 2007 One parent table with two nested child tables will allow what you want... <?php // Build results into array $sql = "SELECT `someField` FROM `someTable`"; if ( !$result = mysql_query($sql) ) { die('MySQL Error: ' . mysql_error()); } while ( list($val) = mysql_fetch_assoc($result) ) { $dataArray[] = $val; } // Create 4 arrays of 4 cells each $content = ''; for ( $x = 0 ; $x < 4 ; $x++ ) { for ( $i = 1*(4*$x) ; $i < 4+(4*$x) ; $i++ ) { $content[$x] .= " <td>$dataArray[$i]</td>\n"; } } // Build the tables // Open parent echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <tr> <td>\n"; // Build 2 child tables for ( $x = 0 ; $x < 2 ; $x++ ) { if ( $x == 1 ) { // Close child 1, Open child 2 echo " </td>\n <td>\n"; } echo " <table border=\"1\" cellpadding=\"2\" cellspacing=\"1\">\n"; for ( $i = 1*(2*$x) ; $i < 2+(2*$x) ; $i++ ) { // Open child row(s), send cells, close row(s) echo " <tr>\n$content[$i] </tr>\n"; } echo" </table>\n"; } // Close parent echo " </td> </tr> </table>"; ?> Example: <?php // emulate MySQL results populating array for ( $x=0 ; $x < 16 ; $x++ ) { $dataArray[$x] = 'Result ' . ($x + 1); } // Create 4 arrays of 4 cells each $content = ''; for ( $x = 0 ; $x < 4 ; $x++ ) { for ( $i = 1*(4*$x) ; $i < 4+(4*$x) ; $i++ ) { $content[$x] .= " <td>$dataArray[$i]</td>\n"; } } // Build the tables // Open parent echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\"> <tr> <td>\n"; // Build 2 child tables for ( $x = 0 ; $x < 2 ; $x++ ) { if ( $x == 1 ) { // Close child 1, Open child 2 echo " </td>\n <td>\n"; } echo " <table border=\"1\" cellpadding=\"2\" cellspacing=\"1\">\n"; for ( $i = 1*(2*$x) ; $i < 2+(2*$x) ; $i++ ) { // Open child row(s), send cells, close row(s) echo " <tr>\n$content[$i] </tr>\n"; } echo" </table>\n"; } // Close parent echo " </td> </tr> </table>"; ?> HTML Source Output: <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> <table border="1" cellpadding="2" cellspacing="1"> <tr> <td>Result 1</td> <td>Result 2</td> <td>Result 3</td> <td>Result 4</td> </tr> <tr> <td>Result 5</td> <td>Result 6</td> <td>Result 7</td> <td>Result 8</td> </tr> </table> </td> <td> <table border="1" cellpadding="2" cellspacing="1"> <tr> <td>Result 9</td> <td>Result 10</td> <td>Result 11</td> <td>Result 12</td> </tr> <tr> <td>Result 13</td> <td>Result 14</td> <td>Result 15</td> <td>Result 16</td> </tr> </table> </td> </tr> </table> PhREEEk Quote Link to comment Share on other sites More sharing options...
EngineeringGuy Posted December 7, 2007 Author Share Posted December 7, 2007 thank you! I'll give this a shot this morning! Quote Link to comment 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.