Jump to content

Subdividing long narrow tables


EngineeringGuy

Recommended Posts

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

Link to comment
Share on other sites

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

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.